Templates 675
19
3:
4: const int DefaultSize = 10;
5:
6: class Animal
7: {
8: public:
9: Animal(int);
10: Animal();
11: ~Animal() {}
12: int GetWeight() const { return itsWeight; }
13: void Display() const { cout << itsWeight; }
14: private:
15: int itsWeight;
16: };
17:
18: Animal::Animal(int weight):
19: itsWeight(weight)
20: {}
21:
22: Animal::Animal():
23: itsWeight(0)
24: {}
25:
26: template <class T> // declare the template and the parameter
27: class Array // the class being parameterized
28: {
29: public:
30: // constructors
31: Array(int itsSize = DefaultSize);
32: Array(const Array &rhs);
33: ~Array() { delete [] pType; }
34:
35: // operators
36: Array& operator=(const Array&);
37: T& operator[](int offSet) { return pType[offSet]; }
38: const T& operator[](int offSet) const
39: { return pType[offSet]; }
40: // accessors
41: int GetSize() const { return itsSize; }
42: // template <class T>
43: friend ostream& operator<< (ostream&, Array<T>&);
44:
45: private:
46: T *pType;
47: int itsSize;
48: };
49:
50: template <class T>
51: ostream& operator<< (ostream& output, Array<T>& theArray)
LISTING19.4 continued