1IDLOOPC1998.
Object-Oriented Programming
Using C++
CLASS 16
2IDLOOPC1998.
� Create a function that supports parameters of a later-determined type
� Use class templates to create a group of related types
� Understand the difference between a class template and a templated classOb
ject
ives
3IDLOOPC1998.
Template Functionstemplate <class T>void printArray(const T *array, const int count){
for (int i = 0; i < count; i++)cout << array[i] << “ ’;
cout << endl; }
Pg. 650
4IDLOOPC1998.
Template Functionstemplate <class T>void printArray(const T *array, const int count){
for (int i = 0; i < count; i++)cout << array[i] << “ ’;
cout << endl;}
Pg. 650
5IDLOOPC1998.
Template Functions
template <class T>void printArray(const T *array, const int count){
for (int i = 0; i < count; i++)cout << array[i] << “ ’;
cout << endl;}main( ){ int a[5] = { 1, 2, 3, 4, 5};
printArray(a, 5);
Pgs. 650 and 651
6IDLOOPC1998.
template <class T>void printArray(const T *array, const int count){
for (int i = 0; i < count; i++)cout << array[i] << “ ’;
cout << endl;}main( ){ int a[5] = { 1, 2, 3, 4, 5};
printArray(a, 5);
Template Functions
Pgs. 650 and 651
7IDLOOPC1998.
Template Functions
template <class T>void printArray(const T *array, const int count){
for (int i = 0; i < count; i++)cout << array[i] << “ ’;
cout << endl;}main( ){ int a[5] = { 1, 2, 3, 4, 5};
printArray(a, 5);char c[6] = “HELLO”;printArray (c, 6); Pg. 651
8IDLOOPC1998.
template <class T>void printArray(const T *array, const int count){
for (int i = 0; i < count; i++)cout << array[i] << “ ’;
cout << endl;}main( ){ int a[5] = { 1, 2, 3, 4, 5};
printArray(a, 5);char c[6] = “HELLO”;printArray (c, 6); Pg. 651
Template Functions
9IDLOOPC1998.
Function Template Supporting 2 Types
template < class T1, class T2>int sum (T1 *array, T2 value, int
num-elements){ T2 sum = 0;
for(int i = 0; i < num_elements; i++)sum + = array [i]; }
main( ){ int values[ ] = {1, 2, 3, 4};
long int total = 0;sum(values, total, 4);cout << “total is” << total;
}
Pg. 652
10IDLOOPC1998.
Function Template Supporting 2 Types
template < class T1, class T2>int sum (T1 *array, T2 value, int
num-elements){ T2 sum = 0;
for(int i = 0; i < num_elements; i++)sum + = array [i]; }
main( ){ int values[ ] = {1, 2, 3, 4};
long int total = 0;sum(values, total, 4);cout << “total is” << total;
}
Pg. 652
11IDLOOPC1998.
Function Template Supporting 2 Types
template < class T1, class T2>int sum (T1 *array, T2 value, int
num-elements){ T2 sum = 0;
for(int i = 0; i < num_elements; i++)sum + = array [i]; }
main( ){ int values[ ] = {1, 2, 3, 4};
long int total = 0;sum(values, total, 4);cout << “total is” << total;
}
Pg. 652
12IDLOOPC1998.
Class Templates-Parameterized Types#ifndef TSTACK1_H#define TSTACK_H#include <iostream.h>template< class T >class Stack {public:
Stack( int = 10 );~Stack( ) { delete [ ] stackPtr; }bool push( const T& );bool pop( T& );
private:int size;int top;T *stackPtr;bool isEmpty( ) const { return top == -1; }bool isFull( ) const { return top == size - 1; }
Pgs. 653 and 654
13IDLOOPC1998.
Class Templates-Parameterized Types#ifndef TSTACK1_H#define TSTACK_H#include <iostream.h>template< class T >class Stack {public:
Stack( int = 10 );~Stack( ) { delete [ ] stackPtr; }bool push( const T& );bool pop( T& );
private:int size;int top;T *stackPtr;bool isEmpty( ) const { return top == -1; }bool isFull( ) const { return top == size - 1; }
Pgs. 653 and 654
type symbol
templatekeyword
14IDLOOPC1998.
Class Templates-Parameterized Types#ifndef TSTACK1_H#define TSTACK_H#include <iostream.h>template< class T >class Stack {public:
Stack( int = 10 );~Stack( ) { delete [ ] stackPtr; }bool push( const T& );bool pop( T& );
private:int size;int top;T *stackPtr;bool isEmpty( ) const { return top == -1; }bool isFull( ) const { return top == size - 1; }
Pgs. 653 and 654
Using theType Symbol
15IDLOOPC1998.
Stack Operations
push - add a plate
16IDLOOPC1998.
Stack Operations
push - add a platepop - remove a plate
17IDLOOPC1998.
Stack Operations
Push - add a platepop - remove a plateis empty - there are no plates
18IDLOOPC1998.
Stack Operations
Push - add a platepop - remove a plateis empty - there are no platesis full - no more room in cabinet
19IDLOOPC1998.
Stack as an array of integerstop -1size 8
20IDLOOPC1998.
Stack as an array of integers
Push (5)
5
top 0size 8
21IDLOOPC1998.
Stack as an array of integers
Push (5)Push (9)
5 9
top 1size 8
22IDLOOPC1998.
Stack as an array of integers
Push (5)Push (9)Push (3)
5 39
top 2size 8
23IDLOOPC1998.
Stack as an array of integers
Push (5)Push (9)Push (3)Pop
5 9
top 1size 8
24IDLOOPC1998.
Stack as an array of integers
Push (5)Push (9)Push (3)PopPop
5
top 0size 8
25IDLOOPC1998.
Templates - Constructor
// Constructor with default size 10template<class T>Stack<T>::Stack(int s){size = s > 0 && s < 1000 ? s: 10;top = -1;stackPtr = new T[size];
}
Pg. 654
26IDLOOPC1998.
Templates - Push// Push an element onto the Stack// return 1 if successful, 0 otherwisetemplate<class T>bool Stack<T>::push(const T &pushValue){
if (!isFull( ) ) {stackPtr[++top] = pushValue;return true;
}return false;
}
Pg. 654
27IDLOOPC1998.
Templates - Pop// Pop an element off the Stacktemplat<class T>bool Stack<T>::pop(T &popValue){if (!isEmpty( ) ) {
popValue = stackPtr[top- -];return true;
}return false;
}#endif
Pg. 654
28IDLOOPC1998.
Templates
main ( ){Stack<double>doubleStack (5);double f = 1.1;while (doubleStack.push (f))
{ cout << f;f + = 1.1; }
while (doubleStack.pop (f)cout << f;
}
29IDLOOPC1998.
Templates
main ( ){Stack<double>doubleStack (5);double f = 1.1;while (doubleStack.push (f))
{ cout << f;f + = 1.1; }
while (doubleStack.pop (f)cout << f;
}
Class Name
30IDLOOPC1998.
Templates
main ( ){Stack<double>doubleStack (5);double f = 1.1;while (doubleStack.push (f))
{ cout << f;f + = 1.1; }
while (doubleStack.pop (f)cout << f;
}
Class NameObject Type
31IDLOOPC1998.
Templates
main ( ){Stack<double>doubleStack (5);double f = 1.1;while (doubleStack, push (f))
{ cout << f;f + = 1.1; }
while (doubleStack.pop (f)cout << f;
}
Class NameObject Type
Desired Stack Size
32IDLOOPC1998.
Q & A
33IDLOOPC1998.