






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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