Increment ++ and Decrement -- Operator Overloading in C++.

Increment ++ and Decrement -- Operator Overloading

Increment Operator Overloading
/* C++ program to demonstrate the overloading of ++ operator. */

#include <iostream.h>
#include <conio.h>
class Check
{
    private:
       int i;
    public:
       Check(): i(0) {  }
       void operator ++()
          { ++i; }
       void Display()
          { cout<<"i="<<i<<endl; }
};
int main()
{
    Check obj;

 /* Displays the value of data member i for object obj */
    obj.Display();

/* Invokes operator function void operator ++( ) */
    ++obj;
 
/* Displays the value of data member i for object obj */
    obj.Display(); 
    return 0;
}
Expected Output:-
i=0
i=1
Explanation
  • Initially when the object obj is declared the data member I value for the the object is 0(constructor initializes I to 0). 
  • The operator function void operator++) (is invoked when++ operator is worked on obj, which increases the data member value I to 1. 
  • This program is not complete in the sense that you can not use code:
obj1=++obj;
  • It is because the return type of operator function in above program is void. Here is the little modification of above program so that you can use code obj1=++obj.
/* C++ program to demonstrate the working of ++ operator overloading. */

#include <iostream.h>
#include <conio.h>

class Check
{
  private:
    int i;
  public:
    Check(): i(0) {  }
    Check operator ++() /* Notice, return type Check*/
    {
       Check temp;  /* Temporary object check created */
       ++i;         /* i increased by 1. */
       temp.i=i;   /* i of object temp is given same value as i */
       return temp; /* Returning object temp */
    }
   void Display()
      { cout<<"i="<<i<<endl; }
};
int main()
{
    Check obj, obj1;
    obj.Display();
    obj1.Display();
    obj1=++obj;
    obj.Display();
    obj1.Display();
    return 0;
}
Expected Output:-
i=0
i=0
i=1
i=1
  • This program is similar to above program. 
  • The only difference is that, the return type of operator function is Check in this case which allows to use both codes ++obj; obj1=++obj;
  • It is because, temp returned from operator function is stored in object obj. Since, the return type of operator function is Check, you can also assign the value of obj to another object. 
  • Notice that, = (assignment operator) does not need to be overloaded because this operator is already overloaded in C++ library.
Operator Overloading of Post-fix Operator
  • Up to this point, increment operator overloading is valid only if it is used in prefix form. 
  • It is the software update above to make it function with both prefix form as well as post-fix form.
/* C++ program to demonstrate the working of ++ operator overloading. *

#include <iostream.h>
#include <conio.h>

class Check
{
  private:
    int i;
  public:
    Check(): i(0) {  }
    Check operator ++ ()
    {
        Check temp;
        temp.i=++i;
        return temp;
    }

/* Notice int inside barcket which indicates postfix increment. */
    Check operator ++ (int)
    {
        Check temp;
        temp.i=i++;
        return temp;
    }
   void Display()
      { cout<<"i="<<i<<endl; }
};
int main()
{
    Check obj, obj1;   
    obj.Display();
    obj1.Display();
    obj1=++obj;     /* Operator function is called then only value of obj is assigned to obj1. */
    obj.Display();
    obj1.Display();
    obj1=obj++;     /* Assigns value of obj to obj1++ then only operator function is called. */
    obj.Display();
    obj1.Display();
    return 0;
}
Expected Output:-
i=0
i=0
i=1
i=1
i=2
i=1
  • When increment operator is overloaded in prefix form; Check operator ++ () is called but, when increment operator is overloaded in post-fix form; Check operator ++ (int) is invoked. 
  • Notice, the int inside bracket. This int gives information to the compiler that it is the post-fix version of operator. Don't confuse this int doesn't indicate integer.
Operator Overloading of Decrement -- Operator

#include <iostream.h>
#include <conio.h>

class Check
{
  private:
    int i;
  public:
    Check(): i(3) {  }
    Check operator -- ()
    {
        Check temp;
        temp.i = --i;
        return temp;
    }

    // Notice int inside barcket which indicates postfix decrement.
    Check operator -- (int)
    {
        Check temp;
        temp.i = i--;
        return temp;
    }

    void Display()
    { cout << "i = "<< i <<endl; }
};

int main()
{
    Check obj, obj1;    
    obj.Display(); 
    obj1.Display();

    // Operator function is called, only then value of obj is assigned to obj1
    obj1 = --obj;
    obj.Display();
    obj1.Display();

    // Assigns value of obj to obj1, only then operator function is called.
    obj1 = obj--;
    obj.Display();
    obj1.Display();

    return 0;
}
Expected Output:-

i = 3
i = 3
i = 2
i = 2
i = 1
i = 2

Post a Comment

Previous Post Next Post