Download presentation
Presentation is loading. Please wait.
Published byAdam Owen Modified over 9 years ago
1
1
2
2 2
3
3 3
4
C:\Temp\Templates 4
5
5
6
Use This Main Program 6
7
Copy/Paste Main # include "Utilities.hpp" # include "Student.hpp" void MySwap (int Value1, int Value2); int main(int argc, char * argv[]) { int A = 5, B = 10; cout << "A = " << A << " B = " << B << endl; //MySwap (A, B); cout << "A = " << A << " B = " << B << endl; getchar(); return(0); } 7
8
void MySwap (int Value1, int Value2) { int Temp; } Write Code For MySwap Do Not Look Ahead In The Slides! 8
9
void MySwap (int Value1, int Value2) { int Temp; Temp = Value1; Value1 = Value2; Value2 = Temp; } How Many Of You Have Something Like The Following: Only To Find That It Does Not Work? Pass By Value? - BAD! 9
10
void MySwap (int & Value1, int & Value2) { int Temp; Temp = Value1; Value1 = Value2; Value2 = Temp; } Better - Pass By Reference 10
11
11
12
Change The Main Program 12
13
Copy/Paste Main int main(int argc, char * argv[]) { int A = 5, B = 10; cout << "A = " << A << " B = " << B << endl; MySwap (A, B); cout << "A = " << A << " B = " << B << endl; float AA = 5.5, BB = 7.7; cout << "AA = " << AA << " BB = " << BB << endl; MySwap (AA, BB); cout << "AA = " << AA << " BB = " << BB << endl; getchar(); return(0); } 13
14
No Signature For (float, float) 14
15
Overload MySwap! 15
16
int bool float short int double long int Student Part Employee Auto CD Stamp Coin Element Lumber Squadron Unit Officer Desk Flower Shrub Tree China Equipment Soldier Infinite # Of Objects 16
17
17
18
Templates 18
19
template Template Functions Begin With Something Like: template Or Maybe Something Like: template Or Maybe Something Like: 19
20
template void MySwap ( T & Value1, T & Value2); template void MySwap (T & Value1, T & Value2) { } The class Variable Must Appear In The Parameter List 20
21
21
22
Copy/Paste Main int main(int argc, char * argv[]) { int A = 5, B = 10; cout << "A = " << A << " B = " << B << endl; MySwap (A, B); cout << "A = " << A << " B = " << B << endl; float AA = 5.5, BB = 7.7; cout << "AA = " << AA << " BB = " << BB << endl; MySwap (AA, BB); cout << "AA = " << AA << " BB = " << BB << endl; getchar(); return(0); } 22
23
Template MySwap Works For in & float 23
24
24
25
Copy/Paste - Add This To Main char C = 'X', D = '?'; cout << "C = " << C << " D = " << D << endl; MySwap (C, D); cout << "C = " << C << " D = " << D << endl; getchar(); return(0); } 25
26
26
27
Copy/Paste - Add This To Main bool E = true, F = false; cout << "E = " << E << " F = " << F << endl; MySwap (E, F); cout << "E = " << E << " F = " << F << endl; getchar(); return(0); } 27
28
28
29
Copy/Paste - Add This To Main Student Student1 ("Pete", 2222, MALE), Student2 ("Sandra", 3333, FEMALE); Student1.Display("Student1"); Student2.Display("Student2"); MySwap (Student1, Student2); Student1.Display("Student1"); Student2.Display("Student2"); getchar(); return(0); } 29
30
Will Work For All Classes (As Long As Operator = Has Been Overloaded Properly) 30
31
31
32
C++ Also Supports Generics, But Templates Enable Us To Easily Create Functions That Can Be Used With Thousands Of Data Types. template void BubbleSort(T Array[], long ActNo); template long Search(InfoType Data[], long ActNo, InfoType SoughtInfo); The class Variable Must Appear In The Parameter List 32
33
33
34
34 Templates Are Not Really Functions. They are more of a design or pattern for what shall become a function if evoked. The Compiler Shall Generate the Necessary Variations of this Function During Run Time. Template Functions Must be placed in.hpp files! Template Functions Begin With Something Like:
35
Template - Multiple Parameters 35 template class Binary Tree {... }; An important goal of both software engineering and object-oriented programming is reuse! Function Templates facilitate code reuse!
36
36
37
template class ListType { public: private: }; Write The Class Definition For A Templated Class, Called ListType Whose Template Argument Is InfoType.
38
template class ListType { public: private: }; Write The Declaration To Create An Integer Type List, Called IntNos, of ListType.
39
template class ListType { public: private: InfoType Info[10]; // Container To Store Data long ActNo; // Actual No Items In Container Add An Array To The Private Data of ListType. ListType IntNos; ListType Chars; ListType Class;
40
template class ListType { public: private: InfoType * Info; // Container To Store Data long ActNo; // Actual No Items In Container Add An Array To The Private Data of ListType. ListType IntNos; ListType Chars; ListType Class;
41
template class ListType { public: private: InfoType Info[10]; // Container To Store Data long Max, // Capacity Of The Container ActNo; // Actual No Items In Container }; Write The Constructor ListType (long int NewMax = 10)
42
template class ListType { public: private: InfoType Info[10]; // Container To Store Data long Max, // Capacity Of The Container ActNo; // Actual No Items In Container }; Write The Code For The Constructor ListType (long int NewMax = 10) ListType (long int NewMax = 10);
43
template ListType :: ListType (long int NewMax) { puts("Evoking Constructor ListType(NeMax)"); printf("NewMax = %ld\n\n", NewMax); }; ListType (long int NewMax = 10) Update main
44
template ListType :: ListType (long int NewMax) { ActNo = 0; Info = new InfoType [NewMax + 1]; if (Info == NULL) { puts("Not Enough Memory!"); Max = 0; } else Max = NewMax; }; ListType (long int NewMax = 10) Allocate Max + 1 Memory - Set Max & ActNo
45
template ListType :: ListType (long int NewMax) { ActNo = 0; Info = new InfoType [NewMax + 1]; if (Info == NULL) { puts("Not Enough Memory!"); Max = 0; } else Max = NewMax; }; "Memory Leak" You Have Created A Memory leak If You Have Executed The Program One TIme!?
46
template class ListType { public: ListType (long int NewMax = 10); ~ListType(void); private: InfoType Info[10]; long Max, ActNo; }; Write The Destructor ~ ListType (void)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.