Templates 679
19
21:
22: private:
23: int itsWeight;
24: };
25:
26: // extraction operator for printing animals
27: ostream& operator<<
28: (ostream& theStream, const Animal& theAnimal)
29: {
30: theStream << theAnimal.GetWeight();
31: return theStream;
32: }
33:
34: Animal::Animal(int weight):
35: itsWeight(weight)
36: {
37: // cout << “Animal(int)” << endl;
38: }
39:
40: Animal::Animal():
41: itsWeight(0)
42: {
43: // cout << “Animal()” << endl;
44: }
45:
46: Animal::~Animal()
47: {
48: // cout << “Destroyed an animal...” << endl;
49: }
50:
51: template <class T> // declare the template and the parameter
52: class Array // the class being parameterized
53: {
54: public:
55: Array(int itsSize = DefaultSize);
56: Array(const Array &rhs);
57: ~Array() { delete [] pType; }
58:
59: Array& operator=(const Array&);
60: T& operator[](int offSet) { return pType[offSet]; }
61: const T& operator[](int offSet) const
62: { return pType[offSet]; }
63: int GetSize() const { return itsSize; }
64: // friend function:
65: // template <class T>
66: friend ostream& operator<< (ostream&, const Array<T>&);
67:
68: private:
69: T *pType;
LISTING19.5 continued