Lesson 19 of 24

Operator Overloading

10 min 35 XP

You can give operators like + - == < a meaning for your own class. Overload << and cout << object just works.

Example: edit me and press Run

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.

Challenge

+35 XP

Write class Fraction with operator+ that adds two fractions and reduces the result with the GCD, and operator<< that prints "a/b". In main(), read a b c d and print a/b + c/d. Input: 1 2 1 3 → Output: 5/6

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.