C++_Chapter7
C++_Chapter7
Template is a new concept which enable us to define generic class es and functions and
thus provides support for generic programming.
Generic programming is an approach where generic types are used as parameters in
algorithms so that they work for a variety of suitable data types and data structures.
A template can be used to create a family of classes or functions.
Since the template is defined with a parameter that would be replaced by a specified data
type at the time of actual use of the class or function, the templates are sometimes called
parameterized classes or function.
Format:
template<class T>
class class_name
{
………………. //class member
………………. // specifications with
………………. // anonymous type T
………………. // where or appropriate
};
Example:
#include <iostream>
using namespace std;
template<class T1>
class Test
{
T1 a;
public:
void add (T1 x, T1 y)
{
a=x+y;
}
void mul(T1 x, T1 y)
{
a = x * y;
}
void div(T1 x, T1 y)
{
a = x/y;
}
void sub(T1 x, T1 y)
{
a = x -y;
}
void show()
1
Compiled By:
Er. Ranjan Raj Aryal
Amrit Campus
{
cout<<a<<"\n";
}
};
int main()
{
Test <float> testf;
Test <int> testi;
testf.add(5.23,6.43);testf.show();
testf.div(6.4,2.0);testf.show();
testi.mul(20,32); testi.show();
testi.sub(200,150); testi.show();
return 0;
}
Output
2
Compiled By:
Er. Ranjan Raj Aryal
Amrit Campus
}
};
int main()
{
Test<float, int> test1(1.23,123);
Test<int,char>test2(100,'W');
test1.show();
test2.show();
return 0;
}
Output
Function Templates:
template<class T>
returntype functionname (arguments of type t)
{
……………………..
…………………….. // Body of function with type T
……………………..
…………………….. // Wherever appropriate
}
#include <iostream>
using namespace std;
3
Compiled By:
Er. Ranjan Raj Aryal
Amrit Campus
int main()
{
fun(100,200,11.22,33.44);
return 0;
}
#include<iostream>
#include<string.h>
using namespace std;
template<class T1, class T2>
void display (T1 x, T2 y)
{
cout<< x << " " <<y <<"\n";
}
int main ()
{
display (2022, "NEPAL");
display (12.34, 1234);
return 0;
}
4
Compiled By:
Er. Ranjan Raj Aryal
Amrit Campus
Example:
#include<iostream>
#include<string.h>
using namespace std;
template<class T>
void display (T x)
{
cout<<”Template display:”<<x<<”\n”;
}
void display (int x)
{
cout<<”Explicity display:”<<x<<”\n”;
}
int main()
{
display (100);
display(12.34);
display (‘C’);
return 0;
}
5
Compiled By:
Er. Ranjan Raj Aryal
Amrit Campus
};
template <class T1>
class polar
{
T1 radius;
T1 thita;
public:
polar(){ }
polar(rectangle <float> r)
{ T1 tempx=r.get_x();
T1 tempy=r.get_y();
radius = sqrt(tempx*tempx + tempy*tempy);
thita = atan(tempy/tempx);
}
void show()
{ cout<<"radius is:"<<radius<<endl;
cout<<"thita is:"<<thita*(180/3.14);
}
};
int main()
{
rectangle <float> r(6.0,9.0);
polar <float> p(r);
p.show();
return 0;
}
Exception Handling
The two most common types of bugs are logic errors and syntactic errors
The logic errors occur due to poor understanding of the problem and solution procedure
The syntactic error occurs due to poor understanding of the language itself
We often come across with some peculiar problems other than logic or syntax errors. They are
known as exceptions.
Exceptions are runtime anomalies or unusual conditions that a program may encounter while
executing
Anomalies might include conditions such as division by zero, access to an array outside of its
bounds or running out of memory or disk space.
Exception handling is a new feature added to ANSI C++
6
Compiled By:
Er. Ranjan Raj Aryal
Amrit Campus
The purpose of exception handling mechanism is to provide means to detect and report an
“exceptional Circumstances” so that appropriate actions can be taken.
This mechanism suggests the following tasks:
o 1. Find the problem (Hit the exception)
o 2. Inform that an error has occurred (Throw the exception)
o 3. Receive the error information (catch the exception)
o 4. Take corrective actions (Handle the exception)
“ try “
The keyword try is used to preface a block of statements which may generate
exceptions.
“ throw “
When an exception is detected, it is thrown using a throw statement in the try block
“ catch “
“ catch “ catches the exceptions thrown by the throw statement in the try block.
Note: The catch block that catches the exceptions must immediately follow the try block that throws the
exception.
General form:
7
Compiled By:
Er. Ranjan Raj Aryal
Amrit Campus
--------------
--------------
try {
---------
---------
----------
----------
----------
---------
----------
Example
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter values of a and b\n";
cin>>a>>b;
int x = a - b;
try{
if(x!=0)
{
cout<<"Result(a/x)="<<a/x<<"\n";
}
else{
throw(x);
}
8
Compiled By:
Er. Ranjan Raj Aryal
Amrit Campus
}
catch(int i)
{
cout<<"Exception caught : x= "<<x<<"\n";
}
cout<<"END" ;
return 0;
}
9
Compiled By:
Er. Ranjan Raj Aryal
Amrit Campus
…….
}
try
{
…..
….. invoke function here
…..
}
catch (type arg) // catches exception
{
…….
……. Handles exception here
…….
Note: The try block is immediately followed by the catch block, irrespective of the location of the throw
point. In the below program show how a try block invokes a function that generates an exception
/* Throw point outside the try block */
#include <iostream>
using namespace std;
void divide(int x, int y, int z)
{
cout << "\n we are inside the function \n";
if((x-y)!=0) // it is ok
{
int r=z/(x-y);
cout << "Result= " << r <<"\n";
}
10
Compiled By:
Er. Ranjan Raj Aryal
Amrit Campus
}
}
void test(int x)
try
else
else
catch(char c) // catch 1
catch(int m) // catch 2
11
Compiled By:
Er. Ranjan Raj Aryal
Amrit Campus
catch(double d) // catch 3
int main()
test(1);
test(0);
test(-1);
test(2);
12
Compiled By:
Er. Ranjan Raj Aryal
Amrit Campus
#include <iostream>
using namespace std;
void test(int x)
{
try
{
if (x==0) throw x;
if (x==-1) throw 'x';
if (x==1) throw 1.0;
}
catch (...)
{
cout << "Caught an exception \n";
}
}
int main()
{
cout << "Testing generic catch \n";
test(-1);
test(0);
test(1);
return 0;
}
Rethrowing an Exception
#include <iostream>
using namespace std;
void divide(double x, double y)
{
cout << "Inside function \n";
try
{
if (y==0.0)
throw y; // throwing double
else
cout << "Division=" << x/y << "\n";
}
catch (double)
13
Compiled By:
Er. Ranjan Raj Aryal
Amrit Campus
// catch a double
{
cout << "caught double inside function \n";
throw ; // re-throwing double
}
cout << "End of function \n \n";
}
int main()
{
cout << "Inside main \n";
try
{
divide(10.5,2.0);
divide(20.0,0.0);
}
catch (double)
{
cout << "Caught double inside main \n";
}
cout << "End of main \n";
return 0;
}
14