⚡0 XP🔥0

Operator Overloading

Lesson 6 of 12 ⏱ 6 min read ✎ 2 exercises ❓ 4-question quiz ▶ Runs C++

Make your own types work with +, -, ==, <, <<, [] and ++ by overloading operators as member and friend functions.

For built-in types, a + b just works. Operator overloading lets your own classes use operators naturally: price1 + price2, v1 == v2, cout << student. It is an example of compile-time polymorphism.

An overloaded operator is simply a function named operator followed by the symbol: operator+, operator==, operator<<.

Overloading + as a member function#

▶ Try it YourselfC++
Ctrl + Enter to run
Output
a + b = (4, 6)
a - b = (2, 2)
b * 3 = (3, 6)
a == b ? false

a + b is translated by the compiler into a.operator+(b): the left operand is the object, the right operand is the parameter.

हिंदी में समझें

Operator overloading का मतलब है अपनी class के लिए +, ==, << जैसे operators का अर्थ खुद तय करना। a + b लिखने पर compiler असल में a.operator+(b) call करता है। इससे code पढ़ने में बिल्कुल गणित जैसा लगता है।

Overloading << for printing#

To print with cout << obj, the left operand is cout (an ostream), so the operator cannot be a member of your class. Write it as a friend function that returns the stream:

▶ Try it YourselfC++
Ctrl + Enter to run
Output
[120] Tanvi - CGPA 9.31
[121] Manav - CGPA 6.84

Overloading comparison operators - and sorting with them#

If your class has operator<, the STL sort can sort your objects directly:

▶ Try it YourselfC++
Ctrl + Enter to run
Output
Virat 82
Rahul 64
Rohit 45
Gill 31

Unary operators: ++ (prefix and postfix), unary -#

▶ Try it YourselfC++
Ctrl + Enter to run
Output
6 6 7 -7

The subscript operator []#

▶ Try it YourselfC++
Ctrl + Enter to run
Output
67 60
Caught: bad index

Rules and good practice#

  • You cannot create new operators or change precedence; you cannot overload ::, ., .*, ?:, sizeof.
  • At least one operand must be a user-defined type (you cannot redefine int + int).
  • Keep the natural meaning: + should add, == should compare. Surprising operators make code hard to read.
  • Return a new object from arithmetic operators (+, -), a reference from compound ones (+=, prefix ++), and bool from comparisons.

Summary#

  • operator+, operator==, operator<... are ordinary functions with special names.
  • Member operators take the left operand as this; use friend functions when the left side is not your class (like cout <<).
  • Overloading < lets sort work on your objects; [] should return a reference.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Money class

Class Money stores rupees and paise. Overload + (carry 100 paise into 1 rupee) and << to print Rs 125.40 format (2-digit paise). Money(120, 75) + Money(4, 65) must print Rs 125.40.

Expected output:
Rs 125.40
2

Compare fractions

For class Fraction {int num, den;} overload == (cross-multiply) and <. Print the results as shown:

1/2 == 2/4 : true
1/3 < 1/2 : true
3/4 < 2/3 : false
Expected output:
1/2 == 2/4 : true
1/3 < 1/2 : true
3/4 < 2/3 : false

Quick Quiz

Q1.

Which operator can NOT be overloaded?

Q2.

Why is operator<< for cout usually a friend (non-member) function?

Q3.

What should operator<< return?

Q4.

How does C++ tell prefix ++x and postfix x++ overloads apart?