← Back to Classes
Question 80
Distance Add-Assign and Add (operator+= and operator+)
Distance Add-Assign and Add (operator+= and operator+)
You are tracking total distance across multiple runs.
Create a class Distance that stores TWO ints: kilometers and meters.
- meters should always be 0..999 (normalize after operations).
Requirements:
1) Constructor Distance(int kilometers, int meters)
2) int getMeters() const (return TOTAL meters)
3) operator+= adds another Distance to this one and returns *this
4) operator+ returns a NEW Distance using operator+= internally
Do NOT write a main function.Expected Output:
350 15
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
ClassesOperator Overloading
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.