






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 12
This page cannot be seen from the preview
Don't miss anything!







Usman Younis
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.
Usman Younis
Example
#include <iostream.h> #include <string.h>
void main() { int a = 3, b = 5; change(a, b); cout<<a<<" "<<b<<endl;
template
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
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.
Usman Younis
Class Templates (contd..)
template
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; } };
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