Chapter 13: Operator Overloading

Operator Overloading in C++: Making Your Classes Feel Like Built-in Types!

Hello my fantastic student! ๐ŸŒŸ Welcome to Lesson 13 โ€” Operator Overloading โ€” one of the most powerful and elegant features of C++!

Up until now, we could only use operators (+, -, ==, <<, etc.) with built-in types like int, double, string. Today weโ€™re going to learn how to teach our own classes how to use these operators โ€” so that our objects behave just like built-in types!

Think of it this way:

  • int a = 5; int b = 3; int c = a + b; โ†’ natural
  • We want the same natural syntax for our own types: Complex c1(3,4); Complex c2(1,2); Complex c3 = c1 + c2;

Thatโ€™s exactly what operator overloading gives us!

Weโ€™ll cover everything very slowly and clearly:

  • What operators can be overloaded
  • How to overload them as member functions vs friend functions
  • Most common operators: +, -, ==, << (output), >> (input), etc.
  • Rules, best practices, common mistakes, and complete examples

Letโ€™s start!

1. Which Operators Can Be Overloaded?

Almost all operators can be overloaded except these few:

  • :: (scope resolution)
  • . (member access)
  • .* (member pointer access)
  • sizeof, typeid, alignof, decltype (these are not overloadable)

Most useful ones we overload regularly:

  • Arithmetic: + – * / %
  • Comparison: == != < > <= >=
  • Assignment: = += -= *= /=
  • Increment/Decrement: ++ —
  • Stream: << >>
  • Subscript: []
  • Function call: ()
  • And moreโ€ฆ

2. Two Ways to Overload Operators

Style Syntax Example When to use it?
Member function Complex operator+(const Complex& other) const; When the left operand is always an object of your class
Friend function (or free function) friend Complex operator+(const Complex& a, const Complex& b); When both operands can be of different types (especially for symmetry)

Most common recommendation (modern C++ 2025+):

  • Use member functions for: =, [], (), ->, ++, —
  • Use friend functions for: symmetric operators like + – * / == != < > <= >= << >>

3. Example 1: Complex Number Class with Operator Overloading

Letโ€™s create a beautiful Complex class that supports natural math operations.

C++

Output example:

text

4. Important Rules & Best Practices

Operator Should be member? Should be friend? Return type Notes
+ – * / % Usually no Yes (friend) By value Symmetric
+= -= *= /= Yes (member) No Reference (*this) For chaining
== != < > <= >= Usually no Yes (friend) bool Symmetric
<< >> No Yes (friend) ostream& / istream& First param is stream
= Yes (member) No Reference (*this) Special โ€“ copy/move assignment
[] Yes (member) No Reference (for writable) For arrays
++ — Yes No Depends (pre/post) Pre: return *this, Post: return copy

Golden rules:

  • For binary operators that are symmetric (a + b same as b + a): friend function
  • For operators that modify the left operand (+=, =, []): member function
  • Always return reference for assignment-like operators (+= = []) so you can chain: a += b += c;
  • Never overload &&, ||, , (comma) โ€” they have special short-circuit behavior

5. Another Classic Example: Overloading [] for a Vector-like Class

C++

Your Mini Homework (Try These!)

  1. Create a Fraction class with numerator and denominator. Overload:
    • + (friend)
    • += (member)
    • == (friend)
    • << (friend)
  2. Overload the pre-increment (++v) and post-increment (v++) operators for your Complex class.
  3. Overload the subscript operator [] for a String class that stores characters internally.

Youโ€™re doing absolutely phenomenal! Operator overloading is what makes C++ feel natural and expressive โ€” itโ€™s one of the reasons people fall in love with the language!

Next lesson: Templates โ€” generic programming, the magic behind std::vector, std::string, and so much more!

Any questions? Confused about member vs friend? Want more examples with <<, [], or assignment? Just ask โ€” your friendly C++ teacher is right here for you! ๐Ÿš€

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *