Docsity

Templates and Exceptions-Object Oriented Programming-Lecture Slides, Slides of Object Oriented Programming

Toggle sidebar
bg1
5/26/2011
1
Ob
j
ect Oriented
j
Programming using C++
Lecture โ€“ 15
Templates and Exceptions
Introduction
๎š„Template is a declaration (similar to class
declaration) which deals with generic types
declaration)
,
which deals with generic types
๎š„Facilitates to create a large range of related
functions or classes
๎š„Function Templates
๎š‰Permit the development of generic algorithms
Cl T l
Usman Younis
๎š„
Cl
ass
T
emp
l
ates
๎š‰Permit the development of generic objects
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Templates and Exceptions-Object Oriented Programming-Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object Orientedj

Programming using C++

Lecture โ€“ 15

Templates and Exceptions

Introduction

ย„ Template is a declaration (similar to class

declaration)declaration), which deals with generic types which deals with generic types

ย„ Facilitates to create a large range of related

functions or classes

ย„ Function Templates

ย‰ Permit the development of generic algorithms

Cl T l

Usman Younis

ย„ Class Templates

ย‰ Permit the development of generic objects

Function Templates

Function Templates

ย„ A C++ language construct that allows the compiler to generate multiple versions of acompiler to generate multiple versions of a function by allowing parameterized data types.

Function Template

Usman Younis

Template < TemplateParamList >

FunctionDefinition

Example

#include <iostream.h> #include <string.h>

void main() { int a = 3, b = 5; change(a, b); cout<<a<<" "<<b<<endl;

template void change(Z& x, Z& y) { Z temp = x; x = y; y = temp;

char *m, *n ; m = new char[50]; strcpy(m, "hello");

n = new char[50]; strcpy(n, "world this");

Usman Younis

y = temp; } (^) change(m, n); cout<<m<<" "<<n<<endl;

delete[] m; delete[] n; }

Class Templates

Class Templates

ย„ A C++ language construct that allows the compiler to generate multiple versions of acompiler to generate multiple versions of a class by allowing parameterized data types.

Class Template

Usman Younis

Template < TemplateParamList >

ClassDefinition

Class Templates (contd..)

template class my_Class {

Template parameter

{ private: A arg1; A arg2;

public: my_Class(A a, A b): arg1(a), arg2(b){}

Usman Younis

A add() { return arg1 + arg2; } };

Exception Handling

Exceptions

ย„ Exceptions are errors that occur at run-time ย„ย„ An exception is any unusual event that may requireAn exception is any unusual event that may require special processing ย„ This special processing is called exception handling ย„ The reasons why exceptions occur are numerous: ย‰ Falling short of memory ย‰ Inability to open a file Exceeding the bounds of an array

Usman Younis

ย‰ Exceeding the bounds of an array ย‰ Attempting to assign an impossible value

Exceptions (contd..)

ย„ Some of the generic strategies to handle the exceptions can be:exceptions can be: ย‰ Displaying the error message on the screen ย‰ Dialog box incase of GUI ย‰ Terminating the program ย‰ Request user to input correct values

Usman Younis

Exception Handling in C

ย„ Usually C programmers deal with exception by: ย‰ Following the function calls with error checks on return values to find whether the function did its job properly or not

if ( func1( )== ERROR_VALUE) //handle the error else //do the normal processing

Usman Younis

if ( func2( )== NULL) //handle the error else //do the normal processing

Exception handling process

ย„ Programmer writes the code that is suspected to cause an exception in try blocksuspected to cause an exception in try block ย„ Code section that encounters an error throws an object that is used to represent exception

ย„ catch blocks follow try block to catch the

Usman Younis

object thrown

Exception handling

class DivideByZero{ public: DivideByZero() { }

void main(){ int a = 0, b = 0, quot = 0, sum = 0; for (int i=0; i<10; i++ ) { };

int Quotient(int numer, int denom) { if (denom == 0) throw DivideByZero(); //throw exception of class

try{ cin>>a>>b; quot= Quotient(a,b); cout<<a <<โ€œ/โ€<<b<<โ€œis:โ€<< quot; sum += quot; }

Usman Younis

//throw exception of class //DivideByZero return numer / denom; }

catch(DivideByZero){ i--; //decrement cout<<โ€œ\n Attempt to divide numerator with zeroโ€; } }//end for }//end main

Exceptions try { // Statements that process data and may throw // exceptions of type int, string, and SalaryError } catch ( int ) { // Statements to handle an int exception } catch ( string s ) { cout << s << endl;

Usman Younis

; // statements to handle string exception } catch ( SalaryError ) { // Statements to handle a salary error }

Try/Catch Block

ย„ If no exception is thrown in a try block ย‰ All catch blocks for that try block are ignored ย‰ Execution resumes after the last catch block ย„ If an exception is thrown in a try block ย‰ Remaining statements in that try block are ignored ย„ The program searches catch blocks in order, looking for an appropriate exception handler

Usman Younis

ย„ If the type of thrown exception matches the parameter type in one of the catch blocks: ย‰ Code of that catch block executes ย‰ Remaining catch blocks are ignored