cpp_unit-V
cpp_unit-V
UNIT-V
(Generic Programming with Templates & Exception Handling)
P a g e 1 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
Template Declaration
classname_of_class
The first statement template < class T> tells the compiler that
the following class declaration can use the template data type.
The T is a variable of template type that can be used in the class
to define a variable of template type.
Both template and class are keywords. The <> (angle bracket)
is used to declare the variables of template type that can be used
inside the class to define the variables of template type. One or
more variables can be declared separated by a comma.
Templates cannot be declared inside classes or functions. They
should be global and should not be local.
T k;
#include<iostream.h>
#include<conio.h>
class data
public:
data(char c)
data(int c)
data(double c)
};
void main()
clrscr();
P a g e 3 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
OUTPUT
c = A Size in bytes :1
retuntypefunction_name (arguments )
// code
#include<iostream.h>
#include<conio.h>
template<class T>
void show ( T x)
{cout<<“\n x=”<<x ;}
void main()
P a g e 5 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
clrscr();
char c=‘A’;
inti=65;
double d=65.254;
show(c);
show(i);
show(d);
OUTPUT
x=A
x=65
x=65.254
#include<iostream.h>
#include<conio.h>
template<class T>
P a g e 6 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
class data
T x;
public:
data (T u) {x=u;}
void show (T y)
cout<<“ x=”<<x;
};
void main()
clrscr();
data<char> c(‘B’);
data<int>i(100);
data<double> d(48.25);
c.show(‘A’);
i.show(65);
d.show(68.25);
OUTPUT
P a g e 7 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
x=B y=A
x=100 y=65
x=48.25 y=68.25
#include<conio.h>
template<class S>
classsqr
public:
sqr (S c)
cout<<“\n”<< “ c=”<<c*c;
};
P a g e 8 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
void main()
clrscr();
sqr<int>i(25);
sqr<float> f(15.2);
OUTPUT
c = 625
c = 231.039993
P a g e 9 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
#include<iostream.h>
#include<conio.h>
template<class A>
void show(A c)
void main()
clrscr();
show(‘C’);
show(50);
show(50.25);
return 0;
P a g e 10 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
OUTPUT
Template variable c = C
Integer variable f = 50
#include<iostream.h>
#include<conio.h>
template<class S>
{
P a g e 11 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
S p;
void main()
clrscr();
inti[5]={4,3,2,1,-4};
float f[5]={5.3,5.1,5.5,4.5,3.2};
int x=0,y=0;
// integer array
// float array
P a g e 12 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
OUTPUT
1. Macros are not type safe; that is, a macro defined for integer operations
cannot accept floatdata. They are expanded with no type checking.
2. It is difficult to find error in macros.
3. In case a variable is post-incremented or decremented, the operation is
carried out twice.
void main()
int a=10, c;
c=max(a);
cout<<c;
#include<iostream.h>
#include<conio.h>
template<class T>
P a g e 13 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
T max(T k)
++k;
return k;
void main()
clrscr();
int a=10, c;
c=max(a);
cout<<c;
OUTPUT
11
Exception Handling
Synchronous exceptions
Asynchronous exceptions
P a g e 15 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
try
statement 1;
statement 2;
throw (excep);
throw excep;
P a g e 16 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
catch: Similar to the try block, the catch block also contains a
series of statements enclosed in curly braces. It also contains an
argument of an exception type in parentheses.
try
Statement 1;
Statement 2;
catch ( argument)
P a g e 17 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
#include<iostream.h>
#include<conio.h>
void main()
Clrscr();
intx,y;
cin>>x>>y;
int j;
j=x>y ?0 :1;
try
if (j==0)
{ cout<<“Subtraction (x-y)=”<<x-y<<“\n”; }
else { throw(j); }
catch (int k)
P a g e 18 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
OUTPUT
78
Exception caught : j = 1
48
Exception caught : j = 1
We can also define multiple catch blocks; in the try block, such
programs also contain multiple throw statements based on
certain conditions. The format of multiple catch statements is as
follows:
try
// try section
catch (object1)
P a g e 19 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
// catch section1
catch (object2)
// catch section2
.......
.......
// catch section-n
#include<iostream.h>
#include<conio.h>
try
{
P a g e 20 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
if (k==0) throw k;
else
else
catch(char g)
catch (int j)
catch (double f)
void main()
P a g e 21 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
Clrscr();
num(0);
num(5);
num(-1);
return 0;
OUTPUT
SPECIFYING EXCEPTIONS
Specifying Exceptions
P a g e 22 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
Statement 1;
statement 3;
#include<iostream>
#include<conio.h>
else
if (k==2) throw k;
else
void main()
P a g e 23 | 25 (Ranganadh.G)
OBJECT-ORIENTED PROGRAMMING THROUGH C++(R16)
Clrscr();
try {
cout<<“k==1\n”;
check(1);
cout<<“k==2\n”;
check(2);
cout<<“k==-2\n”;
check(-2);
cout<<“k==3\n”;
check(3);
catch ( char g)
catch (int j)
catch (double s)
OUTPUT
k==1
ASSIGNMENT
P a g e 25 | 25 (Ranganadh.G)