Sie sind auf Seite 1von 61

LIST OF EXPERIMENTS

EXPT
TITLE
NO.
1A PROGRAM USING CONTROL STRUCTURES
1B PROGRAM USING ARRAYS
2A PROGRAM USING CLASS AND OBJECTS
2B PROGRAM USING ARRAY OF OBJECTS
3A PROGRAM USING RECURSIVE FUNCTION
3B PROGRAM USING INLINE FUNCTION
4 PROGRAM USING CONSTRUCTOR AND DESTRUCTOR
5A PROGRAM FOR UNARY OPERATOR OVERLOADING
5B PROGRAM FOR BINARY OPERATOR OVERLOADING
6 PROGRAM FOR FUNCTION OVERLOADING
7A PROGRAM FOR SINGLE INHERITANCE
7B PROGRAM FOR MULTIPLE INHERITANCE
7C PROGRAM FOR MULTILEVEL INHERITANCE
8 PROGRAM FOR DATA CONVERSION
9A PROGRAM USING FRIEND FUNCTION
9B PROGRAM USING FRIEND CLASSES
10A PROGRAM USING VIRTUAL FUNCTION
10B PROGRAM FOR VIRTUAL BASE CLASS
11A PROGRAM USING FUNCTION TEMPLATE
11B PROGRAM USING CLASS TEMPLATE
12 PROGRAM FOR FILE HANDLING
13A PROGRAM FOR EXCEPTION HANDLING
13B PROGRAM FOR EXCEPTION HANDLING WITH MULTIPLE CATCH
EXPT.NO: 1A PROGRAM USING CONTROL STRUCTURES

Aim:
To write a program to implement Control Structures (for, while, do-while, switch) using C++.

Algorithm:

Step 1: define a class 'expt1a' with private variables i,n,next,fact,ch, and public member function
void constructure().
Step 2: in function void constructure(), read the choice to perform various operations.
Step 3: is choice=1, initialize f1=0 and f2=1. read the number of terms. calculate next=f1+f2,
f1=f2, f2=next by using do-while loop. write the fibonacci series.
Step 4: is choice=2, read the number. initialize i=2. check n%i is equal to 0 using the loop
while(i<n-1). is true write given number is a prime number. is not, write given number is
not a prime number.
Step 5: is choice=3, read the number. initialize fact=1. calculate fact=fact*i by using the loop
for(i=0;i<n;i++). write factorial of a given number.
Step 6: in function void main(), create an object 'cs' for the class 'expt1a'. invoke the function
constructure() by using cs.constructure().

Program:

#include<iostream.h>
#include<conio.h>

class expt1a
{
private:
int i,n,next,fact,ch;
public:
void constructure()
{
cout<<"1.do-while 2.while 3.for \n";
cout<<"enter your choice : ";
cin>>ch;
switch(ch)
{
case 1:
int f1=0,f2=1;
cout<<"/* fibonacci series */ \n";
cout<<"enter the number of terms : ";
cin>>n;
cout<<f1<<"\t"<<f2<<"\t";
i=0;
do
{
next=f1+f2;
f1=f2;
f2=next;
i++;
cout<<next<<"\t";
}while(i<n-2);
break;
case 2:
cout<<"/* prime number */ \n";
cout<<"enter the number : ";
cin>>n;
i=2;
while(i<=n-1)
{
if(n%i==0)
{
cout<<"given number is not a prime number \n";
break;
}
i++;
}
if(i==n)
cout<<"given number is prime number \n";
break;
case 3:
cout<<"/* factorial of a given number */ \n";
cout<<"enter the number : ";
cin>>n;
fact=1;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
cout<<"factorial of a given number is : "<<fact;
break;
default:
cout<<"enter correct choice";
}
}
};

void main()
{
expt1a cs;
clrscr();
cs.constructure();
getch();
}

Output:

1.do-while 2.while 3.for


enter your choice : 1
/* fibonacci series */
enter the number of terms : 7
0 1 1 2 3 5 8

1.do-while 2.while 3.for


enter your choice : 2
/* prime number */
enter the number : 25
given number is not a prime number

1.do-while 2.while 3.for


enter your choice : 2
/* prime number */
enter the number : 37
given number is prime number

1.do-while 2.while 3.for


enter your choice : 3
/* factorial of a given number */
enter the number : 7
factorial of a given number is : 5040

Result:
Thus the program has been implemented for Control Structures (for, while, do-while, switch)
using C++.
Expt.No.1B PROGRAM USING ARRAYS

Aim:
To write a program to implement Arrays(one dimensional, two dimensional) using C++.

Algorithm:

Step 1: define a class 'expt1b' with private variables a[20],n,find,found, p[10][10], q[10][10],
s[10][10],r,c,i,j, and public member functions void one_d_array(), void two_d_array().
Step 2: in function void one_d_array(), read the number of elements and elements of the array.
read the element to be searched. check a[i] is equal to find. is true, write element found. is
not, write element not found.
Step 3: in function void two_d_array(), read the number of rows and columns of a matrices. read
the elements of matrix P,Q. Calculate s[i][j]=p[i][j]+q[i][j]. write matrix P,Q,S.
Step 4: in function void main(), create an object 'arr' for the class 'expt1b'. invoke the functions
one_d_array(), two_d_array() by using arr.one_d_array(), arr.two_d_array() respectively.

Program:

#include<iostream.h>
#include<conio.h>

class expt1b
{
private:
int a[20],n,find,found;
int p[10][10],q[10][10],s[10][10];
int r,c,i,j;
public:
void one_d_array()
{
cout<<" /* linear search */";
found=0;
cout<<"\n enter the number of elements : ";
cin>>n;
cout<<"enter the elements : ";
for(int i=0;i<n;i++)
cin>>a[i];
cout<<"enter the element to search : ";
cin>>find;
for(i=0;i<n;i++)
{
if(a[i]==find)
{
found=1;
cout<<"element found at position : "<<i+1;
}
}
if(found==0)
cout<<"element not found";
}

void two_d_array()
{
cout<<"\n /* matrix addition */";
cout<<"\n enter the number of rows and columns : ";
cin>>r>>c;
cout<<"enter the elements of matrix P : ";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>p[i][j];
}
}
cout<<"enter the elements of matrix Q : ";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>q[i][j];
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
s[i][j]=p[i][j]+q[i][j];
}
}
cout<<"\n matrix P ";
for(i=0;i<r;i++)
{
cout<<"\n";
for(j=0;j<c;j++)
{
cout<<"\t"<<p[i][j];
}
}
cout<<"\n matrix Q ";
for(i=0;i<r;i++)
{
cout<<"\n";
for(j=0;j<c;j++)
{
cout<<"\t"<<q[i][j];
}
}
cout<<"\n matrix S = matrix P + matrix Q ";
for(i=0;i<r;i++)
{
cout<<"\n";
for(j=0;j<c;j++)
{
cout<<"\t"<<s[i][j];
}
}
}
};

