Presentation is loading. Please wait.

Presentation is loading. Please wait.

Template Classes CMPS 2143.

Similar presentations


Presentation on theme: "Template Classes CMPS 2143."— Presentation transcript:

1 Template Classes CMPS 2143

2 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

3 Start with Template functions
int max (int x, int y) { if (x < y) return y; else return x; } double max (double x, double y) { 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);

4 Notes on previous example:
name T is a parameter (could use ItemType, Ttype..) T must 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.

5 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.

6 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

7 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 + (const NewClass & newClass1, const NewClass & newClass2)   private: T theData; }; #include “NewClass.cpp”

8 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);

9 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).

10 Methods template <class T> T NewClass::setData(const T & newValue ) { theData = T; //assumes = is defined for T } NewClass <T> NewClass <T>::operator + (const NewClass<T> & newClass1, const NewClass<T> & newClass2) NewClass <T> result; result = ??? return result;

11 Ex2. Instead non-member function looks like
NewClass nmFunction(const NewClass & newClass1, const NewClass & newClass2) { . . . } template <class T> NewClass <T> nmFunction (const NewClass<T> & newClass1, const NewClass<T> & newClass2) Ex2. Instead non-member function looks like

12 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); . :

13 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

14 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.

15 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.


Download ppt "Template Classes CMPS 2143."

Similar presentations


Ads by Google