1 2 2 3 3 4 C:\Temp\Templates 5 6 Use This Main Program - - PDF document

1 2 2 3 3
SMART_READER_LITE
LIVE PREVIEW

1 2 2 3 3 4 C:\Temp\Templates 5 6 Use This Main Program - - PDF document

1 2 2 3 3 4 C:\Temp\Templates 5 6 Use This Main Program 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 =


slide-1
SLIDE 1

1

slide-2
SLIDE 2

2 2

slide-3
SLIDE 3

3 3

slide-4
SLIDE 4

C:\Temp\Templates

4

slide-5
SLIDE 5

5

slide-6
SLIDE 6

Use This Main Program

6

slide-7
SLIDE 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

slide-8
SLIDE 8

void MySwap (int Value1, int Value2) { int Temp; }

Write Code For MySwap

Do Not Look Ahead In The Slides!

8

slide-9
SLIDE 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

slide-10
SLIDE 10

void MySwap (int & Value1, int & Value2) { int Temp; Temp = Value1; Value1 = Value2; Value2 = Temp; }

Better - Pass By Reference

10

slide-11
SLIDE 11

11 11

slide-12
SLIDE 12

Change The Main Program

12

slide-13
SLIDE 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

slide-14
SLIDE 14

No Signature For (float, float)

14

slide-15
SLIDE 15

Overload MySwap!

15

slide-16
SLIDE 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

slide-17
SLIDE 17

17 17

slide-18
SLIDE 18

Templates

18

slide-19
SLIDE 19

template <class T>

Template Functions Begin With Something Like:

template <class InfoType>

Or Maybe Something Like:

template <class I>

Or Maybe Something Like:

19

slide-20
SLIDE 20

template <class T> void MySwap (T & Value1, T & Value2); template <class T> void MySwap (T & Value1, T & Value2) { } The class Variable Must Appear In The Parameter List

20

slide-21
SLIDE 21

21 21

slide-22
SLIDE 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

slide-23
SLIDE 23

Template MySwap Works For in & float

23

slide-24
SLIDE 24

24 24

slide-25
SLIDE 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

slide-26
SLIDE 26

26 26

slide-27
SLIDE 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

slide-28
SLIDE 28

28 28

slide-29
SLIDE 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

slide-30
SLIDE 30

Will Work For All Classes

(As Long As Operator = Has Been Overloaded Properly)

30

slide-31
SLIDE 31

31 31

slide-32
SLIDE 32

C++ Also Supports Generics, But Templates Enable Us To Easily Create Functions That Can Be Used With Thousands Of Data Types.

template <class T> void BubbleSort(T Array[], long ActNo); template <class InfoType> long Search(InfoType Data[], long ActNo, InfoType SoughtInfo);

The class Variable Must Appear In The Parameter List

32

slide-33
SLIDE 33

33 33

slide-34
SLIDE 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:

slide-35
SLIDE 35

Template - Multiple Parameters

35

template <class InfoType, class HeaderNodeType> class Binary Tree { ... };

An important goal of both software engineering and object-oriented programming is reuse! Function Templates facilitate code reuse!

slide-36
SLIDE 36

36 36

slide-37
SLIDE 37

template <class InfoType> class ListType { public: private: };

Write The Class Definition For A Templated Class, Called ListType Whose Template Argument Is

InfoType.

slide-38
SLIDE 38

template <class InfoType> class ListType { public: private: };

Write The Declaration To Create An Integer Type List , Called IntNos, of ListType.

slide-39
SLIDE 39

template <class InfoType> 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 <int> IntNos; ListType <char> Chars; ListType <Student> Class;

slide-40
SLIDE 40

template <class InfoType> 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 <int> IntNos; ListType <char> Chars; ListType <Student> Class;

slide-41
SLIDE 41

template <class InfoType> 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)

slide-42
SLIDE 42

template <class InfoType> 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);

slide-43
SLIDE 43

template <class InfoType> ListType <InfoType> :: ListType (long int NewMax) { puts("Evoking Constructor ListType(NeMax)"); printf("NewMax = %ld\n\n", NewMax); };

ListType (long int NewMax = 10) Update main

slide-44
SLIDE 44

template <class InfoType> ListType <InfoType> :: ListType (long int NewMax) { ActNo = 0; Info = new InfoType [NewMax]; if (Info == NULL) { puts("Not Enough Memory!"); Max = 0; } else Max = NewMax ‐ 1; };

ListType (long int NewMax = 10)

Allocate Memory - Set Max & ActNo

slide-45
SLIDE 45

template <class InfoType> ListType <InfoType> :: ListType (long int NewMax) { ActNo = 0; Info = new InfoType [NewMax]; if (Info == NULL) { puts("Not Enough Memory!"); Max = 0; } else Max = NewMax ‐ 1; };

"Memory Leak"

You Have Created A Memory leak If You Have Executed The Program One TIme!?

slide-46
SLIDE 46

template <class InfoType> class ListType { public: ListType (long int NewMax = 10); ~ListType(void); private: InfoType Info[10]; long Max, ActNo; };

Write The Destructor ~ListType (void)