Sams Teach Yourself C++ in 21 Days

(singke) #1
48: friend void Intrude(Array<int>);
49:
50: private:
51: T *pType;
52: int itsSize;
53: };
54:
55: // friend function. Not a template, can only be used
56: // with int arrays! Intrudes into private data.
57: void Intrude(Array<int> theArray)
58: {
59: cout << endl << “*** Intrude ***” << endl;
60: for (int i = 0; i < theArray.itsSize; i++)
61: cout << “i: “ << theArray.pType[i] << endl;
62: cout << endl;
63: }
64:
65: // implementations follow...
66:
67: // implement the Constructor
68: template <class T>
69: Array<T>::Array(int size):
70: itsSize(size)
71: {
72: pType = new T[size];
73: // the constructors of the type you are creating
74: // should set a default value
75: }
76:
77: // copy constructor
78: template <class T>
79: Array<T>::Array(const Array &rhs)
80: {
81: itsSize = rhs.GetSize();
82: pType = new T[itsSize];
83: for (int i = 0; i < itsSize; i++)
84: pType[i] = rhs[i];
85: }
86:
87: // operator=
88: template <class T>
89: Array<T>& Array<T>::operator=(const Array &rhs)
90: {
91: if (this == &rhs)
92: return *this;
93: delete [] pType;
94: itsSize = rhs.GetSize();
95: pType = new T[itsSize];

672 Day 19


LISTING19.3 continued
Free download pdf