Algorithm


** Overloading Increment (++) Operator:

1. Define a Class:
    i. Create a class for which you want to overload the ++ operator.
2. Declare Overloaded Operator in the Class:
    i. Inside the class declaration, declare the ++ operator function. The declaration might look like:
      'ReturnType operator++()';

3. Define Overloaded Operator Outside the Class:
     i. Outside the class, define the overloaded ++ operator. This is where you specify the behavior of the    operator. The function signature should be similar to: ReturnType ClassName::operator++() { /* Implementation */ }

4. Implement Increment Operation:
     i. Inside the definition, write the code for the increment operation. Usually, this involves modifying the state of the object.

5. Return Result:
        Ensure the function returns the modified object or a new object as needed.


** Overloading Decrement (--) Operator:

  1. Define a Class:
      Create a class for which you want to overload the -- operator.

   2. Declare Overloaded Operator in the Class:
        Inside the class declaration, declare the -- operator function. The declaration might look like: ReturnType operator--();

   3. Define Overloaded Operator Outside the Class:
        Outside the class, define the overloaded -- operator. This is where you specify the behavior of the operator. The function signature should be similar to: ReturnType ClassName::operator--() { /* Implementation */ }

   4. Implement Decrement Operation:
        Inside the definition, write the code for the decrement operation. Usually, this involves modifying the state of the object.

   5. Return Result:
        Ensure the function returns the modified object or a new object as needed.

Remember to handle pre-increment, post-increment, pre-decrement, and post-decrement variations as per your requirements.

Code Examples

#1 Code Example- C++ Programing Prefix ++ Increment Operator Overloading with no return type

Code - C++ Programming

#include <iostream>
using namespace std;

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;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
i=0
i=1

#2 Code Example- C++ programing Prefix Increment ++ operator overloading with return type

Code - C++ Programming

#include <iostream>
using namespace std;

class Check
{
  private:
    int i;
  public:
    Check(): i(0) {  }

    // Return type is Check
    Check operator ++()
    {
       Check temp;
       ++i;
       temp.i = i;

       return temp;
    }

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

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

    obj1 = ++obj;

    obj.Display();
    obj1.Display();

    return 0;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
i = 0
i = 0
i = 1
i = 1

#3 Code Example- C++ Programing Postfix Increment ++ Operator Overloading

Code - C++ Programming

#include <iostream>
using namespace std;

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();

    // 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;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
i = 0
i = 0
i = 1
i = 1
i = 2
i = 1

#4 Code Example- C++ Programing Operator Overloading of Decrement -- Operator

Code - C++ Programming

#include <iostream>
using namespace std;

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;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
i = 3
i = 3
i = 2
i = 2
i = 1
i = 2
Advertisements

Demonstration


C++ Programing Example of Increment ++ and Decrement -- Operator Overloading-DevsEnv