J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes
Ch 15/ Foil 2 Templates l A form that can generate a n function n class l when particulars are supplied l Better than overloading n Rewriting function for different types
Ch 15/ Foil 3 Templates l Two kinds n Function generators template T min(const T &a, const T &b) : n Class generators template class ClassName { public: :
Ch 15/ Foil 4 template Syntax l template l template parameter list may include n type template parameter –class X n value template parameter –int n l template
J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Class Template
Ch 15/ Foil 6 Class Templates l A form that generates a class when particulars are supplied template class TC { public: void Assign(X xvalue); //... private: X ValueVectors[n]; }; l Declaration of TC object: TC A; // A.ValueVectors has 20 int’s
Ch 15/ Foil 7 Sample Templatized Class l Vector Class n Similar to STL Vector l Generic element types l Random access to elements l Dynamic memory allocation
Ch 15/ Foil 8 Homegrown Generic Vectors Vectors A(5, 0); // A is five 0's const Vectors B(6, 1); // B is six 1's Vectors C; // C is ten 0/1's A = B; A[5] = 3; A[B[1]] = 2; cout << "A = " << A << endl; // [ ] cout << "B = " << B << endl; // [ ] cout << "C = " << C << endl; // [ 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 0/1 ]
template class Vectors { public: Vectors(int n = 10); Vectors(int n, const T &val); Vectors(const T A[], int n); Vectors(const Vectors &A); ~Vectors(); int Size() const { return NumberValues; } Vectors & operator=(const Vectors &A); const T& operator[](int i) const; T& operator[](int i); private: T *Values; int NumberValues; };
Ch 15/ Foil 10 Auxiliary Operators template ostream& operator &A); template istream& operator>> (istream &sin, Vectors &A);
Ch 15/ Foil 11 Default Constructor template Vectors ::Vectors(int n) { assert(n > 0); NumberValues = n; Values = new T [n]; assert(Values); }
Ch 15/ Foil 12 Copy Constructor template Vectors ::Vectors(const Vectors &A) { NumberValues = A.Size(); Values = new T [A.Size()]; assert(Values); for (int i = 0; i < A.Size(); ++i) Values[i] = A[i]; }
Ch 15/ Foil 13 Destructor template Vectors ::~Vectors() { delete [] Values; NumberValues = 0; }
Ch 15/ Foil 14 Member Assignment template Vectors & Vectors ::operator=(const Vectors &A){ if (this != &A) { if (Size() != A.Size()) { delete [] Values; NumberValues = A.Size(); Values = new T [A.Size()]; assert(Values); } for (int i = 0; i < A.Size(); ++i) Values[i] = A[i]; } return *this; }
Ch 15/ Foil 15 Inspector for Constant Vectorss template const T& Vectors ::operator[](int i) const { assert((i >= 0) && (i < Size())); return Values[i]; }
Ch 15/ Foil 16 Non-Constant Inspector/Mutator template T& Vectors ::operator[](int i) { assert((i >= 0) && (i < Size())); return Values[i]; }
Ch 15/ Foil 17 Insertion Operator template ostream& operator<<(ostream &sout, const Vectors &A) { sout << "[ "; for (int i = 0; i < A.Size(); ++i) sout << A[i] << " "; sout << "]"; return sout; }
Ch 15/ Foil 18 l See g:\ds\ap\apvector.h
Ch 15/ Foil 19