chapter 17-1 templates and exceptions
DESCRIPTION
Chapter 17-1 Templates and Exceptions. Dale/Weems. Chapter 17 Topics. C++ Function Templates Instantiating a Function Templates User-defined Specializations C++ Class Templates Instantiating Class Templates Function Definitions for Members of a Template Class - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/1.jpg)
1
Chapter 17-1
Templates and
Exceptions
Dale/Weems
![Page 2: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/2.jpg)
2
Chapter 17 Topics C++ Function Templates Instantiating a Function Templates User-defined Specializations C++ Class Templates Instantiating Class Templates Function Definitions for Members of a Template
Class Exception Classes, Throwing an Exception Exception Handlers
![Page 3: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/3.jpg)
3
Generic Algorithms
• Generic algorithms are algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not
![Page 4: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/4.jpg)
4
Example of a Generic Algorithmvoid PrintInt(int n){ cout << "***Debug" << endl; cout << "Value is " << n << endl;}void PrintChar(char ch){ cout << "***Debug" << endl; cout << "Value is " << ch << endl;}void PrintFloat(float x){ }void PrintDouble(double d){ }
sum = alpha + beta + gamma;PrintInt(sum); PrintChar(initial); PrintFloat(angle);
To output the traced values, we insert:
![Page 5: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/5.jpg)
5
Function Overloading
• Function overloading is the use of the same name for different functions, distinguished by their parameter lists
Eliminates need to come up with many different names for identical tasks
Reduces the chance of unexpected results caused by using the wrong function name
![Page 6: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/6.jpg)
6
Example of Function Overloadingvoid Print(int n){ cout << "***Debug" << endl; cout << "Value is " << n << endl;}void Print(char ch){ cout << "***Debug" << endl; cout << "Value is " << ch << endl;}void Print(float x){ }
Print(someInt);Print(someChar);Print(someFloat);
To output the traced values, we insert:
![Page 7: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/7.jpg)
7
Function Template• A C++ language construct that allows the compiler to generate multiple versions of a function by allowing parameterized data types
Template < TemplateParamList >FunctionDefinition
FunctionTemplate
TemplateParamDeclaration
class Identifiertypename
![Page 8: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/8.jpg)
8
Example of a Function Template template<class SomeType>
void Print(SomeType val){ cout << "***Debug" << endl; cout << "Value is " << val << endl;}
Print<int>(sum);Print<char>(initial);Print<float>(angle);
To output the traced values, we insert:
Template parameter
Template argument
![Page 9: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/9.jpg)
9
Instantiating a Function Template
• When the compiler instantiates a template, it substitutes the template argument for the template parameter throughout the function template
Function < TemplateArgList > (FunctionArgList)
TemplateFunction Call
![Page 10: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/10.jpg)
10
Implicit template arguments
Print(sum); // Implicit: Print<int>(sum) Print(initial); // Implicit: Print<char>(initial) Print(angle); // Implicit: Print<float>(angle)
![Page 11: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/11.jpg)
11
Enhancing the Print Template template<class SomeType>
void Print( string vName,// Name of the variable
SomeType val )// Value of the variable
{ cout << "***Debug" << endl; cout << "Value of " << vName
<< “ = “ << val << endl;}
![Page 12: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/12.jpg)
12
Generic Functions, Function Overloading, Template Functions
Generic FunctionDifferent Function Definitions
Different Function Names
Function OverloadingDifferent Function Definitions
Same Function Name
Template FunctionsOne Function Definition (a function template)
Compiler Generates Individual Functions
![Page 13: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/13.jpg)
13
User-Defined Specializations
Example that demonstrates use of template < >template<>
void Print(string vName, // Name of the variable
StatusType val ) // Value of the variable
{
cout << "***Debug" << endl;
cout << "Value of " << vName << " = ";
switch (val)
![Page 14: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/14.jpg)
14
Example, continued.
{ case OK : cout << "OK"; break; case OUT_OF_STOCK : cout << "OUT_OF_STOCK"; break; case BACK_ORDERED : cout << "BACK_ORDERED"; break; default : cout << "Invalid value"; } cout << endl;}
![Page 15: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/15.jpg)
15
Organization of Program Code
Three possibilities:
1. Template definitions at the beginning of program file, prior to main function
2. Function prototypes first, then the main function, then the template definitions
3. Template definition in the header file, use #include to insert that file into the program
![Page 16: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/16.jpg)
16
What is a Generic Data Type?
• It is a type for which the operations are defined but the data types of the items being manipulated are not
![Page 17: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/17.jpg)
17
What is a Class Template?
• It is a C++ language construct that allows the compiler to generate multiple versions of a class by allowing parameterized data types
![Page 18: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/18.jpg)
18
Example of a Class Template template<class ItemType>class GList{public: bool IsEmpty() const; bool IsFull() const; int Length() const; void Insert(/* in */ ItemType item); void Delete(/* in */ ItemType item); bool IsPresent(/* in */ ItemType item) const; void SelSort(); void Reset() const; ItemType GetNextItem(); GList(); // Constructorprivate: int length; ItemType data[MAX_LENGTH];};
Template parameter
![Page 19: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/19.jpg)
19
Instantiating a Class Template
// Client code GList<int> list1;GList<float> list2;GList<string> list3; list1.Insert(356);list2.Insert(84.375);list3.Insert("Muffler bolt");
To create lists of different data types
GList_int list1;GList_float list2;GList_string list3;
template argument
Compiler generates 3 distinct class types
![Page 20: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/20.jpg)
20
Instantiating a Class Template• Class template arguments must be
explicit• The compiler generates distinct class
types called template classes or generated classes
• When instantiating a template, a compiler substitutes the template argument for the template parameter throughout the class template
![Page 21: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/21.jpg)
21
Substitution Example
class GList_int{public: void Insert(/* in */ ItemType item);
void Delete(/* in */ ItemType item);
bool IsPresent(/* in */ ItemType item) const; private: int length; ItemType data[MAX_LENGTH];};
int
int
int
int
![Page 22: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/22.jpg)
22
Writing Function Templatestemplate<class ItemType>
void GList<ItemType>::Insert(/* in */ ItemType item)
{
data[length] = item;
length++;
}
void GList<float>::Insert(/* in */ float item)
{
data[length] = item;
length++;
}
![Page 23: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/23.jpg)
23
Organization of Program Code
A compiler must know the argument to the template in order to generate a function template, and this argument is located in the client code
Solutions Have specification file include implementation
file Combine specification file and implementation
file into one file
![Page 24: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/24.jpg)
24
Warning!
Are you using an IDE (integrated development environment) where the editor, compiler, and linker are bundled into one application?
Remember The compiler must know the template argument
How you organize the code in a project may differ depending on the IDE you are using
![Page 25: Chapter 17-1 Templates and Exceptions](https://reader036.vdocuments.us/reader036/viewer/2022062309/56814e2f550346895dbb9464/html5/thumbnails/25.jpg)
25
The End of Chapter 17 Part 1