Answer to Question #329036 in C++ for tarun

Question #329036

Write the definition for a class called Distance that has data member feet as integer and inches as float. The class has the following member functions:

                       void set(int, float) to give value to object

                       void disp() to display distance in feet and inches

                       Distance add(Distance) to sum two distances & return distance

                       1. Write the definitions for each of the above member functions.

                       2. Write main function to create three Distance objects. Set the value in two objects and call add() to calculate sum and assign it in third object. Display all distances.


1
Expert's answer
2022-04-15T04:43:55-0400
#include <iostream>

using namespace std;

class Distance
{
	int feet;
	float inches;
public:
	Distance():feet(0),inches(0){}
	void set(int ft,float in)
	{
		feet = ft;
		if (in >= 12)
		{
			feet += in / 12;
			int tmpin = (int)in;
			inches = (tmpin % 12)+ in- tmpin;
		}
		else
			inches = in;
	}


	Distance add(const Distance& d)
	{
		Distance x;
		int tmpf = feet + d.feet;
		float tmpi = inches + d.inches;
		x.set(tmpf, tmpi);
		return x;
	}
	void disp()
	{
		cout << "Feet of distance: " << feet
			<< "\nInches of distance:" << inches<<endl;
	}
};




int main()
{
	Distance a;
	a.set(5, 23.7);
	cout << "Info about a:\n";
	a.disp();
	Distance b;
	b.set(3, 14.8);
	cout << "Info about b:\n";
	b.disp();
	Distance c = a.add(b);
	cout << "Info about c = a.add(b):\n";
	c.disp();
}



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