Return Type
1. Write a program to define class Distance with data members feet ,inches
of apropriate type.Define member function of class to adds
two distances and return to sum in main program.
#include<iostream.h>
#include<conio.h>
class Distance
{
private:
int feet,inches;
public:
void getdetails();
void display();
Distance returndis(Distance);
};
void Distance::getdetails()
{
cout<<"Enter the value of feet:";
cin>>feet;
cout<<"Enter the value of inches:";
cin>>inches;
}
void Distance::display()
{
cout<<feet<<"-feet "<<inches<<"-inches"<<endl;
}
Distance Distance::returndis(Distance d2)
{
int feet1;
Distance d3;
inches=inches + d2.inches;
d3.inches=inches%12;
feet1=inches/12;
d3.feet= feet1+ feet+ d2.feet;
return d3;
}
void main()
{
Distance d1,d2,d3;
cout<<"Enter the first value for distance:"<<endl;
d1.getdetails();
cout<<endl;
cout<<"Enter the second value for distance:"<<endl;
d2.getdetails();
cout<<endl;
cout<<"The first value of distance is:"<<endl;
d1.display();
cout<<endl;
cout<<"the second value of distance is:"<<endl;
d2.display();
cout<<endl;
d3=d2.returndis(d1);
cout<<"The distance after addition is :"<<endl;
d3.display();
getch();
}
output:
2 Write a program to define class Complex with data members real,img(imaginary)
of apropriate type.Define member function of class to add
two complex and return to sum in main program.
#include<iostream.h>
#include<conio.h>
class Complex
{
private:
int real,img;
public:
void getdetails();
void display();
Complex returnCom(Complex);
};
void Complex::getdetails()
{
cout<<"Real Part :";
cin>>real;
cout<<"Imaginary part :";
cin>>img;
}
void Complex::display()
{
if(img>0)
{
cout<<real<<"+j"<<img;
}
else
{
cout<<real<<"j"<<(-1)*img;
}
}
Complex Complex ::returnCom(Complex C1)
{
Complex C3;
C3.real=C1.real+real;
C3.img=C1.img+img;
return C3;
}
void main()
{
Complex C1,C2,C3;
cout<<"enter the first complex number:"<<endl;
C1.getdetails();
cout<<endl;
cout<<"enter the second complex number:"<<endl;
C2.getdetails();
cout<<endl;
cout<<"First complex number is :"<<endl;
C1.display();
cout<<endl;
cout<<"Second complex number is :"<<endl;
C2.display();
cout<<endl<<"The sum is :"<<endl;
C3=C2.returnCom(C1);
C3.display();
getch();
}
Output:
Comments
Post a Comment