// Overload ++ when used as prefix
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
// Constructor to initialize count to 5
Count() : value(5) {}
// Overload ++ when used as prefix
void operator ++ () {
++value;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
// Call the "void operator ++ ()" function
++count1;
count1.display();
return 0;
}
Output
Count: 6
// Overload ++ when used as prefix and postfix
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
// Constructor to initialize count to 5
Count() : value(5) {}
// Overload ++ when used as prefix
void operator ++ () {
++value;
}
// Overload ++ when used as postfix
void operator ++ (int) {
value++;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
// Call the "void operator ++ (int)" function
count1++;
count1.display();
// Call the "void operator ++ ()" function
++count1;
count1.display();
return 0;
}
Output
Count: 6
Count: 7
Return Value from Operator Function (++ Operator)
#include <iostream>
using namespace std;
class Count {
private:
int value;
public
:
// Constructor to initialize count to 5
Count() : value(5) {}
// Overload ++ when used as prefix
Count operator ++ () {
Count temp;
// Here, value is the value attribute of the calling object
temp.value = ++value;
return temp;
}
// Overload ++ when used as postfix
Count operator ++ (int) {
Count temp;
// Here, value is the value attribute of the calling object
temp.value = value++;
return temp;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1, result;
// Call the "Count operator ++ ()" function
result = ++count1;
result.display();
// Call the "Count operator ++ (int)" function
result = count1++;
result.display();
return 0;
}
Output
Count: 6
Count: 6
// C++ program to overload the binary operator +
// This program adds two complex numbers
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
// Constructor to initialize real and imag to 0
Complex() : real(0), imag(0) {}
void input() {
cout << "Enter real and imaginary parts respectively: ";
cin >> real;
cin >> imag;
}
// Overload the + operator
Complex operator + (const Complex& obj) {
Complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
void output() {
if (imag < 0)
cout << "Output Complex number: " << real << imag << "i";
else
cout << "Output Complex number: " << real << "+" << imag << "i";
}
};
int main() {
Complex complex1, complex2, result;
cout << "Enter first complex number:\n";
complex1.input();
cout << "Enter second complex number:\n";
complex2.input();
// complex1 calls the operator function
// complex2 is passed as an argument to the function
result = complex1 + complex2;
result.output();
return 0;
}
Output
Enter first complex number:
Enter real and imaginary parts respectively: 9 5
Enter second complex number:
Enter real and imaginary parts respectively: 7 6
Output Complex number: 16+11i
Things to Remember in C++ Operator Overloading
Two operators = and & are already overloaded by default in C++. For example, to copy objects
of the same class, we can directly use the = operator. We do not need to create an operator
function.
Operator overloading cannot change the precedence and associativity of operators. However, if
we want to change the order of evaluation, parentheses should be used.
There are 4 operators that cannot be overloaded in C++. They are:
:: (scope resolution)
. (member selection)
.* (member selection through pointer to function)
?: (ternary operator)