Distance Class with > and < Operators

Learn to overload > and < operators in a Distance class.

Challenge

Write a program that implements a Distance class that records the distance in meter and centimeter. Overload the > and < operator in it to check whether one Distance object is smaller or greater than the other.

Sample run

Here’s what you should see when you run the program.

d1 = Distance = 10 m 50 cm
d2 = Distance = 15 m 30 cm
d3 = Distance = 12 m 45 cm
d1 is less than d2 
d1 is less than d3

Coding exercise

Try to solve this challenge on your own. If you can’t solve it, you can look at the solution and explanation below.

Get hands-on with 1400+ tech skills courses.