Operator overloading is a feature of C++ that allows programmers to redefine the behavior of existing operators for user-defined data types such as classes and objects. Instead of being limited to built-in data types, operators can perform meaningful operations on objects, making programs more readable and intuitive.
For example, instead of calling a function like add(), operator overloading allows you to use the familiar + operator to combine two objects.
Learning Objectives
Understand operator overloading
Overload unary operators
Overload binary operators
Use friend functions for operator overloading
Understand the benefits and limitations of operator overloading
Apply operator overloading in object-oriented programs
What is Operator Overloading?
Operator overloading allows an existing operator to perform a customized operation on objects.
For example:
+
-
*
==
++
--
Instead of their default behavior, these operators can be redefined for class objects.
Rules for Operator Overloading
Only existing operators can be overloaded.
New operators cannot be created.
Operator precedence cannot be changed.
Operator associativity remains unchanged.
At least one operand must be a user-defined object.
Unary Operator Overloading
Unary operators work with a single operand.
Common unary operators include:
++
--
!
-
Example:
#include <iostream>
using namespace std;
class Counter
{
private:
int value;
public:
Counter()
{
value = 5;
}
void operator++()
{
value++;
}
void display()
{
cout << value;
}
};
int main()
{
Counter c;
++c;
c.display();
return 0;
}
Output
6
Binary Operator Overloading
Binary operators work with two operands.
Example:
#include <iostream>
using namespace std;
class Number
{
public:
int value;
Number(int v)
{
value = v;
}
Number operator+(Number obj)
{
return Number(value + obj.value);
}
};
int main()
{
Number n1(10);
Number n2(20);
Number result = n1 + n2;
cout << result.value;
return 0;
}
Output
30
Friend Function Operator Overloading
A friend function can also overload operators and access private members of a class.
Example:
#include <iostream>
using namespace std;
class Number
{
private:
int value;
public:
Number(int v)
{
value = v;
}
friend Number operator+(Number, Number);
void display()
{
cout << value;
}
};
Number operator+(Number a, Number b)
{
return Number(a.value + b.value);
}
int main()
{
Number n1(15);
Number n2(25);
Number result = n1 + n2;
result.display();
return 0;
}
Output
40
Advantages of Operator Overloading
Improves code readability
Makes object manipulation easier
Produces cleaner syntax
Supports object-oriented design
Reduces the need for multiple function names
Limitations
Not all operators can be overloaded (for example, . and ?:)
Overuse can reduce code readability
Does not change operator precedence or associativity
Best Practices
Overload operators only when their behavior is intuitive.
Keep overloaded operators simple and predictable.
Use friend functions only when necessary.
Maintain consistency with the operator's original purpose.
Avoid excessive operator overloading.
Common Mistakes
Attempting to overload operators that cannot be overloaded.
Changing the expected meaning of common operators.
Forgetting to return the correct object.
Overloading operators without a practical purpose.
Still need help?
Contact us