C++ · Module 10 · Lesson 11 min read

Operator Overloading Basics

What is operator overloading?

Defining what operators like +, ==, <<, [] mean for your types. An overloaded operator is just a function with a special name: operator+, operator==, operator<<.

Member vs non-member

kotlin
class Money {
public:
    Money& operator+=(const Money& o);              // member: modifies *this
};
Money operator+(Money a, const Money& b) { return a += b; }   // non-member: symmetric
OperatorUsually
=, [], (), ->, +=-style compoundmember (some must be)
+ - * /, ==, <, <<, >>non-member (often a hidden friend)

Non-members allow conversions on both sides and let the left operand be another type (like std::ostream).

Implement + with += ✅

Write the logic once in += (member), then + as a non-member that copies and uses +=.

Stream operators

c
std::ostream& operator<<(std::ostream& os, const Money& m) { return os << ...; }
std::istream& operator>>(std::istream& is, Money& m) { ... return is; }

Return the stream by reference to allow chaining: cout << a << b.

Unary operators

-a → Money operator-() const; · ++a → Money& operator++(); · a++ → Money operator++(int); (the dummy int marks postfix)

Rules

  • You can't create new operators or change precedence
  • At least one operand must be a user-defined type (you can't redefine int + int)
  • Can't overload ::, ., .*, ?:, sizeof
  • Don't overload &&, ||, , (they lose short-circuit/order guarantees)

Golden rule ✅

Do what the operator naturally means. + should add, not print or delete. If the meaning isn't obvious, use a named function.

Example

C++
#include <iostream>

class Money {
public:
    explicit Money(long long paisa = 0) : paisa_(paisa) {}
    static Money rupees(double r) { return Money(static_cast<long long>(r * 100 + (r < 0 ? -0.5 : 0.5))); }

    Money& operator+=(const Money& o) { paisa_ += o.paisa_; return *this; }
    Money& operator-=(const Money& o) { paisa_ -= o.paisa_; return *this; }
    Money operator-() const { return Money(-paisa_); }                  // unary minus
    Money& operator++() { paisa_ += 100; return *this; }                // prefix: add Rs. 1
    Money operator++(int) { Money old = *this; ++*this; return old; }   // postfix

    friend Money operator+(Money a, const Money& b) { return a += b; }  // built from +=
    friend Money operator-(Money a, const Money& b) { return a -= b; }
    friend Money operator*(Money a, double factor) { return Money(static_cast<long long>(a.paisa_ * factor + 0.5)); }
    friend bool operator==(const Money& a, const Money& b) { return a.paisa_ == b.paisa_; }

    friend std::ostream& operator<<(std::ostream& os, const Money& m) {
        long long p = m.paisa_ < 0 ? -m.paisa_ : m.paisa_;
        os << (m.paisa_ < 0 ? "-" : "") << "Rs. " << p / 100 << '.';
        if (p % 100 < 10) os << '0';
        return os << p % 100;
    }
    friend std::istream& operator>>(std::istream& is, Money& m) {
        double r;
        if (is >> r) m = rupees(r);
        return is;
    }

private:
    long long paisa_;
};

int main() {
    Money rent = Money::rupees(15000), food = Money::rupees(4200.5);
    Money total = rent + food;
    std::cout << "rent + food = " << total << '\n';
    std::cout << "rent - food = " << rent - food << '\n';
    std::cout << "food * 1.13 = " << food * 1.13 << '\n';
    std::cout << "-food = " << -food << '\n';

    Money tip = Money::rupees(9.99);
    std::cout << "tip++ returns " << tip++ << ", now " << tip << '\n';
    std::cout << "++tip returns " << ++tip << '\n';

    total += Money::rupees(0.5);
    std::cout << "total after += 0.50: " << total << '\n';
    std::cout << std::boolalpha << "0.10 + 0.20 == 0.30? "
              << (Money::rupees(0.1) + Money::rupees(0.2) == Money::rupees(0.3)) << '\n';

    Money entered;
    std::cin >> entered;
    std::cout << "you entered " << entered << '\n';
    return 0;
}

/* Output:
rent + food = Rs. 19200.50
rent - food = Rs. 10799.50
food * 1.13 = Rs. 4746.56
-food = -Rs. 4200.50
tip++ returns Rs. 9.99, now Rs. 10.99
++tip returns Rs. 11.99
total after += 0.50: Rs. 19201.00
0.10 + 0.20 == 0.30? true
you entered Rs. 349.75
*/