Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to input two distances and then compute the sum of the two dista

ID: 3642081 • Letter: W

Question

Write a program to input two distances and then compute the sum of the two distances and display them. You should be able to input the distance (in feet and inches) by using a constructor and by using a member function, other than the constructor.

The distances to be entered should comprise of feet and inches. For instance Distance1 can be 7 feet 4 inches.
Model this by creating a class Distance. The two data items are of type int to hold the ‘feet’ and type float to hold the ‘inches’.

You will have two constructors. A default constructor always initializes the two private members to 0. An overloaded constructor, to initialize the two member data to the value passed by the user in the program while creating an object of the type class Distance.

A member function getdist() to prompt the user for input for feet and inches for 1 object. Second member function showdist() to display the feet and inches of the object. Third member function add_dist (Distance d1, Distance d2) to add the inches of d1 and d2 AND feet of d1 and d2. Care should be taken and check should be made, if the inches exceed 12, you will need to add 1 to the feet and decrement inches by 12.

For instance if obj.feet=9 and obj.inches=18, than we add 1 to feet and take away 12 from inches. Your final outcome of this operation will look like the following obj.feet=10 and obj.inches=6.

Define the following class:
Class Distance
{
public:
Distance ( ); // constructor

Distance (int ft, float in); //overloaded constructor

void getdist ( ); // Prompt user to give two inputs for distance object- feet and inches and accept the 2 inputs in the
// private variables of the class

void showdist(); //Function to display the two values of the object type Distance.

void add_dist(Distance d1, Distance d2); //Function to add distance values of d1 and d2, with proper care to
//increment value of feet if inches exceed 12 as explained earlier

private:
int feet;
float inches;
};

You should implement each member function as a separate function and test your program with the following main () function.
int main ( )
{
//declare d1, sum_d3 as object of class Distance
//call overloaded constructor for d2 by passing 2 values, example Distance d2(7,8.8);
//call getdist() for d1 to get the feet and inches for object d1
//call add_dist by passing d1 and d2 as parameters to obtain the feet and inches for object sum_d3. This is where
//you will have to computed the summation of d1 and d2 objects. That is sum_d3=d1+d2. Care should be taken to
//increment the feet if the inches value exceeds 12, as explained above.
//call showdist() for each object to display its corresponding values of feet and inches
return 0;
}

Explanation / Answer

Hope this helps! #include using namespace std; class Distance { private: float _inches; int _feet; public: Distance() : _inches(0.0), _feet(0) { } Distance(int f, float i) { _feet = f; _inches = i; //Make sure inches is less than 12 while(_inches >= 12) { _inches -= 12.0; ++_feet; } } void getdist() { cout > _feet; cout > _inches; //Make sure inches is less than 12 while(_inches >= 12) { _inches -= 12.0; ++_feet; } } void add_dist(Distance d1, Distance d2) { _feet = d1._feet + d2._feet; _inches = d1._inches + d2._inches; //Make sure inches is less than 12 while(_inches >= 12) { _inches -= 12.0; ++_feet; } } void showDist() { cout
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote