Answer to Question #248856 in C++ for Rajeev

Question #248856

Write a program to add two distance objects  and stored the result in first object using Operator member function and friend function


1
Expert's answer
2021-10-10T01:49:09-0400
#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;
}

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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS