Sams Teach Yourself C++ in 21 Days

(singke) #1
19:
20: Animal::Animal(int weight):
21: itsWeight(weight)
22: {}
23:
24: Animal::Animal():
25: itsWeight(0)
26: {}
27:
28:
29: template <class T> // declare the template and the parameter
30: class Array // the class being parameterized
31: {
32: public:
33: // constructors
34: Array(int itsSize = DefaultSize);
35: Array(const Array &rhs);
36: ~Array() { delete [] pType; }
37:
38: // operators
39: Array& operator=(const Array&);
40: T& operator[](int offSet) { return pType[offSet]; }
41: const T& operator[](int offSet) const
42: { return pType[offSet]; }
43: // accessors
44: int GetSize() const { return itsSize; }
45:
46: private:
47: T *pType;
48: int itsSize;
49: };
50:
51: // implementations follow...
52:
53: // implement the Constructor
54: template <class T>
55: Array<T>::Array(int size):
56: itsSize(size)
57: {
58: pType = new T[size];
59: // the constructors of the type you are creating
60: // should set a default value
61: }
62:
63: // copy constructor
64: template <class T>
65: Array<T>::Array(const Array &rhs)
66: {

666 Day 19


LISTING19.2 continued
Free download pdf