void main()
{
expt1b arr;
clrscr();
arr.one_d_array();
arr.two_d_array();
getch();
}

Output:

/* linear search */

enter the number of elements : 5


enter the elements : 13 78 96 45 35
enter the element to search : 45
element found at position : 4
/* matrix addition */

enter the number of rows and columns : 2 2


enter the elements of matrix P : 4 4 5 5
enter the elements of matrix Q : 3 4 5 6

matrix P
4 4
5 5
matrix Q
3 4
5 6
matrix S = matrix P + matrix Q
7 8
10 11

Result:
Thus the program has been implemented for Arrays(one dimensional, two dimensional) using
C++.
Expt.No.2A PROGRAM USING CLASS AND OBJECTS

Aim:
To write a program to implement Class and Objects using C++.

Algorithm:

Step 1: define a class 'expt2a' with private variables n.n1,rem,sum, and public member function
void swap(int *,int *), void armstrong(int n).
Step 2: in function void swap(int *m,int *n), swap the numbers using temp=*m, *m=*n, *n=temp.
Step 3: in function void armstrong(int n), initialize sum=0, assign n1=n. calculate rem=n1%10,
sum=sum+(rem*rem*rem), n1=n1/10 by using the loop while(n1!=0). check sum is equal
to n. is true, write given number is armstrong number. is not, write given number is not an
armstrong number.
Step 4: in function void main(), create objects 'co,co1,co2' for the class 'expt2a'. read two
numbers and write the numbers before swapping. call the function swap(int *m,int *n) by
using co.swap(&a,&b). write the numbers after swapping.
Step 5: read the number. invoke the function void armstrong(int n) by using co1.armstrong(a),
co2.armstrong(371).

Program:

#include<iostream.h>
#include<conio.h>

class expt2a
{
private:
int n,n1,rem,sum;
public:
void swap(int *,int *);
void armstrong(int n)
{
n1=n;
sum=0;
while(n1!=0)
{
rem=n1%10;
sum=sum+(rem*rem*rem);
n1=n1/10;
}
if(sum==n)
cout<<"\n"<<n<<" is an armstrong number \n";
else
cout<<"\n"<<n<<" is not an armstrong number \n";
}
};

void expt2a::swap(int *m,int *n)


{
int temp;
temp=*m;
*m=*n;
*n=temp;
}

void main()
{
expt2a co;
expt2a co1,co2;
clrscr();
int a,b;
cout<<"\n /* swapping of two numbers */ ";
cout<<"\n enter two numbers : ";
cin>>a>>b;
cout<<"\n before swapping";
cout<<"\n a="<<a<<"\t b="<<b;
co.swap(&a,&b);
cout<<"\n after swapping";
cout<<"\n a="<<a<<"\t b="<<b;
cout<<"\n /* armstrong number */ ";
cout<<"\n enter the number : ";
cin>>a;
co1.armstrong(a);
co2.armstrong(371);
getch();
}
Output:

/* swapping of two numbers */

enter two numbers : 38 13

before swapping
a=38 b=13

after swapping
a=13 b=38

/* armstrong number */

enter the number : 154

154 is not an armstrong number

371 is an armstrong number

Result:
Thus the program has been implemented for Class and Objects using C++
Expt.No.2B PROGRAM USING ARRAY OF OBJECTS

Aim:
To write a program to implement Array of Objects using C++.

Algorithm:

Step 1: define a class 'expt2b' with private variables l,b, and public member function void area(),
void perimeter(). create a constructor for the class and assign x,y values to l,b respectively.
Step 2: in function void area(), calculate the area of the rectangle l*b. write area of the triangle.
Step 3: in function void perimeter(), calculate the perimeter of the rectangle 2*(l+b). write
perimeter of the triangle.
Step 4: in function void main(), create an object 'arr[3]' for the class 'expt2b'. invoke the
functions void area(), void perimeter() by using arr[i].area(), arr[j].perimeter()
respectively.

Program:

#include<iostream.h>
#include<conio.h>

class expt2b
{
private:
int l,b;
public:
expt2b(int x,int y)
{
l=x;
b=y;
}
void perimeter();
void area()
{
cout<<"area is : "<<l*b<<"\n";
}
};

void expt2b::perimeter()
{
cout<<"perimeter is : "<<2*(l+b)<<"\n";
}
void main()
{
clrscr();
expt2b arr[3]={expt2b(5,7),expt2b(7,9),expt2b(8,12)};
cout<<"/* area of rectangle */ \n";
for(int i=0;i<3;i++)
arr[i].area();
cout<<"/* perimeter of rectangle */ \n";
for(int j=0;j<3;j++)
arr[j].perimeter();
getch();
}

Output:

/* area of rectangle */
area is : 35
area is : 63
area is : 96

/* perimeter of rectangle */
perimeter is : 24
perimeter is : 32
perimeter is : 40

Result:
Thus the program has been implemented for Array of Objects using C++.
Expt.No.3A PROGRAM USING RECURSIVE FUNCTION

Aim:
To write a program to implement Recursive Function using C++.

Algorithm:

Step 1: define a class recursion with private variables int n,sum, and public member function int
sumdigits(int n,int sum).
Step 2: in function int sumdigits(int n,int sum), check the number is equal to 0. is true, return sum.
is not, calculate sum=sum+n%10, n=n/10, and return sumdigits(n,sum).
Step 3: in function main(), create an object rf for the class recursion. read the number. invoke
the function sumdigits(int n, int sum) by using rf.sumdigits(n,sum). write sum of all digits.

Program:

#include <iostream.h>
#include<conio.h>

class recursion
{
private:
int n,sum;
public:
int sumdigits(int n, int sum)
{
if(n==0)
{
return sum;
}
else
{
sum=sum+n%10;
n=n/10;
return sumdigits(n,sum);
}
}
};

void main()
{
recursion rf;
clrscr();
int n,sum=0;
cout<<"enter a number : ";
cin>>n;
cout<<"\n sum of all digits : "<<rf.sumdigits(n,sum);
getch();
}

Output:

enter a number : 4567


sum of all digits : 22

enter a number : 4321


sum of all digits : 10

Result:
Thus the program has been implemented for Recursive Function using C++.
Expt.No.3B PROGRAM USING INLINE FUNCTION

