1. INDEX
S.NO DATE TOPIC PAGE
NO
STAFF
SIGNATURE
1 DISTANCE CONVERSION
PROBLEM
1
2 OVERLOADING OBJECTS 10
3 OVERLOADING
CONVERSIONS
16
4 OVRELOADING MATRIX 22
5 AREA COMPUTATION
USING DERIVED CLASS
32
6 VECTOR PROBLEM 37
7 INHERITANCE 43
2. EX.NO:1
DATE:
DISTANCE CONVERSION PROBLEM
AIM:
Create two classes DM and DB which store the value of distances. DM
store the value ofdistances. DM stores distances in meters and centimeters in
DB in feet and inches. Write a Programthat can create the values of the class
objects and add one object DM with another object DB.
Use a friend function to carry out addition operation. The object that
stores the result may be DM object or DB objectdepending on the units in
which results are required.
The display should be in the order of meter and centimeter and feet or
inches depending on the order of display.
ALGORITHM:
STEP 1:
Start the program execution.
STEP 2:
To create a class metric and british.
STEP 3:
To create a function input() and display().
STEP 4:
Accept the feet,inches value of british.
STEP 5:
Accept the meter ,cm,value of metric.
STEP 6:
To convert metric value to british value.
3. STEP 7:
To convert british value to metric value.
STEP 8:
To display the metric values and british values.
STEP 9:
Stop the program execution.
CODING:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class metric;
class british
{
public:
int feet,inches;
void input();
friend british addbritish(british & b,metric & m);
void display();
};
class metric
{
public:
int meter,cm;
4. void input();
friend metric addmetric(metric & m,british & b);
void display();
};
void british::input()
{
cout<<"nt enter the value of feet:";
cin>>feet;
cout<<"nt enter the value of inches:";
cin>>inches;
}
british addbritish(british & b,metric & m)
{
british b1;
int ftin,tmcm,tin,mtcm,tm,cm,tmcmtin,tot;
ftin=(b.feet)*12;
tin=ftin+b.inches;
mtcm=(m.meter)*100;
tmcm=mtcm+m.cm;
tmcmtin=tmcm/2.5;
tot=tin+tmcmtin;
b1.feet=tot/12;
b1.inches=tot%12;
return(b1);
}
5. void british::display()
{
cout<<"nt the feet value is:t"<<feet;
cout<<"nt the inches value is:t"<<inches;
}
void metric::input()
{
cout<<"nt enter the value of meter:t";
cin>>meter;
cout<<"nt enter the value of cm:t";
cin>>cm;
}
metric addmetric(metric & m,british & b)
{
metric m1;
int tin,ftin,mtcm,tincmt,tot;
ftin=(b.feet)*12;
tincmt=(ftin+b.inches)*2.5;
mtcm=(m.meter)*100;
tot=tincmt+mtcm+m.cm;
m1.meter=tot/100;
m1.cm=tot%100;
return(m1);
}
void metric::display()
6. {
cout<<"nt the meter value is t"<<meter;
cout<<"nt the centimeter value is t"<<cm;
}
void main()
{
int op;
clrscr();
british b1,b2;
metric m1,m2;
cout<<"nDistance conversion";
b1.input();
m1.input();
do
{
clrscr();
cout<<"nMENU";
cout<<"n1.Metric to British";
cout<<"n2.British to Metric";
cout<<"n3.Exit";
cout<<"nEnter the choice:";
cin>>op;
switch(op)
{
case 1:
7. cout<<"nt metric to british";
cout<<"nt output";
b1.display();
m1.display();
b2=addbritish(b1,m1);
cout<<"nt output";
b2.display();
break;
case 2:
cout<<"nt british to metric";
cout<<"nt output";
b1.display();
m1.display();
m2=addmetric(m1,b1);
cout<<"noutput";
m2.display();
break;
case 3:
cout<<"nexit";
break;
}
getch();
}
while (op!=3);
}
8. OUTPUT
Distance conversion
enter the value of feet: 10
enter the value of inches: 6
enter the value of meter: 4
enter the value of cm: 2
MENU
1.Metric to British
2.Britishto Metric
3.Exit
Enter the choice:1
metric to british
output
the feetvalue is: 10
the inches value is: 6
the meter value is 4
the centimetervalue is 2
output
the feetvalue is: 23
the inches value is: 10
MENU
1.Metric to British
9. 2.Britishto Metric
3.Exit
Enter the choice:2
british to metric
output
the feetvalue is: 10
the inches value is: 6
the meter value is 4
the centimetervalue is 2
output
the meter value is 7
the centimetervalue is 17
MENU
1.Metric to British
2.Britishto Metric
3.Exit
Enter the choice:3
Exit
RESULT
10. EX.NO:2
DATE:
OVERLOADING OBJECTS
AIM:
Create a class FLOAT that contains one float data member overload all
the four arithmetic operators so that operate on the objects of FLOAT.
ALGORITHM :
STEP 1:
Start the program execution
STEP 2:
To create a class FLOAT
STEP 3:
Accept the input values
STEP 4:
Overloadthe arithmetic operator+
STEP 5:
Overloadthe arithmetic operetor–
STEP 6:
Overloadthe arithmetic operator*
STEP 7:
Overloadthe arithmetic operator/
11. STEP 8:
To display the output values.
STEP 9:
Stop the program execution.
CODING:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class FLOAT
{
float x,D;
public:
FLOAT()
{
}
FLOAT(float a)
{
x=a;
}
FLOAT operator+(FLOAT);
FLOAT operator-(FLOAT);
FLOAT operator*(FLOAT);
14. cout<<"nnt ADDITION VALUE IS (o1+o2)=";
ADD.outdata();
cout<<"nnt SUBTRACTION VALUE IS (o1-o2)=";
SUB.outdata();
cout<<"nnt MULTIPLICATION VALUE IS (o1*o2)=";
MUL.outdata();
cout<<"nnt DIVISION VALUE IS (o1/o2)=";
DIV.outdata();
getch();
return(0);
}
15. 2.OUTPUT:
enter the value:4
enter the value:4
o1:4
o2:4
ADDITION VALUE IS (o1+o2)=8
SUBTRACTION VALUE IS (o1-o2)=0
MULTIPLICATION VALUE IS (o1*o2)=16
DIVISION VALUE IS (o1/o2)=1
RESULT:
16. EX.NO:3
DATE:
OVERLOADING CONVERSIONS:
AIM:
Designa class polarwhich describes a point in a plane using polar
Co-ordinates radius and angle. A point in polar Co-ordinates is as shown
below.
Use the overloader+ operatorto add two objects of polar. Note that we
cannot add polar values of two points directly. This requires first the
conversion.
Points into rectangular Co-ordinates and finally converting the result
into polar Co-ordinates.
You need to use following trigonometric formulas.
X= r * cos (a); Y= r * sin (a);
a= sqrt (X * X +Y * Y);
ALGORITHM :
STEP 1:
Start the program execution.
STEP 2:
To create class polar.
STEP 3:
Accept the radius and angle values.
STEP 4:
First we acceptthe radius and angle value is 0.
17. STEP 5:
To create arguments in polar.
STEP 6:
To overload the + operatorto add two object of polar.
STEP 7:
To display the output values.
STEP 8:
Stop the program execution.
CODING:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class polar
{
double radius,angle;
public:
polar()
{
radius=0;
angle=0;
}
19. cout<<"noutputn";
cout<<"*****n";
cout<<"nn radius 1="<<x1<<"tangle 1="<<y1;
cout<<"nn radius 2="<<x2<<"tangle 2="<<y2;
x=x1+x2;
y=y1=y2;
cout<<"nn sum of the radius in rectangularcoordinates is:";
cout<<x;
cout<<"nn sum of the angle in rectangularcoordinates is:";
cout<<y;
p2.radius=sqrt(x*x+y*y);
p2.angle=atan(y/x)*(180/3.14);
return(p2);
}
void main()
{
clrscr();
cout.precision(2);
polar o1,o2,o3;
double r1,r2,a1,a2;
char c;
do
{
20. cout<<"nenter the radius 1 and angle 1:";
cin>>r1>>a1;
cout<<"nenter the radius 2 and angle 2:";
cin>>r2>>a2;
o1=polar(r1,a1);
o2=polar(r2,a2);
o3=o1+o2;
o3.disp();
cout<<"nn Do you want to continue(y/n):";
cin>>c;
}
while(c=='y'||c!='n');
getch();
}
21. 3.OUTPUT
enter the radius 1 and angle 1:
11
13
enter the radius 2 and angle 2:
13
15
output
*****
radius 1=10.72 angle 1=2.47
radius 2=12.56 angle 2=3.36
sum of the radius in rectangularcoordinates is:23.28
sum of the angle in rectangularcoordinates is:3.36
radius value is:23.52
angle value is:8.23
Do you want to continue(y/n):n
RESULT:
22. EX.NO:4
DATE:
OVRELOADING MATRIX:
AIM:
Create a class MAT of size M*N. Define all possible matrix operations
for MAT type objects. Verify the identity.
(A-B)^2 = A^2+B^2 –2*A*B
ALGORITHM :
STEP 1:
Start the program execution.
STEP 2:
To create a class MAT.
STEP 3:
To acceptthe elementof matrices.
STEP 4:
To create a function inp(int) and disp().
STEP 5:
To overload the operatorthe operators +,-,*,/,^.
STEP 6:
To using for condition.
23. STEP 7:
To acceptthe elementof first and secondmatrix.
STEP 8:
To calculate (A-B)^2.
STEP 9:
To calculate A^2+B^2-2*A*B.
STEP 10:
To verify (A-B)^2=A^2+B^2-2AB the identity is true using it condition.
STEP 11:
To verify (A-B)^2!=A^2+B^2-2AB the given identity is false.
STEP 12:
To create a output values.
STEP 13:
Stop the program execution.
CODING:
#include<iostream.h>
#include<conio.h>
#include<math.h>
constsize=3;
class mat
{
int nrc;
24. int a[size][size];
public:
void inp(int);
void disp();
mat operator+(mat);
mat operator-(mat);
mat operator*(mat);
mat operator*(int);
mat operator^(int);
int operator==(mat);
};
void mat::inp(int n)
{
nrc=n;
for(int i=0;i<nrc;i++)
for(int j=0;j<nrc;j++)
cin>>a[i][j];
}
void mat::disp()
{
for(int i=0;i<nrc;i++)
{
for(int j=0;j<nrc;j++)
25. cout<<a[i][j]<<" ";
cout<<endl;
}
}
mat mat::operator+(mat m)
{
mat temp;
temp.nrc=nrc;
for(int i=0;i<nrc;i++)
for(int j=0;j<nrc;j++)
temp.a[i][j]=a[i][j]+m.a[i][j];
return temp;
}
mat mat::operator-(mat m)
{
mat temp;
temp.nrc=nrc;
for(int i=0;i<nrc;i++)
for(int j=0;j<nrc;j++)
temp.a[i][j]=a[i][j]-m.a[i][j];
return temp;
}
mat mat::operator*(mat m)
26. {
mat temp;
temp.nrc=nrc;
for(int i=0;i<nrc;i++)
for(int j=0;j<nrc;j++)
{
temp.a[i][j]=0;
for(int k=0;k<nrc;k++)
temp.a[i][j]+=a[i][j]*m.a[i][j];
}
return temp;
}
mat mat::operator*(int x)
{
mat temp;
temp.nrc=nrc;
for(int i=0;i<nrc;i++)
for(int j=0;j<nrc;j++)
temp.a[i][j]=x*a[i][j];
return temp;
}
mat mat::operator^(int)
{
27. mat temp;
temp.nrc=nrc;
for(int i=0;i<nrc;i++)
for(int j=0;j<nrc;j++)
{
temp.a[i][j]=0;
for(int k=0;k<nrc;k++)
temp.a[i][j]+=a[i][j]*a[i][j];
}
return temp;
}
int mat::operator==(mat m)
{
int t=1;
int f=0;
for(int i=0;i<nrc;i++)
for(int j=0;j<nrc;j++)
if(a[i][j]!=m.a[i][j])
return f;
else
return t;
return 0;
}
28. void main()
{
clrscr();
mat m1,m2,mat1,mat2;
int n;
cout<<"nenter the order of matrices:";
cin>>n;
cout<<"nenter the element of first matrix:";
m1.inp(n);
cout<<"nenter the element of secondmatrix:";
m2.inp(n);
cout<<"nfirst matrix is:";
m1.disp();
cout<<"nsecondmatrix is:";
m2.disp();
mat1=(m1-m2)^2;
cout<<"nresult of (A-B)^2 is:n";
mat1.disp();
mat2=(m1^2)+(m2^2)-((m1*m2)*2);
cout<<"nresult of (A^2+B^2-2AB)is:n";
mat2.disp();
if(mat1==mat2)
cout<<"nthe given identity is true:n";
30. 4.OUTPUT:
enter the order of matrices:2
enter the element of first matrix:
1
1
1
1
enter the element of secondmatrix:
1
1
1
1
first matrix is:
1 1
1 1
secondmatrix is:
1 1
1 1
result of (A-B)^2 is:
0 0
0 0
32. EX.NO:5
DATE:
AREA COMPUTATION USING DERIVED CLASS
AIM:
Area of rectangle = X*Y
Area of triangle = ½ * X * Y
ALGORITHM :
STEP 1:
Start the program execution.
STEP 2:
To create a class rectangle andtriangle.
STEP 3:
Create a function get(),cal()and disp().
STEP 4:
Accept the two sides of rectangles and triangle.
STEP 5:
Calculate the area of rectangle are=X*Y.
STEP 5:
Calculate the area of triangle are =1/2*X*Y.
STEP 6:
Display the outputs.
36. 5.OUTPUT:
enter the sides of rectangle and triangle:
4
5
area of the rectangle is:20
area of the triangle is:10
RESULT:
37. EX.NO:6
DATE:
VECTOR PROBLEM:
AIM:
Define a class forvector containing scalarvalues. Apply overloading
concepts for vectoraddition, Multiplication of a vectorby a scalarquantity,
replace the values in a position vector.
ALGORITHM
STEP 1:
Start the program execution.
STEP 2:
To create a class vector.
STEP 3:
To acceptthe input values.
STEP 4:
Overloading the *,+,<<,>>operators.
STEP 5:
To multiply the vector values.
STEP 6:
To calculate the sum of two vectors.
38. STEP 7:
To change the position of elements to be modified vector.
STEP 8:
To acceptthe input values.
STEP 9:
To display the output values.
STEP 10:
Stop the program execution.
CODING:
#include<iostream.h>
#include<conio.h>
#include<math.h>
constsize=3;
class vector
{
public:
int *v;
vector()
{
v=new int[size];
}
int & operator[](int);
42. 6.OUTPUT
enter the element of vector 1:
5
6
4
enter the element of vector 2:
4
8
6
output
****************
vector1:5,6,4
vector2:4,8,6
scalarmultiplication value:3
scalarmultiplication of vector1 is: 15,18,12
sum of the two vectoris : 9,14,10
enter the position of the element to be modified (vector):2
enter the value:5
modified value is:5,5,4
RESULT:
43. EX.NO:7
DATE:
INHERITANCE
AIM:
Create three classes alpha, beta and gamma, eachcontaining one data
member. The class gamma should be inherited from both alpha and beta. Use
a constructorfunction in the class gamma to assignvalues to the data
members of all the classes.
ALGORITHM:
STEP 1:
Start the program execution.
STEP 2:
To create a three classes alpha, beta and gamma
STEP 3:
To acceptthe input values.
STEP 4:
TO use constructorfunction
STEP 5:
To acceptthe input values.
STEP 6:
To display the output values.
STEP 7:
Stop the program execution.