Dlist<int> a; // A dlist of ints Dlist<PayrollRec *> b; // A dlist of pointers to payroll records HashTable<int, string> x; // A hash table in which the key is // an integer and the value is a string
a.insertBeforeCursor(3); // inserts 3 before the cursor PayrollRec *p = new PayrollRec("Brad Vander Zanden", 20000.00); b.insertBeforeCursor(p); // inserts the pointer to the new // payroll record before the cursor string y = x.find(4); // return the value associated with 4
template <class Type1, class Type2, ..., class Typen>where template and class are keywords and Type1, ..., Typen are the names of the type parameters
// Element types are assumed to have defined operator= template <class Element, int ArraySize = 12> class DynArray { public: // operations performed on arrays DynArray ( int sz = ArraySize ); ~DynArray(); // return the value at location index Element getValue(int index); void setValue(int index, Element value); int getSize(); protected: int size; Element *data; }; #include "DynArray.cpp"
template <class Element, int StackSize = 12> class Stack { public: ... protected: int size; Element data[StackSize]; };
Here is an example of the DynArray class with the method bodies embedded in the class declaration:
#include <iostream> using namespace std; template <class Element, int ArraySize = 12> class DynArray { public: // operations performed on arrays DynArray ( int sz = ArraySize ) { size = sz; data = new Element[sz]; } ~DynArray() { delete [] data; } // return the value at location index Element getValue(int index) { if (index < 0) { cerr << "Index " << index << " is negative" << endl; exit(1); } else if (index >= size) { cerr << "Index " << index << " is out of range" << endl; cerr << "The current size of the array is " << size << endl; exit(1); } else return data[index]; } void setValue(int index, Element value) { if (index < 0) { cout << "Index " << index << " is negative" << endl; exit(1); } if (index >= size) { int old_size = size; Element *old_data = data; int i; while (index >= size) size *= 2; data = new Element[size]; for (i = 0; i < old_size; i++) data[i] = old_data[i]; delete [] old_data; } data[index] = value; } int getSize() { return size; } protected: int size; Element *data; };
template <class Type1, ..., class Typen> class Foo { Type1 y(Type2 arg1); }then y would be declared as
template <class Type1, ..., class Typen> Type1 Foo<Type1, ..., Typen>::y(Type2 arg1) { ... }In other words, all the pageantry with the template line must be repeated verbatim and you have to write the class name as if it were being instantiated with the generic types.
Here is a sample definition of the method bodies for Dynarray when they are placed in a separate .cpp file:
#include <iostream> #include <cstdlib> using namespace std; // note that you don't say "sz = ArraySize" in the constructor. // You only say that in the .h file template <class Element, int ArraySize> DynArray<Element, ArraySize>::DynArray( int sz ) { size = sz; data = new Element[sz]; } template <class Element, int ArraySize> DynArray<Element, ArraySize>::~DynArray() { delete [] data; } // set the value at location index template <class Element, int ArraySize> void DynArray<Element, ArraySize>::setValue(int index, Element value) { if (index < 0) { cout << "Index " << index << " is negative" << endl; exit(1); } if (index >= size) { int old_size = size; Element *old_data = data; int i; while (index >= size) size *= 2; data = new Element[size]; for (i = 0; i < old_size; i++) data[i] = old_data[i]; delete [] old_data; } data[index] = value; } // get the value at location index template <class Element, int ArraySize> Element DynArray<Element, ArraySize>::getValue(int index) { if (index < 0) { cerr << "Index " << index << " is negative" << endl; exit(1); } else if (index >= size) { cerr << "Index " << index << " is out of range" << endl; cerr << "The current size of the array is " << size << endl; exit(1); } else return data[index]; } template <class Element, int ArraySize> int DynArray<Element, ArraySize>::getSize() { return size; }
DynArray<int, 15> data; DynArray<int> *data = new DynArray<int>();
#include <iostream> #include "DynArray.h" using namespace std; int main() { DynArray<int, 15> data; int i; for (i = 0; i < 1000; i++) data.setValue(i, i+2); for (i = 0; i < 1000; i+= 50) cout << i << ": " << data.getValue(i) << endl; cout << "size = " << data.getSize() << endl; }