#ifndef ARRAY_CLASS #define ARRAY_CLASS #include <iostream> #include <stdlib.h> //Array class template template <class TA> class Array { public: Array(int sz = 50); //Constructor Array(const Array<TA>& A); //copy constructor ~Array(void); //destructor //Overload the subscript "[ ]", so that the Array object can play the role of a C++ ordinary array TA& operator[ ](int i); private: TA* a; //TA type pointer, used to store the first address of the dynamically allocated array memory int size; //array size (number of elements) }; //The following is the implementation of the class member function // Constructor template <class TA> Array<TA>::Array(int sz) { size = sz; // assign the number of elements to the variable size a = new TA[size]; //Dynamic allocation of size elements of TA type space } // destructor template <class TA> Array<TA>::~Array(void) { delete[] a; } // copy constructor template <class TA> Array<TA>::Array(const Array<TA>& X) { //Get the size of the array from object X and assign it to a member of the current object int n = X.size; size = n; // Allocate memory for the object and check for errors a = new TA[n]; // Dynamically allocate n elements of TA type space // copy array elements from object X to this object TA* src = Xa; // Xa is the first address of the array of object X TA* dest = a; // a is the first address of the array in this object while (n--) // copy array elements one by one * dest ++ = * src ++; } // Overload the subscript operator to access elements by subscript like ordinary arrays, and has out-of-bounds check function template <class TA> TA& Array<TA>::operator[ ] (int n) { // Check if the subscript is out of bounds if (n < 0 || n > size - 1) { cout << n << " is out_of_bound.\n"; exit(1); } return a[n]; // return the array element with index n } #endif // ARRAY_CLASS #pragma once
#include <iostream> #include <stdlib.h> #include "array.h" using std::cout; using std::endl; intmain() { int i; //The array class template Array is instantiated as an int template class to create an integer array Array<int> intarr1(10); for (i = 0; i<10; i++) intarr1[i] = i + 3; //array elements as lvalues cout << "int array1:"; for (i = 0; i<10; i++) cout << intarr1[i] << " "; //return element value cout << endl; //The array class template Array is instantiated as a float template class to create a real array Array<double> douarr(8); for (i = 0; i<8; i++) douarr[i] = (double)(i + 1)*3.14; cout << "double array:"; for (i = 0; i<8; i++) cout << douarr[i] << " "; cout << endl; //The array class template Array is instantiated as an int template class, and the integer array intarr2 is generated by copying the array intarr1 Array<int> intarr2(intarr1); cout << "int array2:"; for (i = 0; i<10; i++) cout << intarr2[i] << " "; cout << endl; // subscript out of bounds check intarr1 [20] = 100; return 0; }
/*Class template static member example*/ #include <iostream> using std::cout; using std::endl; template <class T> class ClassA { public: T getValue() { return a; } void setValue(T val) { a = val; } private: static T a; //class template static member a }; int ClassA < int > ::a = 100; //Initialization of class template static member a char ClassA < char > ::a = 'Z'; //Initialization of class template static member a intmain() { ClassA< int > ObjectInt1, ObjectInt2; ClassA< char > ObjectChar1, ObjectChar2; ObjectInt1.setValue(200); cout << ObjectInt2.getValue() << " , " << ObjectChar2.getValue(); cout << endl; ObjectChar1.setValue('X'); cout << ObjectInt2.getValue() << " , " << ObjectChar2.getValue(); cout << endl; return 0; }
/* An instance of a generic array class template friend */ /* The friend functions of the template class are divided into: bound (bound) friend functions and unbound (unbound) friend functions*/ #include <iostream> using std::cout; using std::endl; template <typename T> class Array; template <typename T> void Print(Array <T> & myarr); template<class T1, class T2> void ItoF (Array <T1> &Iarr, Array <T2> &Carr); template <class T> class Array //Generic array class template definition { public: Array(int s) // constructor { size = s; elem = new T[size]; // allocate array space } Array() { delete [] item; } T& operator[] (int index) //Subscript operator[] overloaded function { return elem[index]; } // uninstantiated friend function template declaration //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! friend void Print<>(Array <T> & myarr); // instantiated friend function template declaration friend void ItoF<>(Array <int> &Iarr, Array <char> &Carr); private: T * element; int size; }; // uninstantiated friend function template definition template<class T> void Print (Array <T> &myarr) { for (int i = 0; i < myarr.size; i++) cout << myarr.elem[i] << " "; } //void Print(Array <char> &myarr) //{ // for (int i = 0; i < myarr.size; i++) // cout << myarr.elem[i] << " "; //} // instantiated friend function template definition template<class T1, class T2> void ItoF (Array <T1> &Iarr, Array <T2> &Carr) { Carr.size = Iarr.size; for (int i = 0; i < Iarr.size; i++) //Force type conversion, convert array elements of type T1 to array elements of type T2 Carr.elem[i] = T2(Iarr.elem[i]); } intmain() { Array< int > arri(10); // Generate int type template class and create int type array object Array< char > arrc(10); // Generate char type template class and create char type array object for (int i = 0; i < 10; i++) arri[i] = i + 65; Print(arri); cout << endl; ItoF(arri, arrc); Print(arrc); cout << endl; return 0; }
// manyfrnd.cpp -- unbound template friend to a template class #include <iostream> using std::cout; using std::endl; template <typename T> class ManyFriend { private: T item; public: ManyFriend(const T & i) : item(i) {} template <typename C, typename D> friend void show2(C &, D &); }; template <typename C, typename D> void show2(C & c, D & d) { cout << c.item << ", " << d.item << endl; } intmain() { ManyFriend<int> hfi1(10); ManyFriend<int> hfi2(20); ManyFriend<double> hfdb(10.5); cout << "hfi1, hfi2: "; show2(hfi1, hfi2); cout << "hfdb, hfi2: "; show2(hfdb, hfi2); // std::cin.get(); return 0; }