Aim:
To write a program to implement Inline Function using C++.

Algorithm:

Step 1: define a class inlinefunc with public member functions inline float cube(float b),
inline float area_triangle(float b,float h), inline float greatest(float b, float h)
Step 2: in function inline float area_triangle(float b,float h), calculate the area of triangle 0.5*b*h
Step 3: in function inline float cube(float b), calculate the cube of a number b*b*b.
Step 4: in function inline float greatest(float b, float h), check b is greater than h. is true, b is
greater. is not, h is greater.
Step 5: in function main(), create an object obj for the class inlinefunc. read the numbers.
invoke the function inline float area_triangle(float b,float h) by using
obj.area_triangle(n1,n2). write the area of triangle.
Step 6: invoke the function inline float cube(float b) by using obj.cube(n1), obj.cube(n2). write the
cube of the numbers n1,n2.
Step 7: invoke the function inline float greatest(float b, float h), by using obj.greatest(n1,n2). write
the greatest of two numbers.

Program:

#include<iostream.h>
#include<conio.h>

class inlinefunc
{
public:
inline float area_triangle(float b,float h)
{
return 0.5*b*h;
}
inline float cube(float b)
{
return b*b*b;
}
inline float greatest(float b, float h)
{
return (b>h)?b:h;
}
};
void main()
{
inlinefunc obj;
float n1,n2;
clrscr();
cout<<"enter two numbers : ";
cin>>n1>>n2;
cout<<"\n area of triangle : "<<obj.area_triangle(n1,n2);
cout<<"\n cube of the number "<<n1<<"is : "<<obj.cube(n1);
cout<<"\n cube of the number "<<n2<<"is : "<<obj.cube(n2);
cout<<"\n greatest of two numbers is : "<<obj.greatest(n1,n2);
getch();
}

Output:

enter two numbers : 4 9

area of triangle : 18
cube of the number 4is : 64
cube of the number 9is : 729
greatest of two numbers is : 9

Result:
Thus the program has been implemented for Inline Function using C++.
Expt.No.4 PROGRAM USING CONSTRUCTOR AND DESTRUCTOR

Aim:
To write a program to implement Constructor and Destructor using C++.

Algorithm:

Step 1: define a class consdes with private members int a, b and public member functions
void swap(int, int), void getdata(), void putdata(). create a constructor for the class
consdes with a=0, b=0 and write hi cse. create a destructor for the class and write hi ace.
Step 2: in function swap(), calculate a=a+b, b=a-b, a=a-b and swap the numbers without using
temporary variable.
Step 3: in function getdata(), read the values of a, b.
Step 4: in function putdata(), write the values of a, b.
Step 5: in function main(), create an object cd for the class consdes. invoke the function
getdata() by using cd.getdata(). invoke the function putdata() by using cd.putdata() before
swapping. invoke the function swap(a,b) by using cd.swap(a, b). invoke the function
putdata() by using cd.putdata() after swapping.

Program:

#include<iostream.h>
#include<conio.h>
class consdes
{
private:
int a,b;
public:
consdes()
{
a=0;
b=0;
cout<<"hi cse... \n";
}
~consdes()
{
cout<<"\n hi ace...";
}
void getdata()
{
cout<<"enter the values of a,b : ";
cin>>a>>b;
}
void swap()
{
a=a+b;
b=a-b;
a=a-b;
}
void putdata()
{
cout<<"\n a="<<a<<"\t b="<<b;
}
};
void main()
{
clrscr();
consdes cd;
cd.getdata();
cout<<"before swapping:";
cd.putdata();
cd.swap();
cout<<"\n after swapping:";
cd.putdata();
getch();
}

output:

hi cse...

enter the values of a,b : 64 107


before swapping:
a=64 b=107
after swapping:
a=107 b=64

hi ace...

Result:
Thus the program has been implemented for Constructor and Destructor using C++.
Expt.No.5A PROGRAM FOR UNARY OPERATOR OVERLOADING

Aim:
To write a program to implement Unary Operator Overloading using C++.

Algorithm:

Step 1: define a class complex with private variables a,b as integers and public member functions
void input(), void operator++(), void operator(), void output(). create a constructor for
the class.
Step 2: in function input(), read the values of a,b.
Step 3: in function operator++(), pre-increment the values of a,b.
Step 4: in function operator--(), pre-decrement the values of a,b.
Step 5: in function output(), write the values of a,b as a complex number.
Step 6: in function main(), create an object obj for the class complex. invoke the function
input() by using obj.input().invoke the function output() by using obj.output(). write the
complex number. invoke the function operator++() by using obj++ and write the complex
number after increment. invoke the function operator--() by using obj-- and write the
complex number after decrement.

Program:

#include<iostream.h>
#include<conio.h>

class complex
{
private:
int a,b,c;
public:
complex(){}

void input()
{
cout<<"enter two numbers : ";
cin>>a>>b;
}
void operator++()
{
a=++a;
b=++b;
}
void operator--()
{
a=--a;
b=--b;
}
void output()
{
cout<<a<<"+i"<<b;
}
};

void main()
{
complex obj;
clrscr();
obj.input();
cout<<"\n the complex number is: \n";
obj.output();
obj++;
cout<<"\n complex number after increment:\n";
obj.output();
obj--;
cout<<"\n complex number after decrement:\n";
obj.output();
getch();
}
Output:

enter two numbers : 16 23

the complex number is:


16+i23

complex number after increment:


17+i24

complex number after decrement:


16+i23

Result:
Thus the program has been implemented for Unary Operator Overloading using C++.
Expt.No.5B PROGRAM USING BINARY OPERATOR OVERLOADING

Aim:
To write a program to implement Binary Operator Overloading using C++.

Algorithm:

Step 1: define a class complex with private members real, imag and public member functions
input(), output(), operator+(complex). create a constructor complex() and assign the values
real=imag=0.0.
Step 2: in function input(), read the real and imag value of complex number.
Step 3: in function output(), write the complex number after addition.
Step 4: in function operator+(complex c2), add the c2.real value with c1.real value and add the
c2.imag value with c1.imag value. here the operator + is used to add the two numbers.
Step 5: in function main(), create object c1, c2, c3 for class complex. invoke the function
input(), with c1.input(), c2.input(). invoke the function output() by using c1.output(),
c2.output() and write the complex numbers c1,c2.
Step 6: invoke the function operator+(complex c2), using c3=c1+c2. here the operator + is used
to invoke the function. invoke the function output() with c3.output().

Program:

