190 likes | 368 Views
Templates. Generic functions and classes. Templates. A form that can generate a function class when particulars are supplied Better than overloading Rewriting function for different types. Templates. Two kinds Function generators template < class T >
E N D
Templates Generic functions and classes
Templates • A form that can generate a • function • class • when particulars are supplied • Better than overloading • Rewriting function for different types
Templates • Two kinds • Function generators template <class T> T min(const T &a, const T &b) : • Class generators template <class X> class ClassName { public: :
template Syntax • template <template parameter list> • template parameter list may include • type template parameter • class X • value template parameter • int n • template <class X, int n>
Class Templates • A form that generates a class when particulars are supplied template <class X, int n> class TC { public: void Assign(X xvalue); // ... private: X ValueVectors[n]; }; • Declaration of TC object: TC<int, 20> A; // A.ValueVectors has 20 int’s
Sample Templatized Class • Vector Class • Similar to STL Vector • Generic element types • Random access to elements • Dynamic memory allocation
Homegrown Generic Vectors Vectors<int> A(5, 0); // A is five 0's const Vectors<int> B(6, 1); // B is six 1's Vectors<Rational> C; // C is ten 0/1's A = B; A[5] = 3; A[B[1]] = 2; cout << "A = " << A << endl; // [ 1 2 1 1 1 3 ] cout << "B = " << B << endl; // [ 1 1 1 1 1 1 ] 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 T> class Vectors { public: Vectors(int n = 10); Vectors(int n, const T &val); Vectors(const T A[], int n); Vectors(const Vectors<T> &A); ~Vectors(); int Size() const { return NumberValues; } Vectors<T> & operator=(const Vectors<T> &A); const T& operator[](int i) const; T& operator[](int i); private: T *Values; int NumberValues; };
Auxiliary Operators template <class T> ostream& operator<<(ostream &sout, const Vectors<T> &A); template <class T> istream& operator>> (istream &sin, Vectors<T> &A);
Default Constructor template <class T> Vectors<T>::Vectors(int n) { assert(n > 0); NumberValues = n; Values = new T [n]; assert(Values); }
Copy Constructor template <class T> Vectors<T>::Vectors(const Vectors<T> &A) { NumberValues = A.Size(); Values = new T [A.Size()]; assert(Values); for (int i = 0; i < A.Size(); ++i) Values[i] = A[i]; }
Destructor template <class T> Vectors<T>::~Vectors() { delete [] Values; NumberValues = 0; }
Member Assignment template <class T> Vectors<T>& Vectors<T>::operator=(const Vectors<T>&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; }
Inspector for Constant Vectorss template <class T> const T& Vectors<T>::operator[](int i) const { assert((i >= 0) && (i < Size())); return Values[i]; }
Non-Constant Inspector/Mutator template <class T> T& Vectors<T>::operator[](int i) { assert((i >= 0) && (i < Size())); return Values[i]; }
Insertion Operator template <class T> ostream& operator<<(ostream &sout, const Vectors<T> &A) { sout << "[ "; for (int i = 0; i < A.Size(); ++i) sout << A[i] << " "; sout << "]"; return sout; }