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

This lecture was delivered by Prof. Usman Younis at Quaid-i-Azam University. This lecture covers following points of course Object Oriented Programming using C plus plus: Templates, Exceptions, Declaration, Facilitates, Function, Templates, Generic, Language, Template, Compiler

Typology: Slides

2011/2012

Uploaded on 07/31/2012

saqqi
saqqi 🇵🇰

4

(33)

40 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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