Operator overloading allows user-defined types to redefine the behavior of operators. In C++, the increment (++) and decrement (--) operators can be overloaded to work with objects in a way similar to built-in data types.
- Allows objects to be incremented or decremented using ++ and --.
- Supports both prefix and postfix forms of the operators.
- Improves code readability and makes custom classes easier to use.
Increment (++) Operator Overloading in C++
The increment operator (++) is used to increase the value of an object. It can be overloaded to define custom increment behavior for user-defined classes.
- Can be overloaded as prefix (++obj) or postfix (obj++).
- prefix increment modifies the object before returning it.
- postfix increment returns the original value and then increments the object.
Prefix Increment Operator Overloading:
Syntax
ClassName operator++(){
// increment logic
}
#include <iostream>
using namespace std;
class Counter {
private:
int count;
public:
Counter(int c = 0) : count(c) {}
Counter operator++() {
++count;
return *this;
}
void display() {
cout << "Count = " << count << endl;
}
};
int main() {
Counter c(5);
++c;
c.display();
return 0;
}
Output
Count = 6
Explanation
- The object c is initialized with the value 5.
- The prefix operator ++c increases the value before it is used.
- The updated value becomes 6.
Postfix Increment Operator Overloading:
Syntax:
ClassName operator++(int){
// increment logic
}
#include <iostream>
using namespace std;
class Counter {
private:
int count;
public:
Counter(int c = 0) : count(c) {}
Counter operator++(int) {
Counter temp = *this;
count++;
return temp;
}
void display() {
cout << "Count = " << count << endl;
}
};
int main() {
Counter c(5);
c++;
c.display();
return 0;
}
Output
Count = 6
Explanation
- The postfix operator uses a dummy int parameter to distinguish it from the prefix version.
- The original value is stored in a temporary object.
- The object is incremented after returning the original value.
Decrement (--) Operator Overloading in C++
The decrement operator (--) is used to decrease the value of an object. Similar to the increment operator, it can be overloaded in both prefix and postfix forms.
- Supports both prefix (--obj) and postfix (obj--) forms.
- Prefix decrement decreases the value before returning it.
- Postfix decrement returns the original value and then decreases it.
Prefix Decrement Operator Overloading:
Syntax:
ClassName operator--(){
// decrement logic
}
#include <iostream>
using namespace std;
class Counter {
private:
int count;
public:
Counter(int c = 0) : count(c) {}
Counter operator--() {
--count;
return *this;
}
void display() {
cout << "Count = " << count << endl;
}
};
int main() {
Counter c(5);
--c;
c.display();
return 0;
}
Output
Count = 4
Explanation
- The object starts with the value 5.
- The prefix decrement operator decreases the value before it is used.
- The updated value becomes 4.
Postfix Decrement Operator Overloading:
Syntax:
ClassName operator--(int){
// decrement logic
}
#include <iostream>
using namespace std;
class Counter {
private:
int count;
public:
Counter(int c = 0) : count(c) {}
Counter operator--(int) {
Counter temp = *this;
count--;
return temp;
}
void display() {
cout << "Count = " << count << endl;
}
};
int main() {
Counter c(5);
c--;
c.display();
return 0;
}
Output
Count = 4
Explanation
- The postfix decrement operator first stores the original value.
- The object's value is then decreased.
- The updated value becomes 4.
Prefix vs Postfix Operator Overloading
| Feature | Prefix (++obj / --obj) | Postfix (obj++ / obj--) |
|---|---|---|
| Syntax | operator++() | operator++(int) |
| Syntax | operator--() | operator--(int) |
| Execution | Value changes before use | Value changes after use |
| Return Value | Returns updated object | Returns original object |
| Performance | Slightly faster | Slightly slower due to temporary object |
| Dummy Parameter | Not required | Requires int parameter |