Latest

recent

Implementing the Template in C++


The full implementation of the Array template class requires implementation of the copy constructor, operator=, and so forth. Listing 19.2 provides a simple driver program to exercise this template class.

NOTE: Some older compilers do not support templates. Templates are, however, part of the emerging C++ standard. All major compiler vendors have committed to supporting templates in their next release, if they have not already done so. If you have an older compiler, you won't be able to compile and run the exercises in this chapter. It's still a good idea to read through the entire chapter, however, and return to this material when you upgrade your compiler.
Listing 19.2. The implementation of the template array.

1: #include <iostream.h>
2:
3: const int DefaultSize = 10;
4:
5: // declare a simple Animal class so that we can
6: // create an array of animals
7:
8: class Animal
9: {
10: public:
11: Animal(int);
12: Animal();
13: ~Animal() {}
14: int GetWeight() const { return itsWeight; }
15: void Display() const { cout << itsWeight; }
16: private:
17: int itsWeight;
18: };
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 = DefaultSize):
56: itsSize(size)
57: {
58: pType = new T[size];
59: for (int i = 0; i<size; i++)
60: pType[i] = 0;
61: }
62:
63: // copy constructor
64: template <class T>
65: Array<T>::Array(const Array &rhs)
66: {
67: itsSize = rhs.GetSize();
68: pType = new T[itsSize];
69: for (int i = 0; i<itsSize; i++)
70: pType[i] = rhs[i];
71: }
72:
73: // operator=
74: template <class T>
75: Array<T>& Array<T>::operator=(const Array &rhs)
76: {
77: if (this == &rhs)
78: return *this;
79: delete [] pType;
80: itsSize = rhs.GetSize();
81: pType = new T[itsSize];
82: for (int i = 0; i<itsSize; i++)
83: pType[i] = rhs[i];
84: return *this;
85: }
86:
87: // driver program
88: int main()
89: {
90: Array<int> theArray; // an array of integers
91: Array<Animal> theZoo; // an array of Animals
92: Animal *pAnimal;
93:
94: // fill the arrays
95: for (int i = 0; i < theArray.GetSize(); i++)
96: {
97: theArray[i] = i*2;
98: pAnimal = new Animal(i*3);
99: theZoo[i] = *pAnimal;
100: delete pAnimal;
101: }
102: // print the contents of the arrays
103: for (int j = 0; j < theArray.GetSize(); j++)
104: {
105: cout << "theArray[" << j << "]:\t";
106: cout << theArray[j] << "\t\t";
107: cout << "theZoo[" << j << "]:\t";
108: theZoo[j].Display();
109: cout << endl;
110: }
111:
112: for (int k = 0; k < theArray.GetSize(); k++)
113: delete &theZoo[j];
114: return 0;
115: }
 

 Output: theArray[0]: 0 theZoo[0]: 0
theArray[1]: 2 theZoo[1]: 3
theArray[2]: 4 theZoo[2]: 6
theArray[3]: 6 theZoo[3]: 9
theArray[4]: 8 theZoo[4]: 12
theArray[5]: 10 theZoo[5]: 15
theArray[6]: 12 theZoo[6]: 18
theArray[7]: 14 theZoo[7]: 21
theArray[8]: 16 theZoo[8]: 24
theArray[9]: 18 theZoo[9]: 27

Analysis: Lines 8 to 26 provide a stripped-down Animal class, created here so that there are objects of a user-defined type to add to the array.

Line 29 declares that what follows is a template, and that the parameter to the template is a type, designated as T. The Array class has two constructors as shown, the first of which takes a size and defaults to the constant integer DefaultSize.

The assignment and offset operators are declared, with the latter declaring both a const and a non-const variant. The only accessor provided is GetSize(), which returns the size of the array.

One can certainly imagine a fuller interface, and, for any serious Array program, what has been supplied here would be inadequate. At a minimum, operators to remove elements, to expand the array, to pack the array, and so forth would be required.

The private data consists of the size of the array and a pointer to the actual in-memory array of objects. 
Implementing the Template in C++ Reviewed by 1000sourcecodes on 23:58 Rating: 5
Powered by Blogger.