Create a class which stores distance in feet and inches. Overload the ++ (post and pre) operator for the class for the statements D2=++D1 and D2=D1++ as member function. Also rewrite this program to overload the operators as friend function.
/*Create a class which stores distance in feet and inches.
Overload the ++ (post and pre) operator for the class for the statements
D2=++D1 and D2=D1++ as member function.
Also rewrite this program to overload the operators as friend function.
*/
#include<iostream>
#include<conio.h>
using namespace std;
class Distance
{
int feet;
int inch;
public:
void getData()
{
cout<<"\nEnter feet,inch : ";
cin>>feet>>inch;
}
void putData()
{
cout<<"\nFeet="<<feet<<", Inch="<<inch;
}
void operator +=(Distance D2)
{
feet+=D2.feet;
inch+=D2.inch;
feet += inch / 12;//INCREMENT IF INCH > 12
inch = inch % 12;
}
};
int main()
{
int clrscr();
Distance D1,D2;
D1.getData();
D2.getData();
//D1+=D2;
D2+=D1;
D1.putData();
getch();
return 0;
}
Comments
Thanks a lot Assignment Expert Team!
Leave a comment