#include<iostream.h>
#include<conio.h>
class complex
{
private:
float real;
float imag;
public:
complex()
{
real=imag=0.0;
}
void input()
{
cout<<"\n enter the real and imag part : ";
cin>>real>>imag;
}
complex operator + (complex c2);
void output()
{
cout<<real<<"+i"<<imag;
}
};

complex complex::operator+(complex c2)


{
complex temp;
temp.real=real+c2.real;
temp.imag=imag+c2.imag;
return (temp);
}

void main()
{
complex c1,c2,c3;
clrscr();
cout<<"\n enter the complex number c1";
c1.input();
cout<<"\n enter the complex number c2";
c2.input();
cout<<"\n complex numbers c1 is :";
c1.output();
cout<<"\n complex numbers c2 is :";
c2.output();
c3=c1+c2;
cout<<"\n addition of two complex numbers is:";
c3.output();
getch();
}

Output:

enter the complex number c1


enter the real and imag part : 7 11

enter the complex number c2


enter the real and imag part : 13 23

complex numbers c1 is :7+i11


complex numbers c2 is :13+i23
addition of two complex numbers is:20+i34

Result:
Thus the program has been implemented for Binary Operator Overloading using C++.
Expt.No.6 PROGRAM FOR FUNCTION OVERLOADING

Aim:
To write a program to implement Function Overloading using C++.

Algorithm:

Step 1: define a class funcover with private variable pi, and public member functions
void volume(float), void volume(float,float), void volume(float,float,float).
Step 2: in function void volume(float r), initialize pi=3.14. calculate the volume of sphere
(4/3)*pi*r*r*r and volume of hemisphere (2/3)*pi*r*r*r. write the volumes of sphere and
hemisphere.
Step 3: in function void volume(float r,float h), calculate the volume of cylinder pi*r*r*h and
volume of cone (1/3)*pi*r*r*h. write the volumes of cylinder and cone.
Step 4: in function void volume(float r1,float r2,float r3), calculate the volume of ellipsoid
(4/3)*pi*r1*r2*r3. write the volume of ellipsoid.
Step 5: in function main(), create an object cse for the class funcover. read the radius. invoke
the function volume(float r) by using cse.volume(r). read the radius and height. invoke the
function volume(float r,float h) by using cse.volume(r,h). read radius r1,r2,r3. invoke the
function volume(float,float,float) by using cse.volume(r1,r2,r3).

Program:

#include<iostream.h>
#include<conio.h>
class funcover
{
private:
float pi;
public:
void volume(float);
void volume(float,float);
void volume(float,float,float);
};

void funcover::volume(float r)
{
pi=3.14;
cout<<"\n volume of sphere = "<<(4.0/3.0)*pi*r*r*r;
cout<<"\n volume of hemisphere = "<<(2.0/3.0)*pi*r*r*r;
}
void funcover::volume(float r,float h)
{
cout<<"\n volume of cylinder = "<<pi*r*r*h;
cout<<"\n volume of cone = "<<(1.0/3.0)*pi*r*r*h;
}
void funcover::volume(float r1,float r2,float r3)
{
cout<<"\n volume of ellipsoid = "<<(4.0/3.0)*pi*r1*r2*r3;
}

void main()
{
funcover cse;
clrscr();
float r,h,r1,r2,r3;
cout<<"\n enter the radius : ";
cin>>r;
cse.volume(r);
cout<<"\n enter the radius and height : ";
cin>>r>>h;
cse.volume(r,h);
cout<<"\n enter radius r1 r2 r3 : ";
cin>>r1>>r2>>r3;
cse.volume(r1,r2,r3);
getch();
}

output:

enter the radius : 3.5

volume of sphere = 179.503339


volume of hemisphere = 89.75167

enter the radius and height : 4.7 9.5

volume of cylinder = 658.944641


volume of cone = 219.648223

enter radius r1 r2 r3 : 3.7 7.9 11.3


volume of ellipsoid = 1382.851917

Result:
Thus the program has been implemented for Function Overloading using C++.
Expt.No.7A PROGRAM FOR SINGLE INHERITANCE

Aim:
To write a program to implement Single Inheritance using C++.

Algorithm:

Step 1: define a class person with private members name, age and public member functions
readdata(), writedata().
Step 2: in function readdata(), read the name and age.
Step 3: in function writedata(), write the name and age.
Step 4: derive a class student from the class person with private members regnum, dept, marks
m1, m2, m3, m4, m5 and public member functions readdata(), writedata().
Step 5: in function readdata(), invoke the function person::readdata(), read the regnum, dept,
subject marks m1, m2, m3, m4, m5.
Step 6: in function writedata(),invoke the function person::writedata(), write the regnum, dept,
subject marks m1, m2, m3, m4, m5 and total.
Step 7: in function main(), create an object s for the class student. invoke the function
readdata() using s.readdata() and invoke the function writedata() using s.writedata().

Program:

#include<iostream.h>
#include<conio.h>

class person
{
private:
char name[20];
int age;
public:
void readdata()
{
cout<<"enter the name and age : \n";
cin>>name>>age;
}
void writedata()
{
cout<<"name : "<<name<<"\n";
cout<<"age : "<<age<<"\n";
}
};

class student : public person


{
private:
int regnum;
char dept[10];
int m1,m2,m3,m4,m5;
public:
void readdata()
{
person::readdata();
cout<<"enter the regnum and dept : \n";
cin>>regnum>>dept;
cout<<"enter the subject marks : \n";
cin>>m1>>m2>>m3>>m4>>m5;
}
void writedata()
{
person::writedata();
cout<<"register number : "<<regnum<<"\n";
cout<<"department : "<<dept<<"\n";
cout<<"subject marks : ";
cout<<m1<<"\t"<<m2<<"\t"<<m3<<"\t"<<m4<<"\t"<<m5;
cout<<"\ntotal = "<<m1+m2+m3+m4+m5;
}
};

void main()
{
student s;
clrscr();
cout<<"enter the student details \n";
s.readdata();
cout<<"student details \n";
s.writedata();
getch();
}
Output:

enter the student details

enter the name and age :


prithiviraj 18

enter the regnum and dept :


27 ece

enter the subject marks :


100 97 98 97 99

student details

name : prithiviraj
age : 18
register number : 27
department : ece
subject marks : 100 97 98 97 99
total = 491

Result:
Thus the program has been implemented for Single Inheritance using C++.
Expt.No.7B PROGRAM FOR MULTIPLE INHERITANCE

Aim:
To write a program to implement Multiple Inheritance using C++.

Algorithm:

