Download presentation
Presentation is loading. Please wait.
Published byBrandon Houston Modified over 9 years ago
1
Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes Standard Library Template (STL) Classes 5. Vector Class
2
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; } Function Template
3
Here’s a small function that you might write to find the maximum of two double numbers. int maximum(double a, double b) { if (a > b) return a; else return b; } Finding the Maximum of Two Doubles
4
Suppose your program uses 100,000,000 different data types, and you need a maximum function for each... int maximum(Knafn a, Knafn b) { if (a > b) return a; else return b; } One Hundred Million Functions... int maximum(Foo a, Foo b) { if (a > b) return a; else return b; } int maximum(Poo a, Poo b) { if (a > b) return a; else return b; } int maximum(Noo a, Noo b) { if (a > b) return a; else return b; } int maximum(Moo a, Moo b) { if (a > b) return a; else return b; } int maximum(Loo a, Loo b) { if (a > b) return a; else return b; } int maximum(Koo a, Koo b) { if (a > b) return a; else return b; } int maximum(Joo a, Joo b) { if (a > b) return a; else return b; } int maximum(Ioo a, Ioo b) { if (a > b) return a; else return b; } int maximum(Hoo a, Hoo b) { if (a > b) return a; else return b; } int maximum(Goo a, Goo 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(Coo a, Coo b) { if (a > b) return a; else return b; } int maximum(Boo a, Boo b) { if (a > b) return a; else return b; } int maximum(Knafn a, Knafn b) { if (a > b) return a; else return b; } int maximum(Foo a, Foo b) { if (a > b) return a; else return b; } int maximum(Poo a, Poo b) { if (a > b) return a; else return b; } int maximum(Noo a, Noo b) { if (a > b) return a; else return b; } int maximum(Moo a, Moo b) { if (a > b) return a; else return b; } int maximum(Loo a, Loo b) { if (a > b) return a; else return b; } int maximum(Koo a, Koo b) { if (a > b) return a; else return b; } int maximum(Joo a, Joo b) { if (a > b) return a; else return b; } int maximum(Ioo a, Ioo b) { if (a > b) return a; else return b; } int maximum(Hoo a, Hoo b) { if (a > b) return a; else return b; } int maximum(Goo a, Goo 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(Coo a, Coo b) { if (a > b) return a; else return b; } int maximum(Boo a, Boo b) { if (a > b) return a; else return b; } int maximum(Knafn a, Knafn b) { if (a > b) return a; else return b; } int maximum(Foo a, Foo b) { if (a > b) return a; else return b; } int maximum(Poo a, Poo b) { if (a > b) return a; else return b; } int maximum(Noo a, Noo b) { if (a > b) return a; else return b; } int maximum(Moo a, Moo b) { if (a > b) return a; else return b; } int maximum(Loo a, Loo b) { if (a > b) return a; else return b; } int maximum(Koo a, Koo b) { if (a > b) return a; else return b; } int maximum(Joo a, Joo b) { if (a > b) return a; else return b; } int maximum(Ioo a, Ioo b) { if (a > b) return a; else return b; } int maximum(Hoo a, Hoo b) { if (a > b) return a; else return b; } int maximum(Goo a, Goo 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(Coo a, Coo b) { if (a > b) return a; else return b; } int maximum(Boo a, Boo b) { if (a > b) return a; else return b; }
5
This template function can be used with many data types. template Item maximum(Item a, Item b) { if (a > b) return a; else return b; } A Template Function for Maximum
6
When you write a template function, you choose a data type for the function to depend upon... template Item maximum(Item a, Item b) { if (a > b) return a; else return b; } A Template Function for Maximum
7
A template prefix is also needed immediately before the function’s implementation: template Item maximum(Item a, Item b) { if (a > b) return a; else return b; } A Template Function for Maximum
8
Once a template function is defined, it may be used with any adequate data type in your program... template Item maximum(Item a, Item b) { if (a > b) return a; else return b; } Using a Template Function cout << maximum(1,2); cout << maximum(1.3, 0.9);...
9
Here’s another function that can be made more general by changing it to a template function: int array_max(int data[ ], int n) { int i; int answer; answer = data[0]; for (i = 1; i < n; i++) if (data[i] > answer) answer = data[i]; return answer; } Finding the Maximum Item in an Array
10
Here’s the template function: template Item array_max(Item data[ ], int n) { int i; Item answer; assert(n > 0); answer = data[0]; for (i = 1; i < n; i++) if (data[i] > answer) answer = data[i]; return answer; } Finding the Maximum Item in an Array
11
Bag Class Template Header #ifndef BAG_H #define BAG_H #define MAXIMUM_SIZE 100 template class bag { public: bag(); bag(int max); int getCount(); bool isFull(); bool isEmpty(); void remove (T item); void clear(); void display();... private: T *data; int count; int capacity; }; #include bag.template // Note #endif
12
Bag Class Template Implementation #include #include "bag.h" using namespace std; template bag ::bag(){ capacity = MAXIMUM_SIZE; data = new T [capacity; count = 0; } template bag ::bag(int max){ capacity = max; data = new T [capacity]; count = 0; }... template int bag ::getCount(){ return count; } template bool bag ::isFull(){ return count == capacity; } template bool bag ::isEmpty(){ return count == 0; }...
13
Client Program for Bag Template #include #include "bag.h" using namespace std; int main() { // Check with int bag iBag; int iList[] = {2, 4, 6}; int iCount = 3; for (int i = 0; i < iCount; i++){ iBag.insert(iList[i]); } cout << "Int bag contents: "; iBag.display(); cout << endl; iBag.remove(iList[0]); cout << "After removing: “; iBag.display(); cout << endl;... // check with string bag sBag; string sList[] = {"Anne", "Bill", "Cathy"}; int sCount = 3; for (int i = 0; i < sCount; i++){ sBag.insert(sList[i]); } cout << "String contents: "; sBag.display(); cout << endl; sBag.remove(sList[0]); cout << "After removing: “; sBag.display(); cout << endl; system("PAUSE"); return EXIT_SUCCESS; }
14
Some STL Class Templates vector Array list Doubly-linked list slist Singly-linked list queue FIFO (first-in, first-out) structure deque Array-like structure, with efficient insertion and removal at both ends set Set of unique elements stack LIFO (last in, first out) structure More templates
15
Your Turn Among the member functions in the vector class template provided by STL are: vector()—constructor void push_back(T item)—insert at back T operator[](int i)—returns item at position i (as in array) int size()—return number of items in vector Using the template, write a short program which inserts 3 string items in a vector and prints out its contents.
16
Solution #include using namespace std; int main(){ vector v; v.push_back("Alice"); v.push_back("Brad"); v.push_back("Chad"); for (int i = 0; i < 3; i++){ cout << v[i]<< endl; } system("PAUSE"); return EXIT_SUCCESS; }
17
A template function depends on an underlying data type. More complex template functions and template classes are discussed in Chapter 6. Summary
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.