Operator Overloading

Lesson 19 of 20

Teach + and << new tricks — overloaded operators make your own types read like the built-in ones.

The big picture

Your types, natural syntax

Operators are just functions in disguise — and C++ lets you write the disguise for your own classes.

Operators are functions

a + b can secretly call a function you write named operator+. Same symbol, your meaning.

Member-friendly syntax

Once operator+ exists for Money, Money code reads like int code: total = bag1 + bag2.

Any sensible operator

Overload ==, <, + and more so sorting and comparing work naturally on your objects.

Rules apply

You cannot invent new operators or change precedence — and + should still feel like adding.