Step 1: define a class person with private members name, age and public member functions
readdata(), writedata().
Step 2: in function readdata(), read the name and age.
Step 3: in function writedata(), write the name and age.
Step 4: define a class student with private members regnum, dept and public member functions
readdata(), writedata().
Step 5: in function readdata(), read the regnum, dept.
Step 6: in function writedata(), write the regnum, dept.
Step 7: derive a class exam from the base classes student, person with private data members
m1, m2, m3, m4, m5 and public member functions readdata(), writedata().
Step 8: in function readdata(), invoke the functions person::readdata(), student::readdata() and
read the subject marks m1, m2, m3, m4, m5.
Step 9: in function writedata(), invoke the functions person::writedata(), student::writedata(),
write the subject marks m1, m2, m3, m4, m5 and total.
Step 10: in function main(), create an object s for the class exam. invoke the function readdata()
using s.readdata() and invoke the function writedata() using s.writedata().

Program:

#include<iostream.h>
#include<conio.h>

class person
{
private:
char name[20];
int age;
public:
void readdata()
{
cout<<"enter the name and age : \n";
cin>>name>>age;
}
void writedata()
{
cout<<"name : "<<name<<"\n";
cout<<"age : "<<age<<"\n";
}
};

class student
{
private:
int regnum;
char dept[10];
public:
void readdata()
{
cout<<"enter the regnum and dept : \n";
cin>>regnum>>dept;
}
void writedata()
{
cout<<"register number : "<<regnum<<"\n";
cout<<"department : "<<dept<<"\n";
}
};

class exam : public person, public student


{
private:
int m1,m2,m3,m4,m5;
public:
void readdata()
{
person::readdata();
student::readdata();
cout<<"enter the subject marks : \n";
cin>>m1>>m2>>m3>>m4>>m5;
}
void writedata()
{
person::writedata();
student::writedata();
cout<<"subject marks : ";
cout<<m1<<"\t"<<m2<<"\t"<<m3<<"\t"<<m4<<"\t"<<m5;
cout<<"\ntotal = "<<m1+m2+m3+m4+m5;
}
};
void main()
{
clrscr();
exam s;
cout<<"enter the student details \n";
s.readdata();
cout<<"student details \n";
s.writedata();
getch();
}

Output:

enter the student details

enter the name and age :


vigneesh 18

enter the regnum and dept :


107 cse

enter the subject marks :


100 98 97 99 99

student details

name : vigneesh
age : 18
register number : 107
department : cse
subject marks : 100 98 97 99 99
total = 493

Result:
Thus the program has been implemented for Multiple Inheritance using C++.
Expt.No.7C PROGRAM FOR MULTILEVEL INHERITANCE

Aim:
To write a program to implement Multilevel Inheritance using C++.

Algorithm:

Step 1: define a class person with private members name, age and public member functions
readdata(), writedata().
Step 2: in function readdata(), read the name and age.
Step 3: in function writedata(), write the name and age.
Step 4: derive a class student from the class person with private members regnum, dept and
public member functions readdata(), writedata().
Step 5: in function readdata(), invoke the function person::readdata(), read the regnum, dept.
Step 6: in function writedata(),invoke the function person::writedata(), write the regnum, dept.
Step 7: derive a class exam from the derived class student with protected data members m1,
m2, m3, m4, m5 and public member functions readdata(), writedata().
Step 8: in function readdata(), invoke the function student::readdata(), read the subject marks m1,
m2, m3, m4, m5.
Step 9: in function writedata(), invoke the function student::writedata(), write the subject marks
m1, m2, m3, m4, m5 and total.
Step 10: in function main(), create an object s for the class exam. invoke the function readdata()
using s.readdata() and invoke the function writedata() using s.writedata().

Program:

#include<iostream.h>
#include<conio.h>
class person
{
private:
char name[20];
int age;
public:
void readdata()
{
cout<<"enter the name and age : \n";
cin>>name>>age;
}
void writedata()
{
cout<<"name : "<<name<<"\n";
cout<<"age : "<<age<<"\n";
}
};

class student:public person


{
private:
int regnum;
char dept[10];
public:
void readdata()
{
person::readdata();
cout<<"enter the regnum and dept : \n";
cin>>regnum>>dept;
}
void writedata()
{
person::writedata();
cout<<"register number : "<<regnum<<"\n";
cout<<"department : "<<dept<<"\n";
}
};

class exam:public student


{
protected:
int m1,m2,m3,m4,m5;
public:
void readdata()
{
student::readdata();
cout<<"enter the subject marks : \n";
cin>>m1>>m2>>m3>>m4>>m5;
}
void writedata()
{
student::writedata();
cout<<"subject marks : ";
cout<<m1<<"\t"<<m2<<"\t"<<m3<<"\t"<<m4<<"\t"<<m5;
cout<<"\ntotal="<<m1+m2+m3+m4+m5;
}
};
void main()
{
clrscr();
exam s;
cout<<"enter the exam student details\n";
s.readdata();
cout<<"student details\n";
s.writedata();
getch();
}

Output:

enter the exam student details

enter the name and age :


abishek 18

enter the regnum and dept :


3 cse

enter the subject marks :


100 98 97 99 99

student details

name : abishek
age : 18
register number : 3
department : cse
subject marks : 100 98 97 99 99
total=493

Result:
Thus the program has been implemented for Multilevel Inheritance using C++.
Expt.No.8 PROGRAM FOR DATA CONVERSION

Aim:
To write a program to implement Data Conversion using C++.

Algorithm:

Step 1: define a class dataconv with private variables float f,c, double a,b, and public member
functions void contocelcius(), void contointeger(), void contofloat().
Step 2: in function contointeger(), assign a=64.107 and write the double value of a. convert the
double value into integer by using b=int(a) and write the value of a as integer.
Step 3: in function contofloat(), assign a=107.158 and write the double value of a. convert the
double value into float by using b=(float)a and write the value of a as float.
Step 4: in function contocelcius(), read a value of farenheit degree. convert the farenheit value into
celcius by using c=(f-32)/1.8 and write the celcius value of farenheit.
Step 5: in function void main(), create an object dc for the class dataconv. invoke the function
contocelcius(), contointeger(), contofloat() by using dc.contocelcius(), dc.contointeger(),
dc.contofloat().

Program:

#include<iostream.h>
#include<conio.h>

class dataconv
{
private:
float f,c;
double a,b;
public:
void contocelcius();
void contointeger()
{
a=64.107;
cout<<"\n double value of a is : "<<a;
b=int(a);
cout<<"\n integer value of a is : "<<b;
}
void contofloat()
{
a=107.158;
cout<<"\n double value of a is : "<<a;
b=(float)a;
cout<<"\n float value of a is : "<<b;
}
};

