Overloading Postfix and Prefix ( ++ , –) Increment and Decrements Operators in C++

Operator Overloading:

In object-oriented programming, operator overloading allows a programmer to redefine a built-in operator to work with user-defined data types.
Let’s pretend we’ve created a class called Integer to handle integer operations. To handle the various operations, we can use the functions add(), subtract(), multiply(), and divide(). However, it is preferable to use operators that correspond to the given operations(+, -, *, and /, respectively) to make the code more understandable and improve readability, i.e. we can substitute the following code with the following code.

Replace

obj5 = multiply(subtract(i1, i2), add(i3, i4))

by a simpler code:

i5 = (i1 - i2) * (i3 + i4)

We’ll look at how to overload postfix and prefix increment and decrement operators in C++ in this article.

There are two types of increment and decrement operators: prefix and postfix.

Overload Prefix and Postfix Operators

1)Prefix Decrement  (–x) and Prefix Increment (++x) Operator

The symbol ++ or — falls before the operand in prefix increment or decrement operators, i.e. ++x and –x.

The prefix operator performs the operation first (increment or decrement) and then returns the modified value, i.e.

int p = 1;
// Prefix increment operator
int q = ++p;
// q is 2

Explanation : It increments p first, then returns the modified value of p, which is then assigned to q.

int p = 1;
// Prefix decrement operator
int q = --p;
// q is 0

Explanation : It decrements p’s value before returning the modified p’s value, which is then assigned to q.

2)Postfix Decrement(x–) and Postfix Increment(x++) Operator

The symbol ++ or — appears after the operand in postfix increment or decrement operators, e.g. x++ and x–-.

After making a temporary copy of the current value, the postfix operator performs the operation (increment or decrement) on the item. The temporary value is then returned.

int p = 1;
// postfix increment operator
int q = p++;
// q is 2

Explanation:

The current value of p is saved in a temp and then incremented by postfix increment. Finally, it returned the value stored in temp, which was the previous value of p. Which was assigned to the variable q.

int p = 1;
// postfix Decrement operator
int q = p--;
// q is 0

Explanation:

The current value of p is stored in a temp and then decremented by the postfix decrement. Finally, it returned the value stored in temp, which was the previous value of p. Which was assigned to the variable q.

3)Prefix Increment Overloading

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
class Preincrement {
private:
    int i;

public:
    // Parameterised constructor
    Preincrement(int i = 0) { this->i = i; }

    // overloading the prefix increment operator
    Preincrement operator++()
    {
        Preincrement temporary;
        temporary.i = ++i;
        return temporary;
    }

    // function which prints the value of i
    void display() { cout << "value of i = " << i << endl; }
};

int main()
{
    Preincrement obj1(9);
   
    cout << "Before prefix increment: ";
    obj1.display();

    // using prefix increment operator
    Preincrement obj2 = ++obj1;

    cout << "After prefix increment: ";
    obj2.display();
}

Output:

Before prefix increment: value of i = 9
After prefix increment: value of i = 10

4)Prefix Decrement Overloading

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
class PreDecrement {
private:
    int i;

public:
    // Parameterised constructor
    PreDecrement(int i = 0) { this->i = i; }

    // overloading the prefix Decrement operator
    PreDecrement operator--()
    {
        PreDecrement temporary;
        temporary.i = --i;
        return temporary;
    }

    // function which prints the value of i
    void display() { cout << "value of i = " << i << endl; }
};

int main()
{
    PreDecrement obj1(9);

    cout << "Before prefix Decrement: ";
    obj1.display();

    // using prefix Decrement operator
    PreDecrement obj2 = --obj1;

    cout << "After prefix Decrement: ";
    obj2.display();
}

Output:

Before prefix Decrement: value of i = 9
After prefix Decrement: value of i = 8

5)Postfix Increment Overloading

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
class PostIncrement {
private:
    int i;

public:
    // Parameterised constructor
    PostIncrement(int i = 0) { this->i = i; }

    // overloading the postfix Increment operator
    PostIncrement operator++(int)
    {
        PostIncrement temporary;
        temporary.i = i++;
        return temporary;
    }

    // function which prints the value of i
    void display() { cout << "value of i = " << i << endl; }
};

int main()
{
    PostIncrement obj1(9);

    cout << "Before postfix Increment: ";
    obj1.display();

    // using postfix Increment operator
    PostIncrement obj2 = obj1++;

    cout << "After postfix Increment: ";
    obj2.display();
}

Output:

Before postfix Increment : value of i = 9
After postfix Increment : value of i = 9

6)Postfix Decrement Overloading

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
class PostDecrement {
private:
    int i;

public:
    // Parameterised constructor
    PostDecrement(int i = 0) { this->i = i; }

    // overloading the postfix Decrement operator
    PostDecrement operator--(int)
    {
        PostDecrement temporary;
        temporary.i = i--;
        return temporary;
    }

    // function which prints the value of i
    void display() { cout << "value of i = " << i << endl; }
};

int main()
{
    PostDecrement obj1(9);

    cout << "Before postfix Decrement: ";
    obj1.display();

    // using postfix Decrement operator
    PostDecrement obj2 = obj1--;

    cout << "After postfix Decrement: ";
    obj2.display();
}

Output:

Before postfix Decrement: value of i = 9
After postfix Decrement: value of i = 9

Related Programs: