Operator overloading in C++ is a method to redefine existing operators so they can work with user-defined types, enabling objects to behave like built-in data types. It is an example of compile-time polymorphism. This makes the object act like primitive data types and improves the readability of code.
Example of operator overloading in c++
- String1 + String2 is used to concatenate two string objects.
- ‘Date++’ is used for incrementing a Date object.
- Number1*Number2 multiplies two Number objects.
- arr[i] accesses an element of an array object.
#include <iostream>
using namespace std;
class Number {
int value;
public:
Number(int v=0) { // constructor
value = v;
}
// Overload ++ operator (unary)
Number operator++() {
value = value + 1;
return Number(value);
}
void display() {
cout << value << endl;
}
};
int main() {
Number n1(5);
++n1; // uses overloaded ++
n1.display(); // Output: 6
return 0;
}Why is operator overloading used?
- It allows objects to behave like basic data types.
- It is useful for complex numbers and vectors.
- It reduces the need for extra function calls and makes the code simpler.
How operator overloading in c++ is used
When you overload an operator in C++, you write a special function called an operator function. This function can be written using two methods.
1. Member function
In a member function, the object on the left side of the operator is automatically passed. The example of member functions is:
- Unary operator ( ++ ) – needs no arguments.
- Binary operator ( + ) – needs one argument.
#include <iostream>
using namespace std;
class Number {
int value;
public:
Number(int v=0) { // constructor
value = v;
}
// Overload + operator as a member function
Number operator+(Number n) {
return Number(value + n.value);
}
void display() {
cout << value << endl;
}
};
int main() {
Number n1(5), n2(10);
Number n3 = n1 + n2; // uses overloaded + (member function)
n3.display(); // Output: 15
return 0;
}2. Friend function
In a friend function, nothing is passed automatically; all operands must be given explicitly.
- Unary operator ( ++ ) – needs one argument.
- Binary operator ( + ) – needs two arguments.
#include <iostream>
using namespace std;
class Number {
int value;
public:
Number(int v=0) { // constructor
value = v;
}
// Declare friend function to overload +
friend Number operator+(Number a, Number b);
void display() {
cout << value << endl;
}
};
// Friend function definition
Number operator+(Number a, Number b) {
return Number(a.value + b.value);
}
int main() {
Number n1(5), n2(10);
Number n3 = n1 + n2; // uses overloaded + (friend function)
n3.display(); // Output: 15
return 0;
}Restrictions of overloading
- It cannot create a new operator; it will overload the existing operator only.
- At least one operand must be a user-defined type.
- Cannot change operator precedence.
Operators That Cannot be Overloaded
The following operators cannot be overloaded in C++.
- Scope resolution (::)
- typeid
- sizeof
- Ternary / conditional operator (?:)
- Class member access operators (. and .*)
Disclaimer: We have provide you with the accurate handout of “Operator Overloading in C++“. If you feel that there is any error or mistake, please contact me at anuraganand2017@gmail.com. The above study material present on our websites is for education purpose, not our copyrights.
Images and content shown above are the property of individual organisations and are used here for reference purposes only. To make it easy to understand, some of the content and images are generated by AI and cross-checked by the teachers.