Download presentation
Presentation is loading. Please wait.
1
Really reusable software
4/25/2019 Templates: Really reusable software tmplts
2
Finding the Maximum of Two Integers
Here’s a small function that you might write to find the maximum of two integers int maximum(int a, int b) { if (a > b) return a; else return b; }
3
Finding the Maximum of Two Doubles
Here’s a small function that you might write to find the maximum of two double numbers. double maximum(double a, double b) { if (a > b) return a; else return b; }
4
Finding the Maximum of Two Knafns
Here’s a small function that you might write to find the maximum of two Knafns. Knafn maximum(Knafn a, Knafn b) { if (a > b) return a; else return b; }
5
One Hundred Million Functions ...
Suppose your program uses 100,000,000 different data types, and you need a maximum function for each... int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; }
6
A Template Function for Maximum
This template function can be used with many data types. template <class Item> Item maximum(Item a, Item b) { if (a > b) return a; else return b; }
7
A Template Function for Maximum
When you write a template function, you choose a data type for the function to depend upon... template <class Item> Item maximum(Item a, Item b) { if (a > b) return a; else return b; }
8
A Template Function for Maximum
A template prefix is also needed immediately before the function’s implementation: template <class Item> Item maximum(Item a, Item b) { if (a > b) return a; else return b; }
9
Using a Template Function
Once a template function is defined, it may be used with any adequate data type in your program... template <class Item> Item maximum(Item a, Item b) { if (a > b) return a; else return b; } cout << maximum(1,2); cout << maximum(1.3, 0.9); ...
10
Finding the Maximum Item in an Array
Here’s another function that can be made more general by changing it to a template function: int array_max(int data[ ], size_t n) { size_t i; int answer; assert(n > 0); answer = data[0]; for (i = 1; i < n; i++) if (data[i] > answer) answer = data[i]; return answer; }
11
Finding the Maximum Item in an Array
Here’s the template function: template <class Item> Item array_max(Item data[ ], size_t n) { size_t i; Item answer; assert(n > 0); answer = data[0]; for (i = 1; i < n; i++) if (data[i] > answer) answer = data[i]; return answer; }
12
Parameter matching for template functions
The second template function example required two parameters: an array of Items (the template type) and a numeric type (size_t in this case) representing array size The second parameter presents us with a potential problem: arguments to template functions must exactly match the data type(s) of formal parameters
13
Parameter matching for template functions
When a template function is activated, the compiler selects underlying data type so that the type of the formal parameter exactly matches the actual parameter No automatic casting takes place The requirement for exact matching applies to all parameters of a template function
14
Solution to matching problem
Can generalize both parameters in the array_max function as template parameters: template <class Item, class SizeType> Item array_max (const Item data[], SizeType n) etc. Function can now be instantiated with any data types appropriate to content & size of array
15
Placing template functions in a toolkit
Notable requirement: implementations of template functions must appear in the header file Since this violates principle of information hiding, can employ a sneaky workaround: place include directive for implementation file at end of header instead of placing include directive for header at start of implementation
16
Template Classes Can implement template classes for reasons similar to template function implementation Precede class definition with template prefix (same as used for template functions) Example: template <class Item> class Bag { public: // can leave out typedef statement now ...
17
Template class functions
Member functions that manipulate a template object are dependent on the template type Within the class definition, the compiler is aware of this dependency -- so function prototypes (which are inside the class definition) look the same as in non-template classes
18
Template class functions
Outside the class definition, follow these rules: place template prefix before each function prototype (for non-member functions) and implementation (for all functions) each use of class name (ex. Bag) is changed to template class name (Bag<Item>) can now use name of underlying type without scope operator (Item instead of Bag::Item)
19
Template class function examples
Function operator + is a non-member function that manipulates Bags Original prototype: Bag operator + (const Bag& b1, const Bag& b2); New prototype: template <class Item> Bag<Item> operator + (const Bag<Item>& b1, const Bag<Item>& b2);
20
Template class function examples
Default constructor implementation Original version: Bag::Bag ( ) New version: template <class Item> Bag<Item>::Bag() Note that the template syntax applies only to the class name, not the function name
21
Template class function examples
Final example: grab function Original implementation: Bag::Item Bag::grab( )const Template version: template <class Item> Item Bag<Item>::grab( ) const
22
Using template classes
Specify underlying data type of the template object at declaration: Bag<char> cbag; Bag<int> ibag; Member functions are used as usual: cbag.insert(‘c’); int num = ibag.grab();
23
Templates & the linked list toolkit
Original version: struct Node { typedef char Item; Item data; Node* link; }; Template version: template <class Item> struct Node { Item data; Node* link; };
24
Function modifications in linked list toolkit
Precede each function definition with template prefix Change each Node reference to Node<Item> Change references to Node::Item to just Item Allow for exact parameter matching
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.