Introduction to Cpp Templates and Exceptions-Object Oriented Programming and Data Structures-Lecture Slides, Slides of Object Oriented Programming

This lecture was delivered by Chanky Rathin for Object Oriented Programming and Data Structures at Anna University of Technology. It includes: C , Templates, Exceptions, Function, Class, Handler, Overloading, Instantiating, Naïve, Approach, Parameter, Lists

Typology: Slides

2011/2012

Uploaded on 07/13/2012

shuja
shuja 🇮🇳

4.3

(6)

51 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to
C++ Templates and Exceptions
C++ Function Templates
C++ Class Templates
Exception and Exception Handler
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download Introduction to Cpp Templates and Exceptions-Object Oriented Programming and Data Structures-Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Introduction to

C++ Templates and Exceptions

^

C++ Function Templates

^

C++ Class Templates

^

Exception and Exception Handler

C++ Function Templates

^

Approaches for functions that implementidentical tasks for different data types^ 

Naïve Approach ^ Function Overloading ^ Function Template

^

Instantiating a Function Templates

Example

void 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 ){

To output the traced values, we insert: PrintInt(sum);PrintChar(initial);PrintFloat(angle);

Approach 2:Function Overloading

(Review)

-^

The use of the same name for different C++functions, distinguished from each other bytheir 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.

Approach 3: Function Template •^ A C++ language construct that allows the compilerto generate multiple versions of a function byallowing parameterized data types.

Template < TemplateParamList >FunctionDefinition

FunctionTemplateTemplateParamDeclaration: placeholder

class typeIdentifier

typename variableIdentifier

Example of a Function Template

template<class

SomeType>

void

Print(

SomeType

val

)

{

cout

<<

"***Debug"

<<

endl;

cout

<<

"Value

is

"

<<

val

<<

endl;

}

To output the traced values, we insert: Print(sum);Print(initial);Print(angle);

Template parameter(class, user definedtype, built-in types)

Templateargument

Summary of Three Approaches

Naïve Approach

Different Function DefinitionsDifferent Function Names

Function Overloading

Different Function Definitions

Same Function Name

Template Functions

One Function Definition (a function template)Compiler Generates Individual Functions

Class Template

-^ A C++ language construct that allows the compilerto generate multiple versions of a class by allowingparameterized data types.

Class TemplateTemplate < TemplateParamList >ClassDefinition

TemplateParamDeclaration: placeholder

class typeIdentifier

typename variableIdentifier

Instantiating a Class Template • Class template arguments

must

be

explicit.

-^

The compiler generates distinct classtypes called template classes orgenerated classes.

-^

When instantiating a template, acompiler substitutes the templateargument for the template parameterthroughout the class template.

Instantiating a Class Template // Client codeGList list1;GList list2;GList 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 3distinct class types

Function Definitions for

Members of a Template Class

templatevoid GList::Insert( /* in */ ItemType item ){

data[length] = item;length++; } //after substitution of floatvoid GList::Insert( /* in */ float item ){

data[length] = item;length++; }

Another Template Example:

passing two parameters^ template <class T, int size>

class Stack {...}; Stack<int,128> mystack;

non-type parameter

Handling Exception

-^

If without handling,

•^ Program crashes •^ Falls into unknown state

-^

An exception handler is a section of programcode that is designed to execute when aparticular exception occurs

•^ Resolve the exception •^ Lead to known state, such as exiting theprogram

Standard Exceptions

^

Exceptions Thrown by the Language

–^ new

^

Exceptions Thrown by StandardLibrary Routines

^

Exceptions Thrown by user code,using

throw

statement