Overloading Increment and Decrement Operators Using Class in C++

Exercise:

Write a C++ Program that overloads the decrement and increment operators.

Example 1: Increment Operator with return type:

Click Here to View the Solution:
#include <iostream>
using namespace std;
class Test
{
    private:
    int x;
    public:
    Test(): x(0) {  }
    // Return type is Test
    Test operator ++()
    {
        Test TempVar;
        ++x;
        TempVar.x = x;
        return TempVar;
    } 
    void View() 
    {  
        cout << "x = " << x << endl;
    }
};
int main()
{
    Test val, val1;
    val.View();
    val1.View();
    val1 = ++val;
    val.View();
    val1.View();
    return 0;
}
Click Here to View the Output:
x = 0
x = 0
x = 1
x = 1
Click Here to View the Explanation:
  • A class is created which has public and private variables. The class is named as Test.
  • A private variable x is declared and an object is created in public TempVar. The value of x is initialized as 0.
  • The value of x is incremented and put assigned to TempVar.
  • a void type view () is created to display the answer.
  • 2 variables val and val1 are created in int main ().
  • The view () function is used to display the values of val and val1.
  • The values are incremented and displayed again using view () function.

Example 2: Increment Operator without return type:

Click Here to View the Solution:
#include <iostream>
using namespace std;
class Test
{
    private:
    int x;
    public:
    Test(): x(0) {  }
    void operator ++() 
    {
        ++x;
    }
    void view() 
    { 
        cout << "x=" << x << endl;
    }
};
int main()
{
    Test val;
    // views the value of data member x for object val
    val.view();
    // calls operator function void operator ++( )
    ++val; 
    // views the value of data member x for object val
    val.view();
    return 0;
}
Click Here to View the Output:
x=0
x=1
Click Here to View the Explanation:
  • This code is similar to the one above.
  • The difference is that a void operator is declared to increment the value of x. Since it is void type, it has no return type.
  • The result is displayed on the screen in a similar manner.

Example 3: Overloading of increment operator (Postfix):

Click Here to View the Solution:
#include <iostream>
using namespace std;
class Test
{
    private:
    int x;
    public:
    Test(): x(0) {  }
    Test operator ++ ()
    {
        Test TempVar;
        TempVar.x = ++x;
        return TempVar;
    }
    //  int inside barcket shows postfix increment.
    Test operator ++ (int)
    {
        Test TempVar;
        TempVar.x = x++;
        return TempVar;
    }
    void View()
    { 
        cout << "x = "<< x <<endl;
    }
};
int main()
{
    Test val, val1;    
    val.View(); 
    val1.View();
    // Operator function is called, and then value of val is assigned to val1
    val1 = ++val;
    val.View();
    val1.View();
    // Assigns value of val to val1, and then operator function is
called
    val1 = val++;
    val.View();
    val1.View();
    return 0;
}
Click Here to View the Output:
 x = 0
 x = 0
 x = 1
 x = 1
 x = 2
 x = 1
Click Here to View the Explanation:
  • This code is similar to the code above. Overloading of the increment operator is done by a prefix form test operator ++ ()
  • The postfix form is used after the overloading of the increment operator test operator (int). int in this is not used to describe int type. This int provides information to the compiler that the version is the postfixed one.

Example 4: Decrement Operator Overloading:

Click Here to View the Solution:
#include <iostream>
using namespace std;
class Test
{
    private:
    int x;
    public:
    Test(): x(3) {  }
    Test operator -- ()
    {
        Test TempVar;
        TempVar.x = --x;
        return TempVar;
    }
    // int inside barcket shows postfix decrement.
    Test operator -- (int)
    {
        Test TempVar;
        TempVar.x = x--;
        return TempVar;
    }
    void View()
    { 
        cout << "x = "<< x <<endl;
    }
};
int main()
{
    Test obj, obj1;    
    obj.View(); 
    obj1.View();
    // value of obj is assigned to obj1 when calling operator function.
    obj1 = --obj;
    obj.View();
    obj1.View();
    // value of obj is assigned to obj1, when calling operator function.
    obj1 = obj--;
    obj.View();
    obj1.View();
    return 0;
}
Click Here to View the Output:
 x = 3
 x = 3
 x = 2
 x = 2
 x = 1
 x = 2
Click Here to View the Explanation:
  • The code shows that a class can be created in a similar manner for the decrement operator overloading, too.
%d bloggers like this: