Professional Documents
Culture Documents
1 DISTANCE CONVERSION 1
PROBLEM
2 OVERLOADING OBJECTS 10
3 OVERLOADING 16
CONVERSIONS
4 OVRELOADING MATRIX 22
5 AREA COMPUTATION 32
USING DERIVED CLASS
6 VECTOR PROBLEM 37
7 INHERITANCE 43
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 Program that 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 object depending 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:
STEP 7:
STEP 8:
STEP 9:
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;
void input();
friend metric addmetric(metric & m,british & b);
void display();
};
void british::input()
{
cout<<"\n\t enter the value of feet:";
cin>>feet;
cout<<"\n\t 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);
}
void british::display()
{
cout<<"\n\t the feet value is:\t"<<feet;
cout<<"\n\t the inches value is:\t"<<inches;
}
void metric::input()
{
cout<<"\n\t enter the value of meter:\t";
cin>>meter;
cout<<"\n\t 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()
{
cout<<"\n\t the meter value is \t"<<meter;
cout<<"\n\t the centimeter value is \t"<<cm;
}
void main()
{
int op;
clrscr();
british b1,b2;
metric m1,m2;
cout<<"\n Distance conversion";
b1.input();
m1.input();
do
{
clrscr();
cout<<"\n MENU";
cout<<"\n 1.Metric to British";
cout<<"\n 2.British to Metric";
cout<<"\n 3.Exit";
cout<<"\n Enter the choice:";
cin>>op;
switch(op)
{
case 1:
cout<<"\n\t metric to british";
cout<<"\n\t output";
b1.display();
m1.display();
b2=addbritish(b1,m1);
cout<<"\n\t output";
b2.display();
break;
case 2:
cout<<"\n\t british to metric";
cout<<"\n\t output";
b1.display();
m1.display();
m2=addmetric(m1,b1);
cout<<"\n output";
m2.display();
break;
case 3:
cout<<"\n exit";
break;
}
getch();
}
while (op!=3);
}
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.British to Metric
3.Exit
Enter the choice:1
metric to british
output
the feet value is: 10
the inches value is: 6
the meter value is 4
the centimeter value is 2
output
the feet value is: 23
the inches value is: 10
MENU
1.Metric to British
2.British to Metric
3.Exit
Enter the choice:2
british to metric
output
the feet value is: 10
the inches value is: 6
the meter value is 4
the centimeter value is 2
output
the meter value is 7
the centimeter value is 17
MENU
1.Metric to British
2.British to Metric
3.Exit
Enter the choice:3
Exit
RESULT
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:
STEP 2:
STEP 3:
STEP 4:
STEP 5:
STEP 7:
STEP 8:
STEP 9:
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);
FLOAT operator/(FLOAT);
void outdata();
void indata()
{
cout<<"\n\n\t enter the value:";
cin>>x;
}
};
FLOAT FLOAT::operator+(FLOAT D)
{
FLOAT temp;
temp.x=x+D.x;
return(temp);
}
FLOAT FLOAT::operator-(FLOAT D)
{
FLOAT temp;
temp.x=x-D.x;
return(temp);
}
FLOAT FLOAT::operator*(FLOAT D)
{
FLOAT temp;
temp.x=x*D.x;
return(temp);
}
FLOAT FLOAT::operator/(FLOAT D)
{
FLOAT temp;
temp.x=x/D.x;
return(temp);
}
void FLOAT::outdata(void)
{
cout<<x<<endl;
}
int main()
{
FLOAT o1,o2,ADD,SUB,MUL,DIV;
clrscr();
o1.indata();
o2.indata();
ADD=o1+o2;
SUB=o1-o2;
MUL=o1*o2;
DIV=o1/o2;
cout<<"\n\n\t o1:";
o1.outdata();
cout<<"\n\n\t o2:";
o2.outdata();
cout<<"\n\n\t ADDITION VALUE IS (o1+o2)=";
ADD.outdata();
cout<<"\n\n\t SUBTRACTION VALUE IS (o1-o2)=";
SUB.outdata();
cout<<"\n\n\t MULTIPLICATION VALUE IS (o1*o2)=";
MUL.outdata();
cout<<"\n\n\t DIVISION VALUE IS (o1/o2)=";
DIV.outdata();
getch();
return(0);
}
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:
EX.NO:3
DATE:
OVERLOADING CONVERSIONS:
AIM:
Design a class polar which 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 + operator to 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:
STEP 2:
STEP 4:
STEP 5:
STEP 6:
STEP 7:
STEP 8:
CODING:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class polar
double radius,angle;
public:
polar()
{
radius=0;
angle=0;
polar(double r,double a)
radius=r;
angle=a;
void disp();
polar operator+(polar);
};
void polar::disp()
cout<<radius;
cout<<angle;
polar p2;
double x1,x2,y1,y2,x,y;
x1=radius*cos(angle*3.14/180);
x2=p1.radius*cos(p1.angle*3.14/180);
y1=radius*sin(angle*3.14/180);
y2=p1.radius*sin(p1.angle*3.14/180);
cout<<"\n output\n";
cout<<"*****\n";
x=x1+x2;
y=y1=y2;
cout<<x;
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
cin>>r1>>a1;
cin>>r2>>a2;
o1=polar(r1,a1);
o2=polar(r2,a2);
o3=o1+o2;
o3.disp();
cin>>c;
while(c=='y'||c!='n');
getch();
}
3.OUTPUT
11
13
13
15
output
*****
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:
STEP 2:
STEP 3:
STEP 4:
STEP 5:
STEP 7:
STEP 8:
To calculate (A-B)^2.
STEP 9:
To calculate A^2+B^2-2*A*B.
STEP 10:
STEP 11:
To verify (A-B)^2!=A^2+B^2-2AB the given identity is false.
STEP 12:
STEP 13:
CODING:
#include<iostream.h>
#include<conio.h>
#include<math.h>
const size=3;
class mat
int nrc;
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++)
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)
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)
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;
void main()
clrscr();
mat m1,m2,mat1,mat2;
int n;
cin>>n;
m1.inp(n);
m2.inp(n);
m1.disp();
m2.disp();
mat1=(m1-m2)^2;
mat1.disp();
mat2=(m1^2)+(m2^2)-((m1*m2)*2);
cout<<"\n result of (A^2+B^2-2AB) is:\n";
mat2.disp();
if(mat1==mat2)
else
getch();
}
4.OUTPUT:
11
11
11
11
00
00
00
EX.NO:5
DATE:
AIM:
Area of rectangle = X*Y
Area of triangle = * X * Y
ALGORITHM :
STEP 1:
STEP 2:
STEP 3:
STEP 4:
STEP 5:
STEP 5:
Calculate the area of triangle are =1/2*X*Y.
STEP 6:
STEP 7:
CODING
#include<iostream.h>
#include<conio.h>
#include<math.h>
class rectangle
float area;
protected:
float x,y;
public:
void get();
void calc()
area=x*y;
}
void disp();
};
void rectangle::get()
cin>>x>>y;
void rectangle::disp()
float area;
public:
void calc()
area=(x*y)/2;
void disp()
}
};
void main()
clrscr();
triangle tr;
tr.get();
tr.rectangle::calc();
tr.rectangle::disp();
tr.calc();
tr.disp();
getch();
}
5.OUTPUT:
EX.NO:6
DATE:
VECTOR PROBLEM:
AIM:
Define a class for vector containing scalar values. Apply overloading
concepts for vector addition, Multiplication of a vector by a scalar quantity,
replace the values in a position vector.
ALGORITHM
STEP 1:
STEP 2:
STEP 3:
STEP 4:
STEP 5:
STEP 6:
To calculate the sum of two vectors.
STEP 7:
STEP 8:
STEP 9:
STEP 10:
CODING:
#include<iostream.h>
#include<conio.h>
#include<math.h>
const size=3;
class vector
public:
int *v;
vector()
{
v=new int[size];
};
return v[pos];
vector c;
for(int i=0;i<size;i++)
c.v[i]=a*b.v[i];
return c;
vector c;
for(int i=0;i<size;i++)
c.v[i]=a.v[i]+b.v[i];
return c;
for(int i=0;i<size;i++)
cin>>b.v[i];
return cin;
cout<<","<<b.v[0];
for(int i=1;i<size;i++)
cout<<","<<b.v[i];
return cout;
void main()
clrscr();
vector m,n;
int sc;
cout<<"\n enter the element of vector 1:";
cin>>m;
cin>>n;
cout<<"\n output";
cout<<"\n ****************";
vector sum,mul;
cin>>sc;
mul=sc * m;
sum=m+n;
int pos,nval;
cin>>pos;
cin>>nval;
m[pos-1]=nval;
getch();
}
6.OUTPUT
output
****************
vector 1:5,6,4
vector 2:4,8,6
INHERITANCE
AIM:
Create three classes alpha, beta and gamma, each containing one data
member. The class gamma should be inherited from both alpha and beta. Use
a constructor function in the class gamma to assign values to the data
members of all the classes.
ALGORITHM:
STEP 1:
STEP 2:
STEP 3:
STEP 4:
STEP 5:
STEP 6:
CODING:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class alpha
int x;
public:
alpha(int i)
x=i;
cout<<"alpha intialised\n";
void show_x(void)
cout<<"x="<<x<<"\n";
};
class beta
{
float y;
public:
beta(float j)
y=j;
cout<<"beta initialized\n";
void show_y(void)
cout<<"y="<<y<<"\n";
};
int m,n;
public:
m=c;
n=d;
cout<<"gamma initialized\n";
void show_mn(void)
{
cout<<"m="<<m<<"\n";
cout<<"n="<<n<<"\n";
};
void main()
clrscr();
gamma g(5,10.75,20,30);
g.show_x();
g.show_y();
g.show_mn();
getch();
}
7.OUTPUT
beta initialized
alpha intialised
gamma initialized
x=5
y=10.75
m=20
n=30
RESULT: