Increment (++) and Decrement () Operator Overloading in C++ (original) (raw)

Last Updated : 4 Jun, 2026

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.

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.

**Prefix Increment Operator Overloading:

**Syntax

ClassName operator++(){
// increment logic
}

C++ `

#include 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;

}

`

Explanation

**Postfix Increment Operator Overloading:

**Syntax:

ClassName operator++(int){
// increment logic
}

C++ `

#include 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;

}

`

Explanation

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.

**Prefix Decrement Operator Overloading:

**Syntax:

ClassName operator--(){
// decrement logic
}

C++ `

#include 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;

}

`

Explanation

**Postfix Decrement Operator Overloading:

**Syntax:

ClassName operator--(int){
// decrement logic
}

C++ `

#include 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;

}

`

Explanation

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