Sams Teach Yourself C++ in 21 Days

(singke) #1
Templates 673

19


96: for (int i = 0; i < itsSize; i++)
97: pType[i] = rhs[i];
98: return *this;
99: }
100:
101: // driver program
102: int main()
103: {
104: Array<int> theArray; // an array of integers
105: Array<Animal> theZoo; // an array of Animals
106: Animal *pAnimal;
107:
108: // fill the arrays
109: for (int i = 0; i < theArray.GetSize(); i++)
110: {
111: theArray[i] = i*2;
112: pAnimal = new Animal(i*3);
113: theZoo[i] = *pAnimal;
114: }
115:
116: int j;
117: for (j = 0; j < theArray.GetSize(); j++)
118: {
119: cout << “theZoo[“ << j << “]:\t”;
120: theZoo[j].Display();
121: cout << endl;
122: }
123: cout << “Now use the friend function to “;
124: cout << “find the members of Array<int>”;
125: Intrude(theArray);
126:
127: cout << endl <<”Done.” << endl;
128: return 0;
129: }

theZoo[0]: 0
theZoo[1]: 3
theZoo[2]: 6
theZoo[3]: 9
theZoo[4]: 12
theZoo[5]: 15
theZoo[6]: 18
theZoo[7]: 21
theZoo[8]: 24
theZoo[9]: 27
Now use the friend function to find the members of Array<int>
*** Intrude ***

OUTPUT


LISTING19.3 continued

Free download pdf