Operator Overloading

What Is Operator Overloading? Explained With an Example

Operator overloading lets a class define what +, ==, or << does for its objects. A short C++ example with a Money class shows how it works in code.

On this page
  1. The basic idea
  2. Example: adding money amounts
  3. Why it’s useful
  4. Things to keep in mind
  5. In short

Operator overloading is a C++ feature that lets you define what an operator, such as +, does when its operands are your own types. A class for money can define + so that adding two amounts looks the same as adding two numbers.

The basic idea

Built-in types such as int and double already know what + means. A type you create doesn’t. If you write a + b on two of your objects without defining anything, the compiler reports an error. To fix that, you write a function named operator+ in your class. When the compiler sees a + b and a is your type, it calls that function.

Example: adding money amounts

The Money class below stores an amount in whole cents, so the addition is exact integer math.

#include <iostream>

class Money {
public:
    explicit Money(int cents) : cents_(cents) {}

    Money operator+(const Money& other) const {
        return Money(cents_ + other.cents_);
    }

    int cents() const { return cents_; }

private:
    int cents_;
};

int main() {
    Money a(250);           // $2.50
    Money b(199);           // $1.99
    Money total = a + b;    // calls a.operator+(b)

    std::cout << total.cents() << " cents\n";
}

Output:

449 cents

Here is what happens:

  • operator+ is a member function, so a + b is the same as a.operator+(b).
  • It returns a new Money object and leaves a and b unchanged.
  • The constructor is marked explicit, so a plain integer doesn’t turn into a Money by accident. A line like Money m = 250; won’t compile.

Why it’s useful

Once + works, the code reads the way you would write the sum on paper: total = a + b. Without overloading, you would write total = add(a, b). Both work, but the operator form is shorter and closer to the math it describes.

Things to keep in mind

  • The operator should do what a reader expects. + should add, not subtract.
  • Not every operator can be overloaded. Which operators cannot be overloaded is a short, fixed list that includes :: and ..
  • At least one operand must be your type or an enumeration, so you can’t redefine how int + int works.

In short

Operator overloading lets your class use operators like + by defining a function named operator+. The compiler calls that function whenever it sees the operator used with your type.

The complete guide to operator overloading

Operator Overloading in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

Recursive Functions in C++

A recursive function calls itself to solve smaller parts of a problem. Learn the base case, the recursive case, examples in C++ and when to use recursion.

Recursion 5 min read

The const Keyword in C++

Learn what the const keyword does in C++, how it applies to variables, pointers, references and member functions, and how the compiler enforces it.

const 5 min read