Sams Teach Yourself C++ in 21 Days

(singke) #1
Templates 685

19


72: int itsSize;
73: };
74:
75: template <class T>
76: Array<T>::Array(int size = DefaultSize):
77: itsSize(size)
78: {
79: pType = new T[size];
80: for (int i = 0; i < size; i++)
81: pType[i] = (T)0;
82: }
83:
84: template <class T>
85: Array<T>& Array<T>::operator=(const Array &rhs)
86: {
87: if (this == &rhs)
88: return *this;
89: delete [] pType;
90: itsSize = rhs.GetSize();
91: pType = new T[itsSize];
92: for (int i = 0; i < itsSize; i++)
93: pType[i] = rhs[i];
94: return *this;
95: }
96:
97: template <class T>
98: Array<T>::Array(const Array &rhs)
99: {
100: itsSize = rhs.GetSize();
101: pType = new T[itsSize];
102: for (int i = 0; i < itsSize; i++)
103: pType[i] = rhs[i];
104: }
105:
106:
107: template <class T>
108: ostream& operator<< (ostream& output, const Array<T>& theArray)
109: {
110: for (int i = 0; i<theArray.GetSize(); i++)
111: output << “[“ << i << “] “ << theArray[i] << endl;
112: return output;
113: }
114:
115:
116: Array<Animal>::Array(int AnimalArraySize):
117: itsSize(AnimalArraySize)
118: {
119: pType = new Animal[AnimalArraySize];
120: }

LISTING19.6 continued

Free download pdf