Object Oriented Programming Using C CLASS 16 IDLOOPC1998 1 Objectives 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 class IDLOOPC1998 2 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 Pg 650 IDLOOPC1998 3 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 Pg 650 IDLOOPC1998 4 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 IDLOOPC1998 5 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 IDLOOPC1998 6 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 IDLOOPC1998 7 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 IDLOOPC1998 8 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 IDLOOPC1998 9 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 IDLOOPC1998 10 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 IDLOOPC1998 11 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 IDLOOPC1998 12 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 template keyword IDLOOPC1998 13 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 the Type Symbol IDLOOPC1998 14 Stack Operations push add a plate IDLOOPC1998 15 Stack Operations push pop add a plate remove a plate IDLOOPC1998 16 Stack Operations Push add a plate pop remove a plate is empty there are no plates IDLOOPC1998 17 Stack Operations Push add a plate pop remove a plate is empty there are no plates is full no more room in cabinet IDLOOPC1998 18 Stack as an array of integers top 1 size 8 IDLOOPC1998 19 Stack as an array of integers top 0 size 8 5 Push 5 IDLOOPC1998 20 Stack as an array of integers top 1 size 8 5 9 Push 5 Push 9 IDLOOPC1998 21 Stack as an array of integers top 2 size 8 5 9 3 Push 5 Push 9 Push 3 IDLOOPC1998 22 Stack as an array of integers top 1 size 8 5 9 Push 5 Push 9 Push 3 Pop IDLOOPC1998 23 Stack as an array of integers top 0 size 8 5 Push 5 Push 9 Push 3 Pop Pop IDLOOPC1998 24 Templates Constructor Constructor with default size 10 template class T Stack T Stack int s size s 0 s 1000 s 10 top 1 stackPtr new T size Pg 654 IDLOOPC1998 25 Templates Push Push an element onto the Stack return 1 if successful 0 otherwise template class T bool Stack T push const T pushValue if isFull stackPtr top pushValue return true return false Pg 654 IDLOOPC1998 26 Templates Pop Pop an element off the Stack templat class T bool Stack T pop T popValue if isEmpty popValue stackPtr top return true return false endif Pg 654 IDLOOPC1998 27 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 IDLOOPC1998 28 Templates Class Name main Stack double doubleStack 5 double f 1 1 while doubleStack push f cout f f 1 1 while doubleStack pop f cout f IDLOOPC1998 29 Templates Class Name main Object Type Stack double doubleStack 5 double f 1 1 while doubleStack push f cout f f 1 1 while doubleStack pop f cout f IDLOOPC1998 30 Templates Class Name main Object Type Stack double doubleStack 5 double f 1 1 while doubleStack push f Desired cout f f 1 1 while doubleStack pop f cout f Stack Size IDLOOPC1998 31 Q A IDLOOPC1998 32 IDLOOPC1998 33
View Full Document
Unlocking...