1 / 19

Templates

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 >

tauret
Download Presentation

Templates

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Templates Generic functions and classes

  2. Templates • A form that can generate a • function • class • when particulars are supplied • Better than overloading • Rewriting function for different types

  3. Templates • Two kinds • Function generators template <class T> T min(const T &a, const T &b) : • Class generators template <class X> class ClassName { public: :

  4. 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>

  5. Class Template

  6. 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

  7. Sample Templatized Class • Vector Class • Similar to STL Vector • Generic element types • Random access to elements • Dynamic memory allocation

  8. 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 ]

  9. 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; };

  10. Auxiliary Operators template <class T> ostream& operator<<(ostream &sout, const Vectors<T> &A); template <class T> istream& operator>> (istream &sin, Vectors<T> &A);

  11. Default Constructor template <class T> Vectors<T>::Vectors(int n) { assert(n > 0); NumberValues = n; Values = new T [n]; assert(Values); }

  12. 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]; }

  13. Destructor template <class T> Vectors<T>::~Vectors() { delete [] Values; NumberValues = 0; }

  14. 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; }

  15. Inspector for Constant Vectorss template <class T> const T& Vectors<T>::operator[](int i) const { assert((i >= 0) && (i < Size())); return Values[i]; }

  16. Non-Constant Inspector/Mutator template <class T> T& Vectors<T>::operator[](int i) { assert((i >= 0) && (i < Size())); return Values[i]; }

  17. 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; }

  18. See g:\ds\ap\apvector.h

More Related