Open In App

Increment (++) and Decrement (–) Operator Overloading in C++

Improve
Improve
Like Article
Like
Save
Share
Report

Operator overloading is a feature in object-oriented programming which allows a programmer to redefine a built-in operator to work with user-defined data types.

Why Operator Overloading? 
 

Let’s say we have defined a class Integer for handling operations on integers. We can have functions add(), subtract(), multiply() and divide() for handling the respective operations. However, to make the code more intuitive and enhance readability, it is preferred to use operators that correspond to the given operations(+, -, *, / respectively) i.e. we can replace the following code.
Example: 

Replace
i5 = divide(add(i1, i2), subtract(i3, i4))

by a simpler code:
i5 = (i1 + i2) / (i3 - i4) 

Overloading the Increment Operator

The operator symbol for both prefix(++i) and postfix(i++) are the same. Hence, we need two different function definitions to distinguish between them. This is achieved by passing a dummy int parameter in the postfix version.
Here is the code to demonstrate the same.

Example: Pre-increment overloading 

CPP




// C++ program to demonstrate
// prefix increment operator overloading
 
#include <bits/stdc++.h>
using namespace std;
 
class Integer {
private:
    int i;
 
public:
    // Parameterised constructor
    Integer(int i = 0)
    {
        this->i = i;
    }
 
    // Overloading the prefix operator
    Integer& operator++()
    {
        ++i;
        // returned value should be a reference to *this
        return *this;
    }
 
    // Function to display the value of i
    void display()
    {
        cout << "i = " << i << endl;
    }
};
 
// Driver function
int main()
{
    Integer i1(3);
 
    cout << "Before increment: ";
    i1.display();
 
    // Using the pre-increment operator
    Integer i2 = ++i1;
 
    cout << "After pre increment: " << endl;
    cout << "i1: ";
    i1.display();
    cout << "i2: "
    i2.display();
}


Output

Before increment: i = 3
After post decrement: 
i1: i = 4
i2: i = 4

Example: Post-Increment Overloading

CPP




// C++ program to demonstrate
// postfix increment operator
// overloading
#include <bits/stdc++.h>
using namespace std;
 
class Integer {
private:
    int i;
 
public:
    // Parameterised constructor
    Integer(int i = 0)
    {
        this->i = i;
    }
 
    // Overloading the postfix operator
    Integer operator++(int)
    {
        // returned value should be a copy of the object before increment
        Integer temp = *this;
        ++i;
        return temp;
    }
 
    // Function to display the value of i
    void display()
    {
        cout << "i = " << i << endl;
    }
};
 
// Driver function
int main()
{
    Integer i1(3);
 
    cout << "Before increment: ";
    i1.display();
 
    // Using the post-increment operator
    Integer i2 = i1++;
 
    cout << "After post increment: " << endl;
    cout << "i1: ";
    i1.display();
    cout << "i2: "
    i2.display();
}


Output

Before increment: i = 3
After post increment: 
i1: i = 4
i2: i = 3

Overloading the Decrement Operator

Similarly, we can also overload the decrement operator as follows:

Example: Pre-Decrement Overloading

CPP




// C++ program to demonstrate
// prefix decrement operator
// overloading
 
#include <bits/stdc++.h>
using namespace std;
 
class Integer {
private:
    int i;
 
public:
    // Parameterised constructor
    Integer(int i = 0)
    {
        this->i = i;
    }
 
    // Overloading the prefix operator
    Integer& operator--()
    {
        --i;
        // returned value should be a reference to *this
        return *this;
    }
 
    // Function to display the value of i
    void display()
    {
        cout << "i = " << i << endl;
    }
};
 
// Driver function
int main()
{
    Integer i1(3);
 
    cout << "Before decrement: ";
    i1.display();
 
    // Using the pre-decrement operator
    Integer i2 = --i1;
 
    cout << "After pre decrement: " << endl;
    cout << "i1: ";
    i1.display();
    cout << "i2: "
    i2.display();
}


Output

Before decrement: i = 3
After pre decrement: 
i1: i = 2
i2: i = 2

Example: Post-Decrement Overloading

CPP




// C++ program to demonstrate
// postfix decrement operator
// overloading
#include <bits/stdc++.h>
using namespace std;
 
class Integer {
private:
    int i;
 
public:
    // Parameterised constructor
    Integer(int i = 0)
    {
        this->i = i;
    }
 
    // Overloading the postfix operator
    Integer operator--(int)
    {
        // returned value should be a copy of the object before decrement
        Integer temp = *this;
        --i;
        return temp;
    }
 
    // Function to display the value of i
    void display()
    {
        cout << "i = " << i << endl;
    }
};
 
// Driver function
int main()
{
    Integer i1(3);
 
    cout << "Before decrement: ";
    i1.display();
 
    // Using the post-decrement operator
    Integer i2 = i1--;
 
    cout << "After post decrement: " << endl;
    cout << "i1: ";
    i1.display();
    cout << "i2: "
    i2.display();
}


Output

Before decrement: i = 3
After post decrement: 
i1: i = 2
i2: i = 3


Last Updated : 16 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads