Sams Teach Yourself C++ in 21 Days

(singke) #1
70: int itsSize;
71: };
72:
73: template <class T>
74: ostream& operator<< (ostream& output, const Array<T>& theArray)
75: {
76: for (int i = 0; i < theArray.itsSize; i++)
77: output << “[“ << i << “] “ << theArray[i] << endl;
78: return output;
79: }
80:
81: // implementations follow...
82:
83: // implement the Constructor
84: template <class T>
85: Array<T>::Array(int size):
86: itsSize(size)
87: {
88: pType = new T[size];
89: for (int i = 0; i < size; i++)
90: pType[i] = 0;
91: }
92:
93: // copy constructor
94: template <class T>
95: Array<T>::Array(const Array &rhs)
96: {
97: itsSize = rhs.GetSize();
98: pType = new T[itsSize];
99: for (int i = 0; i < itsSize; i++)
100: pType[i] = rhs[i];
101: }
102:
103: void IntFillFunction(Array<int>& theArray);
104: void AnimalFillFunction(Array<Animal>& theArray);
105:
106: int main()
107: {
108: Array<int> intArray;
109: Array<Animal> animalArray;
110: IntFillFunction(intArray);
111: AnimalFillFunction(animalArray);
112: cout << “intArray...\n” << intArray;
113: cout << “\nanimalArray...\n” << animalArray << endl;
114: return 0;
115: }
116:
117: void IntFillFunction(Array<int>& theArray)
118: {

680 Day 19


LISTING19.5 continued
Free download pdf