Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Four integers are read from input, where the first two integers are the kilometers and meters of distance 1 and the second two integers are
Four integers are read from input, where the first two integers are the kilometers and meters of distance and the second two integers are the kilometers and meters of distance Define two functions to overload the operator. The first function overloads the operator to add a distance and an integer representing the number of kilometers. The second function overloads the operator to add two distances.
Ex: If the input is then the output is:
kilometers, meters
kilometers
Sum: kilometers, meters
kilometers, meters
kilometers, meters
Sum: kilometers, meters
Note: The sum of a distance and an integer representing the number of kilometers is:
the sum of the number of kilometers and the integer
the number of meters is unchanged
Note: The sum of two distances is:
the sum of the number of kilometers
the sum of the number of meters #include
using namespace std;
class Distance
public:
Distanceint kilometers int meters ;
void Print const;
Distance operatorint rhs;
Distance operatorDistance rhs;
private:
int km;
int m;
;
Distance::Distanceint kilometers, int meters
km kilometers;
m meters;
No need to accommodate for overflow or negative values
Your code goes here
Distance Distance::operatorconst Distance& rhs
return Distancekm rhskm m rhsm;
void Distance::Print const
cout km kilometers, m meters";
int main
int kilometers;
int meters;
int kilometers;
int meters;
cin kilometers;
cin meters;
cin kilometers;
cin meters;
Distance distancekilometers meters;
Distance distancekilometers meters;
Distance sum distance kilometers;
Distance sum distance distance;
distancePrint;
cout endl;
cout kilometers kilometers" endl;
cout "Sum: ;
sumPrint;
cout endl;
cout endl;
distancePrint;
cout endl;
distancePrint;
cout endl;
cout "Sum: ;
sumPrint;
cout endl;
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started