Answered step by step
Verified Expert Solution
Link Copied!

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 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Systems Analysis And Synthesis Bridging Computer Science And Information Technology

Authors: Barry Dwyer

1st Edition

0128054492, 9780128054499

More Books

Students also viewed these Databases questions

Question

2.7 Identify how privacy legislation impacts employees.

Answered: 1 week ago