Module 19: Operator Overloading in C++
Operator Overloading allows predefined C++ operators to work with user-defined objects. It enables operators such as +, -, [], (), <<, ==, and != to perform meaningful operations on objects of custom classes.
For example, instead of writing:
1complex.add(other);
You can write:
1complex + other;
This makes code more readable and intuitive.
Advantages of Operator Overloading
- Makes code easier to read.
- Improves code reusability.
- Gives custom classes natural syntax.
- Supports object-oriented programming.
1. Division Operator (/)
The division operator can be overloaded for user-defined classes.
Example
1#include <iostream> 2 3using namespace std; 4 5class Number 6{ 7private: 8 int value; 9 10public: 11 Number(int v) 12 { 13 value = v; 14 } 15 16 Number operator/(const Number& other) 17 { 18 return Number(value / other.value); 19 } 20 21 void display() 22 { 23 cout << value; 24 } 25}; 26 27int main() 28{ 29 Number n1(20); 30 Number n2(4); 31 32 Number result = n1 / n2; 33 34 result.display(); 35 36 return 0; 37}
Output
15
2. Subscript Operator ([])
The subscript operator allows objects to behave like arrays.
Example
1#include <iostream> 2 3using namespace std; 4 5class Array 6{ 7private: 8 int data[5] = {10,20,30,40,50}; 9 10public: 11 int operator[](int index) 12 { 13 return data[index]; 14 } 15}; 16 17int main() 18{ 19 Array arr; 20 21 cout << arr[2]; 22 23 return 0; 24}
Output
130
3. Function Call Operator (())
The function call operator lets an object behave like a function.
Example
1#include <iostream> 2 3using namespace std; 4 5class Square 6{ 7public: 8 int operator()(int x) 9 { 10 return x * x; 11 } 12}; 13 14int main() 15{ 16 Square square; 17 18 cout << square(6); 19 20 return 0; 21}
Output
136
4. Stream Insertion Operator (<<)
The << operator is commonly overloaded to print objects using cout.
Example
1#include <iostream> 2 3using namespace std; 4 5class Student 6{ 7private: 8 string name; 9 10public: 11 Student(string n) 12 { 13 name = n; 14 } 15 16 friend ostream& operator<<(ostream& out, const Student& s) 17 { 18 out << s.name; 19 return out; 20 } 21}; 22 23int main() 24{ 25 Student s("Ankit"); 26 27 cout << s; 28 29 return 0; 30}
Output
1Ankit
5. Increment Operator (++)
The increment operator can be overloaded.
Prefix Increment
1#include <iostream> 2 3using namespace std; 4 5class Counter 6{ 7public: 8 int value = 0; 9 10 Counter& operator++() 11 { 12 value++; 13 return *this; 14 } 15}; 16 17int main() 18{ 19 Counter c; 20 21 ++c; 22 23 cout << c.value; 24 25 return 0; 26}
Output
11
6. Decrement Operator (--)
The decrement operator decreases an object's value.
Example
1#include <iostream> 2 3using namespace std; 4 5class Counter 6{ 7public: 8 int value = 5; 9 10 Counter& operator--() 11 { 12 value--; 13 return *this; 14 } 15}; 16 17int main() 18{ 19 Counter c; 20 21 --c; 22 23 cout << c.value; 24 25 return 0; 26}
Output
14
7. Equality Operator (==)
The equality operator compares two objects.
Example
1#include <iostream> 2 3using namespace std; 4 5class Student 6{ 7private: 8 int id; 9 10public: 11 Student(int i) 12 { 13 id = i; 14 } 15 16 bool operator==(const Student& other) 17 { 18 return id == other.id; 19 } 20}; 21 22int main() 23{ 24 Student s1(1); 25 Student s2(1); 26 27 if (s1 == s2) 28 { 29 cout << "Equal"; 30 } 31 32 return 0; 33}
Output
1Equal
8. Inequality Operator (!=)
The inequality operator checks whether two objects are different.
Example
1#include <iostream> 2 3using namespace std; 4 5class Student 6{ 7private: 8 int id; 9 10public: 11 Student(int i) 12 { 13 id = i; 14 } 15 16 bool operator!=(const Student& other) 17 { 18 return id != other.id; 19 } 20}; 21 22int main() 23{ 24 Student s1(1); 25 Student s2(2); 26 27 if (s1 != s2) 28 { 29 cout << "Not Equal"; 30 } 31 32 return 0; 33}
Output
1Not Equal
Operators That Can Be Overloaded
| Operator | Purpose |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
[] | Array indexing |
() | Function call |
<< | Stream insertion |
>> | Stream extraction |
++ | Increment |
-- | Decrement |
== | Equality |
!= | Inequality |
< | Less than |
> | Greater than |
Operators That Cannot Be Overloaded
The following operators cannot be overloaded:
::(Scope Resolution).(Member Access).*(Pointer-to-member)?:(Conditional Operator)sizeoftypeid
Best Practices
- Overload operators only when the meaning is intuitive.
- Keep operator behavior consistent with built-in types.
- Return references where appropriate to improve performance.
- Use
constreferences for parameters to avoid unnecessary copies. - Avoid overloading operators in ways that surprise users.
Practice Problems
- Overload the
+operator for aComplexclass. - Overload the
/operator for aFractionclass. - Create a custom container using the
[]operator. - Overload the
()operator for a calculator class. - Overload the
<<operator to print employee details. - Implement
==and!=for aBookclass. - Implement prefix and postfix
++operators for a counter class.
Interview Questions
- What is operator overloading in C++?
- Why is operator overloading useful?
- Which operators can be overloaded?
- Which operators cannot be overloaded?
- What is the difference between prefix and postfix increment operator overloading?
- Why is
<<usually implemented as a friend function? - When should operator overloading be avoided?
- What are the benefits of overloading comparison operators?
Summary
In this module, you learned:
- What operator overloading is and why it is useful.
- How to overload the division (
/) operator. - How to implement the subscript (
[]) and function call (()) operators. - How to overload the stream insertion (
<<) operator for output. - How to overload increment (
++) and decrement (--) operators. - How to overload equality (
==) and inequality (!=) operators. - Best practices for writing clear, efficient, and intuitive overloaded operators.
Next Module: Module 20: Exception Handling — try, catch, throw, Standard Exceptions, Custom Exceptions, Exception Specifications, and Best Practices.