Answer to Question #255644 in C++ for gikovi

Question #255644

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.


1
Expert's answer
2021-10-23T13:52:56-0400
/*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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

gikovi
26.10.21, 17:31

Thanks a lot Assignment Expert Team!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS