Operator overloading in C++ allows you to redefine how operators work for user-defined classes. It makes programs more natural, readable, and easier to understand.
What is Operator Overloading?
Operator overloading means giving additional meaning to operators such as +, -, *, and == when they are used with objects.
Instead of using separate functions, operators can directly work with class objects.
Why Use Practical Examples?
Practical examples help you understand how operator overloading is used in real programming situations rather than only learning theory.
Example 1: Adding Two Objects
This example overloads the + operator to add two complex numbers.
#include <iostream>
using namespace std;
class Complex {
public:
int real, imag;
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
Complex operator + (Complex obj) {
Complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(2, 3), c2(4, 5);
Complex c3 = c1 + c2;
c3.display();
return 0;
}
Output
6 + 8i
How It Works
c1 + c2uses overloaded+operator- Real parts are added separately
- Imaginary parts are added separately
- A new object is returned
Example 2: Comparing Two Objects
This example overloads the > operator.
#include <iostream>
using namespace std;
class Number {
public:
int value;
Number(int v) {
value = v;
}
bool operator > (Number obj) {
return value > obj.value;
}
};
int main() {
Number n1(10), n2(5);
if (n1 > n2) {
cout << "n1 is greater";
} else {
cout << "n2 is greater";
}
return 0;
}
Output
n1 is greater
How It Works
>operator compares object values- Returns
trueif first object is greater - Simplifies object comparison
Example 3: Increment Operator Overloading
This example overloads the ++ operator.
#include <iostream>
using namespace std;
class Counter {
public:
int count;
Counter() {
count = 0;
}
void operator ++ () {
count++;
}
void display() {
cout << count << endl;
}
};
int main() {
Counter c;
++c;
++c;
c.display();
return 0;
}
Output
2
How It Works
++ccalls overloaded increment operator- Value increases automatically
- Makes object behavior natural
Example 4: Multiplying Two Objects
This example overloads the * operator.
#include <iostream>
using namespace std;
class Multiply {
public:
int value;
Multiply(int v) {
value = v;
}
Multiply operator * (Multiply obj) {
Multiply temp(0);
temp.value = value * obj.value;
return temp;
}
};
int main() {
Multiply m1(5), m2(4);
Multiply result = m1 * m2;
cout << result.value;
return 0;
}
Output
20
Example 5: Equality Operator Overloading
This example overloads the == operator.
#include <iostream>
using namespace std;
class Test {
public:
int value;
Test(int v) {
value = v;
}
bool operator == (Test obj) {
return value == obj.value;
}
};
int main() {
Test t1(100), t2(100);
if (t1 == t2) {
cout << "Equal";
} else {
cout << "Not Equal";
}
return 0;
}
Output
Equal
Real-Life Uses of Operator Overloading
Operator overloading is widely used in:
- Complex number calculations
- Matrix operations
- Game development
- Mathematical libraries
- Graphics programming
- Custom string handling
Advantages of Operator Overloading
- Makes code more readable
- Simplifies complex operations
- Improves code maintainability
- Supports object-oriented programming
- Makes custom classes behave naturally
Important Points
- Only existing operators can be overloaded
- New operators cannot be created
- Some operators cannot be overloaded such as:
::sizeof?:
Why Practical Examples are Important
Practical examples help you:
- Understand real-world implementation
- Improve problem-solving skills
- Learn OOP concepts deeply
- Write cleaner and professional code
Conclusion
Practical examples of operator overloading in C++ demonstrate how operators can work with objects naturally and efficiently. By overloading operators like +, >, and ++, developers can create cleaner, more readable, and powerful object-oriented programs.