151 likes | 256 Views
Template Classes. CMPS 2143. Another form of polymorphism. Generics (or templates) in C++ provide a way to parameterize a function or a class by use of a type A name is defined as a type parameter in the case of a class, eg . List <int> list;
E N D
Template Classes CMPS 2143
Another form of polymorphism • Generics (or templates) in C++ provide a way to parameterize a function or a class by use of a type • A name is defined as a type parameter in the case of a class, eg. List <int> list; • Then that type is used within the class definition in place of the parameter • Generics in C++, Eiffel, ML, Haskell, Ada, and Java 7
Start with Template functions int max (int x, int y) { if (x < y) return y; else return x; } double max (double x, double y) { if (x < y) return y; else return x; } template <class T> T max (T x, T y) { if (x < y) return y; else return x; } int a = max (3, 27); double d = max(3.14, 2.75); Fraction f1 (1, 2); Fraction f2 (3, 4); Fraction f3 = max (f1, f2);
Notes on previous example: • name T is a parameter (could use ItemType, Ttype..) • Tmust be a type (can be primitive type, despite keyword class) • Limits to the type – must have < defined for that type. • so in Fraction < must be overloaded.
Template Classes • Template functions useful, but more common to see templates applied to classes (especially container classes) • A class that depends on an underlying data type(s) likewise can be implemented as a template class. • We can have a ‘NewClass’ of ints, chars, strings, whatever.
Syntax Declaring and defining a template class requires three things. • The template class declaration • Methods (member functions) for the template class • Make the implementation visible
The template class declaration template <class T> class NewClass { public: NewClass (); NewClass (T initialData); NewClass (NewClass other); void setData(const T & newValue); T getData () const; void resize (int newSize); NewClass operator + (constNewClass & newClass1, constNewClass & newClass2) private: T theData; }; #include “NewClass.cpp”
Methods for the template class • All of the methods that manipulate newClass objects are now dependent on the type T. • Within the template class declaration, the compiler already knows about this dependency. So declaration of methods written like they have always been. Ex1. void setData (const T & newData);
Outside some rules must be followed to tell the compiler about the dependency. • the template prefix template <class T> is placed immediately before each function heading that uses the class. • Each use of the class name (such as NewClass) is changed to the template class name • such as NewClass<T> • Each use of complete underlying type name (such as NewClass::T) may be shortened to just the type name (such as T).
Methods template <class T> T NewClass::setData(const T & newValue) { theData = T; //assumes = is defined for T } template <class T> NewClass<T> NewClass <T>::operator + (constNewClass<T> & newClass1, constNewClass<T> & newClass2) { NewClass <T> result; result = ??? return result; }
Ex2. Insteadnon-member function looks like NewClassnmFunction(constNewClass & newClass1, constNewClass & newClass2) { . . . } template <class T> NewClass<T> nmFunction (constNewClass<T> & newClass1, constNewClass<T> & newClass2) { … }
NOTE: The name NewClass is changed to NewClass <T> only when it is used as a class name void main() { NewClass<int> myFirst; NewClass<int> mySecond(2); NewClass <int> myThird (33); NewClass <double> myFourth (4.5); myFirst.setData (5); cout << myFourth.getData() << endl; myFirst = nmFunction (mySecond, myThird); . :
Make the implementation visible • Annoying requirement: The implementation of the methods must be present in the header file. • Trick: Keep the implementations in a separate implementation file, but place an “include” directive at the bottom of the header file to pick up those implementations. • So we have files NewClass.h and NewClass.cpp
Parameter Matching for Methods of Template Classes • In template non-member functions, we are careful to help the compiler by providing a template parameter for each of the function’s parameters. HOWEVER, this help is not needed for member functions of a template class.
Homework • Write a template function called swap() that interchanges the values of the two arguments sent to it. • Then write a main() function to test the function with several types. • Due: Tuesday 10 points • Extra credit +5 point if you turn in coded program with output.