Answer to Question #323923 in C++ for khan

Question #323923

class Distance with two private data members:

 feet: An integer that holds the feet.

 inches: An integer that holds the inches.

 Write a constructor with default parameters that initializes each data member of the class.

If inches are greater than equal to 12 then they must be appropriately converted to

corresponding feet.

.

o void setFeet(int f) and int getFeet()const

o void setInches(int i) It should ensure proper conversion to feet.

o int getInches() const

 Define an operator ‘+’ that overloads the standard ‘+’ math operator and allows one

Distance object to be added to another. Distance operator+ (const Distance &obj).

 Define an operator - function that overloads the standard ‘-‘ math operator and allows

subtracting one Distance object from another. Distance operator-(const Distance &obj)

 Define an operator= function that overloads the = operator and assign one Distance object to another. const Distance operator=(const Distance &obj)


1
Expert's answer
2022-04-06T07:10:48-0400
#include <iostream>


using namespace std;


class Distance
{
	int feet;
	int inches;
public:
	Distance(int _feet=0,int _inches=0)
	:feet(_feet)
	{
		if (_inches >= 12)
		{
			feet += _inches / 12;
			inches = _inches % 12;
		}
		else
			inches = _inches;
	}
	void SetFeet(int f) { feet = f; }
	int GetFeet() { return feet; }
	void SetInches(int in) 
	{
		if (in >= 12)
		{
			feet += in / 12;
			inches = in % 12;
		}
		else
			inches = in;
	}
	int GetInches() { return inches; }
	Distance operator+ (const Distance& obj)
	{
		Distance tmp;
		tmp.feet = feet + obj.feet;
		tmp.inches = inches + obj.inches;
		if (tmp.inches >= 12)
		{
			tmp.feet += tmp.inches / 12;
			tmp.inches = tmp.inches % 12;
		}
		return tmp;
	}
	Distance operator- (const Distance& obj)
	{
		Distance tmp;
		tmp.feet = feet - obj.feet;
		if (inches >= obj.inches)
		{
			tmp.inches= inches - obj.inches;
		}
		else
		{
			tmp.feet -= 1;
			tmp.inches = 12 + inches - obj.inches;
		}
		return tmp;
	}


	const Distance operator= (const Distance& obj)
	{
		Distance tmp;
		tmp.feet = obj.feet;
		tmp.inches = obj.inches;
		return tmp;
	}
	friend ostream& operator<<(ostream&, Distance&);
};


ostream& operator<<(ostream& os, Distance& d)
{
	return os << "\nFeet of distance: " << d.feet
		<< "\nInches of distance:" << d.inches;
}


int main()
{
	Distance d;
	cout << "\nFeet d: " << d.GetFeet() << " Inches : " << d.GetInches();
	d.SetFeet(5);
	d.SetInches(16);
	cout << "\nFeet d: " << d.GetFeet() << " Inches d: " << d.GetInches();
	Distance c = d;
	cout << "\nFeet c: " << c.GetFeet() << " Inches c: " << c.GetInches();
	Distance e = d + c;
	cout << "\nFeet e: " << e.GetFeet() << " Inches e: " << e.GetInches();
	Distance m(4, 23);
	Distance n = e - m;
	cout << "\nFeet n: " << n.GetFeet() << " Inches n: " << n.GetInches();


}



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