templates 1. template functions 2. template class 3. bag class template 4. standard library template...

17
Templates 1. 1. Template Functions Template Functions 2. 2. Template Class Template Class 3. 3. Bag Class Template Bag Class Template 4. Standard Library Template (ST L) Classes 5. 5. Vector Class Vector Class

Upload: brandon-houston

Post on 18-Jan-2018

220 views

Category:

Documents


0 download

DESCRIPTION

 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

TRANSCRIPT

Page 1: Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes

Templates

1.1. Template FunctionsTemplate Functions2.2. Template ClassTemplate Class3.3. Bag Class TemplateBag Class Template4. Standard Library Template (STL) Classes5.5. Vector ClassVector Class

Page 2: Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes

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

Page 3: Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes

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

Page 4: Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes

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;}

Page 5: Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes

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;}

A Template Function for Maximum

Page 6: Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes

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;}

A Template Function for Maximum

Page 7: Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes

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;}

A Template Function for Maximum

Page 8: Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes

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;}

Using a Template Function

cout << maximum(1,2);cout << maximum(1.3, 0.9);...

Page 9: Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes

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

Page 10: Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes

Here’s the template function:

template <class Item>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

Page 11: Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes

Bag Class Template Header

#ifndef BAG_H#define BAG_H

#define MAXIMUM_SIZE 100

template <class T>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

Page 12: Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes

Bag Class Template Implementation#include <iostream> #include "bag.h"using namespace std;

template<class T>bag<T>::bag(){ capacity = MAXIMUM_SIZE; data = new T [capacity; count = 0;}

template<class T>bag<T>::bag(int max){ capacity = max; data = new T [capacity]; count = 0;} . . .

. . .template<class T>int bag<T>::getCount(){ return count;}template<class T>bool bag<T>::isFull(){ return count == capacity;} template<class T>bool bag<T>::isEmpty(){ return count == 0;}. . .

Page 13: Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes

Client Program for Bag Template#include <iostream>#include <string>#include "bag.h"using namespace std;int main() { // Check with int bag<int> 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<string> 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;}

Page 14: Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes

Some STL Class Templates

vector Arraylist Doubly-linked listslist Singly-linked listqueue FIFO (first-in, first-out) structuredeque Array-like structure, with efficient insertion

and removal at both endsset Set of unique elementsstack LIFO (last in, first out) structure

More templates

Page 15: Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes

Your Turn

Among the member functions in the Among the member functions in the vector vector class class template provided by STL are:template provided by STL are: vector()—constructorvector()—constructor void push_back(T item)—insert at backvoid push_back(T item)—insert at back T operator[](int i)—returns item at position i (as in array)T operator[](int i)—returns item at position i (as in array) int size()—return number of items in vectorint size()—return number of items in vector

Using the template, write a short program which Using the template, write a short program which inserts 3 string items in a vector and prints out its inserts 3 string items in a vector and prints out its contents.contents.

Page 16: Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes

Solution#include <iostream>#include <string>#include <vector>using namespace std;

int main(){ vector<string> 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;}

Page 17: Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes

A template function depends on an underlying data type.

More complex template functions and template classes are discussed in Chapter 6.

Summary