Question: 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 distance1 and the second two integers are the kilometers and meters of distance2. 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 103882156, then the output is:
10 kilometers, 388 meters
2 kilometers
Sum: 12 kilometers, 388 meters
10 kilometers, 388 meters
2 kilometers, 156 meters
Sum: 12 kilometers, 544 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:
Distance(int kilometers =0, int meters =0);
void Print() const;
Distance operator+(int rhs);
Distance operator+(Distance rhs);
private:
int km;
int m;
};
Distance::Distance(int kilometers, int meters){
km = kilometers;
m = meters;
}
// No need to accommodate for overflow or negative values
/* Your code goes here */
Distance Distance::operator+(const Distance& rhs){
return Distance(km + rhs.km, m + rhs.m);
}
void Distance::Print() const {
cout << km <<" kilometers, "<< m <<" meters";
}
int main(){
int kilometers1;
int meters1;
int kilometers2;
int meters2;
cin >> kilometers1;
cin >> meters1;
cin >> kilometers2;
cin >> meters2;
Distance distance1(kilometers1, meters1);
Distance distance2(kilometers2, meters2);
Distance sum1= distance1+ kilometers2;
Distance sum2= distance1+ distance2;
distance1.Print();
cout << endl;
cout << kilometers2<<" kilometers" << endl;
cout << "Sum: ";
sum1.Print();
cout << endl;
cout << endl;
distance1.Print();
cout << endl;
distance2.Print();
cout << endl;
cout << "Sum: ";
sum2.Print();
cout << endl;
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!