void dataconv::contocelcius()
{
cout<<"\n enter a value of farenheit degree : ";
cin>>f;
c=(f-32)/1.8;
cout<<"the celcius value of farenheit "<<f<<" is : "<<c;
}

void main()
{
dataconv dc;
clrscr();
dc.contocelcius();
dc.contointeger();
dc.contofloat();
getch();
}

Output:

enter a value of farenheit degree : 107


the celcius value of farenheit 107 is : 41.666668

double value of a is : 64.107


integer value of a is : 64

double value of a is : 107.158


float value of a is : 107.158

Result:
Thus the program has been implemented for Data Conversion using C++.
Expt.No.9A PROGRAM USING FRIEND FUNCTION

Aim:
To write a program to implement Friend Function using C++.

Algorithm:

Step 1: define a class one with private variable float data1, and public member function
void setdata(float x), and declare a function friend float mean(one a,two b).
Step 2: in function setdata(float x), assign the value of x into data1.
Step 3: define a class two with private variable float data2, and public member function
void setdata(float y), and declare a function friend float mean(one a,two b).
Step 4: in function setdata(float y), assign the value of y into data2.
Step 5: in function mean(one a,two b), calculate the mean (a.data1+b.data2)/2.
Step 6: in function main(), create objects a,b for the classes one,two respectively. read the
values of x,y. invoke the functions setdata(float x), setdata(float y) by using a.setdata(x),
b.setdata(y) respectively. write the values of x and y. invoke the function
mean(one a,two b) by using mean(a,b). write the mean of x and y.

Program:

#include<iostream.h>
#include<conio.h>

class two;
class one
{
private:
float data1;
public:
void setdata(float x)
{
data1=x;
}
friend float mean(one a,two b);
};

class two
{
private:
float data2;
public:
void setdata(float y)
{
data2=y;
}
friend float mean(one a,two b);
};

float mean(one a,two b)


{
return (a.data1+b.data2)/2;
}

void main()
{
one a;
two b;
clrscr();
float x,y;
cout<<"enter the values of x,y : ";
cin>>x>>y;
a.setdata(x);
b.setdata(y);
cout<<"\n x = "<<x<<"\n y = "<<y;
cout<<"\n mean of x and y is : "<<mean(a,b);
getch();
}

Output:

enter the values of x,y : 45 80

x = 45
y = 80
mean of x and y is : 62.5

Result:
Thus the program has been implemented for Friend Function using C++.
Expt.No.9B PROGRAM USING FRIEND CLASSES

Aim:
To write a program to implement Friend Classes using C++.

Algorithm:

Step 1: define a class readint with private variables float a,b and public member function
void read(). declare a class sum as friend class sum.
Step 2: in function void read(), read and write tha values of a and b.
Step 3: define a class sum with private variable c, and public member function
void add(readint rd).
Step 4: in function void add(readint rd), calculate the sum of two numbers c=rd.a+rd.b, write the
sum of two numbers a and b.
Step 5: in function main(), create objects rd,s for the classes readint,sum respectively. invoke
the function read() by using rd.read(). invoke the function void add(readint rd) by using
s.add(rd).

Program:

#include<iostream.h>
#include<conio.h>
class readint
{
float a,b;
public:
void read()
{
cout<<"\n enter the value of a : ";
cin>>a;
cout<<"\n enter the value of b : ";
cin>>b;
cout<<"\n a = "<<a<<"\n b = "<<b;
}
friend class sum;
};

class sum
{
float c;
public:
void add(readint rd)
{
c=rd.a+rd.b;
cout<<"\n sum of two numbers a and b is : "<<c;
}
};

void main()
{
readint rd;
sum s;
clrscr();
rd.read();
s.add(rd);
getch();
}

Output:

enter the value of a : 25


enter the value of b : 34

a = 25
b = 34
sum of two numbers a and b is : 59

Result:
Thus the program has been implemented for Friend Classes using C++.
Expt.No.10A PROGRAM USING VIRTUAL FUNCTION

Aim:
To write a program to implement Virtual Function using C++.

Algorithm:

Step 1: define a class base with private variable int n, and public member functions
virtual void show(), virtual void square(int n).
Step 2: in function virtual void show(), write hi cse-a...
Step 3: in function virtual void square(int n), calculate the square of the given number using
pow(n,2) and write the square of the number.
Step 4: derive a class derived1 from the class base with public member function
virtual void show().
Step 5: in function virtual void show(), write hi cse-b...
Step 6: derive a class derived2 from the class base with public member function
virtual void square(int n).
Step 7: in function virtual void square(int n), calculate the square of the given number using
pow(n,2) and write the square of the number.
Step 8: in function main(), create an object obj for the class base. create a pointer *p for the
class base. assign the objects address into p. invoke the function show() by using
p->show(). read the value of x. invoke the function square() by using p->square(x).
Step 9: create an object obj1 for the class derived1. assign the objects address into p. invoke
the function show() by using p->show(). read the value of y. invoke the function square()
by using p->square(y).
Step 10: create an object obj2 for the class derived2. assign the objects address into p. invoke
the function show() by using p->show(). read the value of z. invoke the function square()
by using p->square(z).

Program:

#include<iostream.h>
#include<conio.h>
#include<math.h>

class base
{
private:
int n;
public:
virtual void show()
{
cout<<"\n hi cse-a...";
}
virtual void square(int n)
{
cout<<"square of the given number is : "<<pow(n,2)<<endl;
}
};

class derived1:public base


{
public:
virtual void show()
{
cout<<"\n hi cse-b...";
}
};

class derived2:public base


{
public:
virtual void square(int n)
{
cout<<"\n square of the given number is : "<<pow(n,2)<<endl;
}
};

void main()
{
base obj;
base *p;
clrscr();
int x,y,z;

p=&obj;
p->show();
cout<<"\n enter the value of x : ";
cin>>x;
p->square(x);

derived1 obj1;
p=&obj1;
p->show();
cout<<"\n enter the value of y : ";
cin>>y;
p->square(y);

derived2 obj2;
p=&obj2;
p->show();
cout<<"\n enter the value of z : ";
cin>>z;
p->square(z);

getch();
}

Output:

hi cse-a...
enter the value of x : 13
square of the given number is : 169

hi cse-b...
enter the value of y : 34
square of the given number is : 1156

hi cse-a...
enter the value of z : 16
square of the given number is : 256

Result:
Thus the program has been implemented for Virtual Function using C++.
Expt.No.10B PROGRAM FOR VIRTUAL BASE CLASS

Aim:
To write a program to implement Virtual Base Class using C++.

Algorithm:

Step 1: define a class student with private variables char name[50], dept[5], int rollno, and
public member functions void getdata(), void putdata().
Step 2: in function void getdata(), read the student name,roll number,and department.
Step 3: in function void putdata(), write the student name,roll number,and department.
Step 4: derive a class internalexam from the class student. the class student is the virtual
base class. declare private variables m1,m2,m3,m4,m5, and public member functions
void readdata(), void writedata(), int totalint().
Step 5: in function void readdata(), read the students internal marks m1,m2,m3,m4,m5.
Step 6: in function void writedata(), write the students internal marks m1,m2,m3,m4,m5. invoke
the function totalint(). write total internal marks.
Step 7: in function totalint(), calculate total internal marks m1+m2+m3+m4+m5.
Step 8: derive a class externalexam from the class student. the class student is the virtual
base class. declare private variables m1,m2,m3,m4,m5, and public member functions
void readdata(), void writedata(), int totalext().
Step 9: in function void readdata(), read the students external marks m1,m2,m3,m4,m5.
Step 10: in function void writedata(), write the students external marks m1,m2,m3,m4,m5. invoke
the function totalext(). write total external marks.
Step 11: in function totalext(), calculate total external marks m1+m2+m3+m4+m5.
Step 12: derive the class result from the base classes internalexam,externalexam. declare the
variable total as integer, and public member function int totalmarks().
Step 13: in function totalmarks(), calculate the total marks totalint()+totalext().
Step 14: in function main(), create an object s for the class result. invoke the function getdata()
using s.getdata(). invoke the functions readdata() by using s.internalexam::readdata() and
s.externalexam::readdata().
Step 15: invoke the function putdata() using s.putdata(). invoke the functions writedata() by using
s.internalexam::writedata() and s.externalexam::writedata(). invoke the function
totalmarks() using s.totalmarks().

Program:

#include<iostream.h>
#include<conio.h>

class student
{
private:
char name[50];
int rollno;
char dept[5];
public:
void getdata()
{
cout<<"enter the student name,roll number,department : ";
cin>>name>>rollno>>dept;
}
void putdata()
{
cout<<"\n name : "<<name;
cout<<"\n roll number : "<<rollno;
cout<<"\n department : "<<dept;
}
};

class internalexam:virtual public student


{
private:
int m1,m2,m3,m4,m5;
public:
void readdata()
{
cout<<"\n enter the student internal marks : ";
cin>>m1>>m2>>m3>>m4>>m5;
}
void writedata()
{
cout<<"\n internal marks : ";
cout<<m1<<"\t"<<m2<<"\t"<<m3<<"\t"<<m4<<"\t"<<m5<<"\t";
cout<<"\n total internal marks : "<<totalint();
}
int totalint()
{
return m1+m2+m3+m4+m5;
}
};

class externalexam:virtual public student


{
private:
int m1,m2,m3,m4,m5;
public:
void readdata()
{
cout<<"\n enter the student external marks : ";
cin>>m1>>m2>>m3>>m4>>m5;
}
void writedata()
{
cout<<"\n external marks :";
cout<<m1<<"\t"<<m2<<"\t"<<m3<<"\t"<<m4<<"\t"<<m5<<"\t";
cout<<"\n total external marks : "<<totalext();
}
int totalext()
{
return m1+m2+m3+m4+m5;
}
};

class result:public internalexam,public externalexam


{
private:
int total;
public:
int totalmarks()
{
return totalint()+totalext();
}
};

void main()
{
result s;
clrscr();
cout<<"enter student details : \n";
s.getdata();
cout<<"\n enter internal marks : ";
s.internalexam::readdata();
cout<<"\n enter external marks : ";
s.externalexam::readdata();
cout<<"\n student details: \n";
s.putdata();
s.internalexam::writedata();
s.externalexam::writedata();
cout<<"\n total marks : "<<s.totalmarks();
getch();
}

Output:

enter student details :


enter the student name,roll number,department : abishek 3 cse

enter internal marks :


enter the student internal marks : 97 98 95 97 100

enter external marks :


enter the student external marks : 100 95 98 95 96

student details:

name : abishek
roll number : 3
department : cse
internal marks : 97 98 95 97 100
total internal marks : 487
external marks :100 95 98 95 96
total external marks : 484
total marks : 971

Result:
Thus the program has been implemented for Virtual Base Class using C++.
Expt.No.11A PROGRAM USING FUNCTION TEMPLATE

Aim:
To write a program to implement Function Template using C++.

Algorithm:

Step 1: define a function template, template < class T>


Step 2: define a function swap(), by using template such as void swap(T &x, T &y) and swap the
values of x and y.
Step 3: in function main(), read the values of a,b as integer, and write the values of a,b before
swapping.
Step 4: call the function swap(a, b), and write the values of a, b after swapping.
Step 5: read the values of c, d as float, and write the values of a, b before swapping.
Step 6: call the function swap(c, d), and write the values of c, d after swapping.

Program:

#include<iostream.h>
#include<conio.h>

template <class T>


void swap (T&x,T&y)
{
T t;
t=x;
x=y;
y=t;
}

void main()
{
clrscr();
int a,b;
cout<<"enter the values of a,b(integer): ";
cin>>a>>b;
cout<<"\nbefore swapping \n";
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
swap(a,b);
cout<<"\nafter swapping \n";
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
float c,d;
cout<<"enter the values of c,d(float): ";
cin>>c>>d;
cout<<"\nbefore swapping \n";
cout<<"c="<<c<<endl;
cout<<"d="<<d<<endl;
swap(c,d);
cout<<"\nafter swapping \n";
cout<<"c="<<c<<endl;
cout<<"d="<<d<<endl;
getch();
}

Output:

enter the values of a,b(integer): 13 38

before swapping
a=13
b=38

after swapping
a=38
b=13

enter the values of c,d(float): 13.31 38.83

before swapping
c=13.31
d=38.83

after swapping
c=38.83
d=13.31

Result:
Thus the program has been implemented for Function Template using C++.
Expt.No.11B PROGRAM USING CLASS TEMPLATE

Aim:
To write a program to implement Class Template using C++.

Algorithm:

Step 1: create a class template, template <class T>


Step 2: define a class complex with private members T real, T imag, and public member
functions input(), output(), operator+(complex). create a constructor complex() and assign
the values real=imag=0.0.
Step 3: in function input(), read the real and imag value of complex number.
Step 4: in function output(), write the complex number after addition.
Step 5: in function operator+(complex <T> c2), add c2.real value with c1.real value, and add
c2.imag value with c1.imag value. Template <T> is used to add different data types. here
the operator + is used to add the two numbers.
Step 6: in function main(), create objects c1, c2, c3 for the class complex with data type as int.
Step 7: invoke the function input(), with c1.input(), c2.input().
Step 8: invoke the function operator+(complex <T> c2), using c3=c1+c2. here the operator + is
used to invoke the function.
Step 9: invoke the function output() with c3.output().
Step 10: create objects f1, f2, f3 for class complex with data type as float.
Step 11: invoke the function input(), with f1.input(), f2.input().
Step 12: invoke the function operator+(complex <T> f2), using f3=f1+f2. here the operator + is
used to invoke the function.
Step 13: invoke the function output() with f3.output().

Program:

#include<iostream.h>
#include<conio.h>

template <class T>

class complex
{
private:
T real;
T imag;
public:
complex()
{
real=imag=0.0;
}
void input()
{
cout<<"\n enter the real and imag part: ";
cin>>real>>imag;
}
complex operator+(complex);
void output()
{
cout<<"\n the sum of two complex number is: ";
cout<<real<<"+i"<<imag;
}
};

template <class T>


complex<T> complex<T>::operator+(complex<T>c2)
{
complex<T>temp;
temp.real=real+c2.real;
temp.imag=imag+c2.imag;
return(temp);
}

void main()
{
clrscr();
complex<int> c1,c2,c3;
cout<<"\n enter the complex number1 (int) :";
c1.input();
cout<<"enter the complex number2 (int) : ";
c2.input();
c3=c1+c2;
c3.output();
complex<float>f1,f2,f3;
cout<<"\nenter the complex number1 (float) : ";
f1.input();
cout<<"\nenter the complex number2 (float) : ";
f2.input();
f3=f1+f2;
f3.output();
getch();
}
Output:

enter the complex number1 (int) :


enter the real and imag part: 5 6

enter the complex number2 (int) :


enter the real and imag part: 4 5

the sum of two complex number is: 9+i11

enter the complex number1 (float) :


enter the real and imag part: 1.2 2.3

enter the complex number2 (float) :


enter the real and imag part: 4.5 3.6

the sum of two complex number is: 5.7+i5.9

Result:
Thus the program has been implemented for Class Template using C++.
Expt.No.12 PROGRAM FOR FILE HANDLING

Aim:
To write a program to implement File Handling using C++.

Algorithm:

Step 1: define a class files with public member function void fileopr().
Step 2: in function void fileopr(), open the files abc.txt, xyz.txt using ifstream, ofstream
respectively.
Step 3: is input file is not available, then write failed to open input file.
Step 4: read the input file until it gets end. increment the line count. write the content of the input
file along with line number in output file.
Step 5: close the input file and output file. write the total number of lines.
Step 6: in function main(), create an object f for the class files. invoke the function fileopr() by
using f.fileopr().

Program:

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<fstream.h>

class files
{
public:
void fileopr()
{
char myline[256];
int lc=0;
ofstream outfile("xyz.txt",ios::app);
ifstream infile("abc.txt");
if(!infile)
{
cerr<<"failed to open input file...";
exit(1);
}
while(1)
{
infile.getline(myline,256);
if(infile.eof())
break;
lc++;
outfile<<lc<<" : "<<myline<<endl;
}
infile.close();
outfile.close();
cout<<"total number of lines in input file : "<<lc<<endl;
getch();
}
};

void main()
{
files f;
clrscr();
f.fileopr();
getch();
}

Output:

input file:

abc.txt

hi
cse...
object
oriented
programming
lab
regulation 2015
cse-a section

output:

total number of lines in input file : 7


output file:

xyz.txt

1 : hi
2 : cse...
3 : object
4 : oriented
5 : programming
6 : lab
7 : regulation 2015

Result:
Thus the program has been implemented for File Handling using C++.
Expt.No.13 PROGRAM FOR EXCEPTION HANDLING

Aim:
To write a program to implement Exception Handling using C++.

Algorithm:

Step 1: define a class exception with private variables a,b,c,d, and public member function
void exchandle().
Step 2: in function void exchandle(), read the values of a,b,c.
Step 3: in try block, check the value of a-b is not equal to 0. is true, calculate d=c/(a-b). write the
value of d. is not, throw the object (a-b) using throw(a-b).
Step 4: in catch block, receive the exception object and write the result.
Step 5: in function main(), create an object eh for the class exception. invoke the function
exchandle() by using eh.exchandle().

Program:

#include<iostream.h>
#include<conio.h>

class exception
{
private:
int a,b,c,d;
public:
void exchandle()
{
cout<<"enter the values of a, b, c : ";
cin>>a>>b>>c;
try
{
if((a-b)!=0)
{
d=c/(a-b);
cout<<"result is : "<<d;
}
else
{
throw(a-b);
}
}
catch(int i)
{
cout<<"result is infinite because a-b is : "<<i;
}
getch();
}
};

void main()
{
exception eh;
eh.exchandle();
}

Output:

enter the values of a, b, c :34 16 107


result is : 5

enter the values of a, b, c :64 64 158


result is infinite because a-b is : 0

Result:
Thus the program has been implemented for Exception Handling using C++.
Expt.No.13B PROGRAM FOR EXCEPTION HANDLING WITH MULTIPLE CATCH

Aim:
To write a program to implement Exception Handling with Multiple Catch using C++.

Algorithm:

Step 1: define a class mulcatch with public member function void test(int x).
Step 2: in function void test(int x), check the value of x is greater than 0 in try block. is true, throw
the object x as interger. is not, throw the object x as character.
Step 3: in catch block, catch(int x), catch and write the integer x.
Step 4: in catch block, catch(char x), catch and write the charecter x.
Step 5: in function main(), create an object mc for the class mulcatch. invoke the function
test(int x) by using mc.test(10), mc.test(0).

Program:

#include<iostream.h>
#include<conio.h>

class mulcatch
{
public:
void test(int x)
{
try
{
if(x>0)
throw x;
else
throw 'x';
}
catch(int x)
{
cout<<"\n catch a integer and that integer is : "<<x;
}
catch(char x)
{
cout<<"\n catch a character and that character is : "<<x;
}
}
};

void main()
{
mulcatch mc;
mc.test(10);
mc.test(0);
getch();
}

Output:

catch a integer and that integer is : 10

catch a character and that character is : x

Result:
Thus the program has been implemented for Exception Handling with Multiple Catch using
C++.
215ESP01 OBJECT ORIENTED
PROGRAMMING
LAB MANUAL

REGULATION 2015

Das könnte Ihnen auch gefallen