Presentation is loading. Please wait.

Presentation is loading. Please wait.

J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes.

Similar presentations


Presentation on theme: "J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes."— Presentation transcript:

1

2 J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes

3 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

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

5 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

6 J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Class Template

7 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

8 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

9 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; // [ 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 ]

10 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; };

11 Ch 15/ Foil 10 Auxiliary Operators template ostream& operator &A); template istream& operator>> (istream &sin, Vectors &A);

12 Ch 15/ Foil 11 Default Constructor template Vectors ::Vectors(int n) { assert(n > 0); NumberValues = n; Values = new T [n]; assert(Values); }

13 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]; }

14 Ch 15/ Foil 13 Destructor template Vectors ::~Vectors() { delete [] Values; NumberValues = 0; }

15 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; }

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

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

18 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; }

19 Ch 15/ Foil 18 l See g:\ds\ap\apvector.h

20 Ch 15/ Foil 19


Download ppt "J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes."

Similar presentations


Ads by Google