Write a program to add two distance objects and stored the result in first object using Operator member function and friend function
#include <iostream>
using namespace std;
class Distance
{
private:
int f;
int i;
public:
void obtain_d()
{
cout<<"Enter first distance in feet: ";
cin>>f;
cout<<"Enter second distance in inches: ";
cin>>i;
}
void getD()
{
cout<<"Distance in feet= "<<f<<", inches= "<<i<<endl;
}
void add(Distance x, Distance y)
{
f = x.f + y.f;
i = x.i + y.i;
f = f + (i / 12);
i = i % 12;
}
};
int main()
{
Distance x, y, z;
x.obtain_d();
y.obtain_d();
z.add(x, y);
z.getD();
return 0;
}
Comments
Leave a comment