Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Joe Meehean.  Suppose we wanted a class to store a pair of ints  Access the first using first()  and the second using second()  Lets call it LCPair.

Similar presentations


Presentation on theme: "1 Joe Meehean.  Suppose we wanted a class to store a pair of ints  Access the first using first()  and the second using second()  Lets call it LCPair."— Presentation transcript:

1 1 Joe Meehean

2  Suppose we wanted a class to store a pair of ints  Access the first using first()  and the second using second()  Lets call it LCPair 2

3 class LCPair{ public: LCPair(int a,int b) { this->first = a; this->second = b; } int& first() { return this->first_; } int& second() { return this->second_; } private: int first_; int second_; }; 3

4  The type of the pair items doesn’t matter class doesn’t do anything with them  Suppose we wanted to make LC Pair type independent  How do we do it?  Do we have to write a different pair class for every data type? 4

5  The type of the pair items doesn’t matter class doesn’t do anything with them  Suppose we wanted to make LC Pair type independent  How do we do it?  Do we have to write a different pair class for every data type? 5

6 template class LCPair{ public: LCPair(const Obj1& o1,const Obj2& o2) { this->first_ = o1; this->second_ = o2; } Obj1& first() { return this->first_; } Obj2& second() { return this->second_; } private: Obj1 first_; Obj2 second_; }; 6

7  Need to declare what the generic/template types are template generic type must follow the typename keyword e.g., template can define multiple generic types using a list template  Then use the declared generic type like any other type 7

8  When initializing a templated class must declare what types you want it to use refer to class using ClassName e.g., LCPair intPair;  Then use it like any other class 8

9  Using a template class 9 int main(){ LCPair intPair; LCPair boolPair; LCPair mixedPair; mixedPair.first() = 7; mixedPair.second() = 4.5; int i = mixedPair.first(); float f = mixedPair.second(); }

10  A class template is not a class it is a pattern for what will become a class when you declare a class template variable e.g., LCPair intPair; the compiler makes a whole new class file replaces all instances of template types with declared types e.g., Obj1 replaced with int does this for each different declaration LCPair & LCPair are classes 10

11  What if we want to find the largest element in an array?  If the element overrides the > operator, the data type of the elements is irrelevant  Use Function Templates to solve this problem uses similar syntax to class templates 11

12 12 /** * Comparable must provide > operator */ template Comparable& findMax(Comparable* a, int size){ int maxIndex = 0; for(size_t i = 1; i < size; i++){ if( a[i] > a[maxIndex] ){ maxIndex = i; } return a[maxIndex]; }

13  Compiler replaces all template types with actual types  If you use methods, constructors or operators on a template type e.g., a[i] > a[maxIndex] produces compile error if not defined on concrete types 13

14  When using template types always specify required methods & constructors in comments e.g., /* Comparable must provide > operator */ assume the template type is a class e.g., Obj1 obj = 0; // NO, may not compile e.g., Obj1 obj; // YES 14

15  We always declare our classes in a.h file  And define their methods in a.cpp file  Many compilers cannot do this with templates including Visual Studio and g++  Template classes must be completely defined in the.h file 15

16 16

17 17 /** * Comparable must provide > operator */ template Comparable& findMax(Comparable* a, int size){ int maxIndex = 0; for(size_t i = 1; i < size; i++){ if( a[i] > a[maxIndex] ){ maxIndex = i; } return a[maxIndex]; }

18  What if we want findMax to work for more complex objects e.g., Student object has Name, GPA, GradDate what should > compare them on? what if we want to be able to find max Name OR max GPA all with one function? 18

19  What if we want findMax to work for more complex objects e.g., Student object has Name, GPA, GradDate what should > compare them on? what if we want to be able to find max Name OR max GPA all with one function? 19

20  Objects that act like functions may have no data require only one public method  Can make lots of different functors for same class  Using templates we can compare any element using any functor that takes that element as a parameter 20

21 21 // compare students by name class StudentNameCmp{ public: bool isGreaterThan( const Student& lhs, const Student& rhs){ return lhs.getName() > rhs.getName(); } }; // compare students by gpa class StudentGPACmp{ public: bool isGreaterThan( const Student& lhs, const Student& rhs){ return lhs.getGPA() > rhs.getGPA(); } };

22 22 /** * Comparator must provide * isGreaterThan(Object, Object) */ template < typename Object, typename Comparator> Object& findMax( Object* a, int size, Comparator cmp){ int maxIndex = 0; for(size_t i = 1; i < size; i++){ if( cmp.isGreaterThan(a[i],a[maxIndex]) ){ maxIndex = i; } return a[maxIndex]; }

23 23 int main(){ Student students[15]; // fill in array... // find the largest name Student maxName = findMax(students, 15, StudentNameCmp() ); // find the student with best GPA Student bestGPA = findMax(students, 15, StudentGPACmp() ); }

24  Making functors look like functions  Instead of defining isGreaterThan method  Override the operator() known as the function call operator calling it looks just like calling a non-member function 24

25 25 // compare students by name class StudentNameCmp{ public: bool operator()( const Student& lhs, const Student& rhs){ return lhs.getName() > rhs.getName(); } }; // compare students by gpa class StudentGPACmp{ public: bool operator()( const Student& lhs, const Student& rhs){ return lhs.getGPA() > rhs.getGPA(); } };

26 26 /** * Comparator must provide * function operator */ template <typename Object, typename Comparator> Object& findMax( Object* a, int size, Comparator cmp){ int maxIndex = 0; for(size_t i = 1; i < size; i++){ if( cmp.isGreaterThan(a[i],a[maxIndex]) ){ if( cmp(a[i],a[maxIndex]) ){ maxIndex = i; } return a[maxIndex]; }

27 27


Download ppt "1 Joe Meehean.  Suppose we wanted a class to store a pair of ints  Access the first using first()  and the second using second()  Lets call it LCPair."

Similar presentations


Ads by Google