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

In this chapter we shall see:
1. Overloading prefix increment operator without return type
2. Overloading prefix increment operator with return type
3. Overloading Postfix increment operator
4. Overloading prefix and postfix decrement operator

1. Overloading prefix increment operator without return type

Consider the example below;
#include <iostream>
using namespace std;

class MyClass
{
    private:
        int num;
    
    public:
        MyClass(): num(5){ }
    
    void operator ++ ()
    {
        cout<<"The value of num before increment is "<<num<<endl;
        num = num + 1;
        cout<<"The value of num after increment is "<<num<<endl;
    }
};

int main(void)
{
    MyClass obj;
    
    ++obj;
}

Output:

The value of num before increment is 5
The value of num after increment is 6

But there is an error in above program.
You cannot do “obj1 = obj++;”
Because, the overloaded function is not returning anything.

Hence for that statement to work, you need to return the class type. We shall see how to do that in below example:

2. Overloading prefix increment operator with return type

#include <iostream>
using namespace std;

class MyClass
{
    private:
        int num;
    
    public:
        MyClass(): num(5){ }
    
    MyClass operator ++ ()
    {
        //create a temp object
        MyClass temp;
        
        //increment the data member and store in temp object
        temp.num = num + 1;
        
        //return the temp object
        return temp;
    }
    
    void display()
    {
        cout<<"The value of num = "<<num<<endl;
    }
};

int main(void)
{
    MyClass obj;
    MyClass obj_1;
    
    obj_1 = ++obj;
    obj_1.display();
    
}

The above program will work for both “++obj” and “obj_1 = ++obj;” statements. Because we are returning a class type object.

3. Overloading Postfix increment operator

Till now we have seen prefix increment operator. For postfix operator, we need to include “int” in the parameter list of the function.
This is as shown below:

#include <iostream>
using namespace std;

class MyClass
{
    private:
        int num;
    
    public:
        MyClass(): num(5){ }
    
    MyClass operator ++ ()
    {
        cout<<"In prefix"<<endl;
        //create a temp object
        MyClass temp;
        
        //increment the data member and store in temp object
        temp.num = num + 1;
        
        //return the temp object
        return temp;
    }
    
    //postfix increment operator
    MyClass operator ++ (int)
    {
        cout<<"In Postfix"<<endl;
        //create a temp object
        MyClass temp;
        
        //increment the data member and store in temp object
        temp.num = num + 1;
        
        //return the temp object
        return temp;
    }
        
    void display()
    {
        cout<<"The value of num = "<<num<<endl;
    }
};

int main(void)
{
    MyClass obj;

    ++obj;
    
    obj ++;
}

Output:

In prefix
In Postfix

Note:

Notice the “(int)” in function argument for postfix operator. This will give information to the compiler that we have called postfix version of the operator.

4. Overloading prefix and postfix decrement operator

#include <iostream>
using namespace std;

class MyClass
{
    private:
        int num;
    
    public:
        MyClass(): num(5){ }
    
    //prefix decrement operator
    MyClass operator -- ()
    {
        cout<<"In prefix"<<endl;
        //create a temp object
        MyClass temp;
        
        //decrement the data member and store in temp object
        temp.num = num - 1;
        
        //return the temp object
        return temp;
    }
    
    //postfix decrement operator
    MyClass operator -- (int)
    {
        cout<<"In Postfix"<<endl;
        //create a temp object
        MyClass temp;
        
        //decrement the data member and store in temp object
        temp.num = num - 1;
        
        //return the temp object
        return temp;
    }
        
    void display()
    {
        cout<<"The value of num = "<<num<<endl;
    }
};

int main(void)
{
    MyClass obj;

    --obj;
    
    obj--;
}

Output:

In prefix
In Postfix
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *