Increment operator overloading in C++

Share Your Love

Hello guys, In this post today we are discussing increment operator overloading in c++.

In object-oriented programming, operator overloading allows a programmer to rewrite a built-in operator to work with user-defined data types.

Increment operator overloading is done in 2 ways-

  1. Post-Increment Operator Overloading
  2. Pre-Increment Operator Overloading

Why operator overloading? 

Let’s pretend we’ve created a class called Integer to handle integer operations. To handle the various operations, we can use the methods add(), subtract(), multiply(), and division().

However, it is preferable to use operators that correspond to the supplied operations(+, -, *, and /, respectively) to make the code more intuitive and better readability, i.e. we can replace the following code-

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

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

Overloading the increment operator:

Both prefix(++i) and postfix(i++) have the same operator symbol. As a result, we’ll need two different function definitions to tell them apart. In the postfix version, this is accomplished by giving a dummy int parameter.

Now we code it and see how increment operator overloading works?

Join Our Community

Join our WhatsApp Group To know more about Programming Language tips, tricks and knowledge about and how to start learning any programming language.

Post Increment Operator Overloading:

// Post Increment Operator Overloading

#include <iostream>

using namespace std;

class PostIncrement
{
public:
    int i;

    void setData(int i = 0)
    {
        this->i = i;
    }

    // Post Increment Operator Overloading
    PostIncrement operator++(int)
    {
        PostIncrement P;
        P.i = i++;
        return P;
    }

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

int main()
{
    PostIncrement P;
    P.setData(3);
    P.display(); // 3
    PostIncrement P2 = P++;
    P2.display(); // 3
    P.display();  // 4
    return 0;
}

Output:

i = 3
i = 3
i = 4

Read More: Decrement Operator Overloading In C++

Note

Pre-Increment Operator Overloading:

// Pre - Increment Operator Overloading

#include <iostream>

using namespace std;

class Increment
{
public:
    int i;

    void setData(int i = 0)
    {
        this->i = i;
    }

    // pre-increment operator overloading
    Increment operator++()
    {
        Increment I;
        I.i = ++i;
        return I;
    }

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

int main()
{
    Increment I;
    I.setData(2);
    I.display(); // 2
    Increment I2 = ++I;
    I2.display(); // 3
}

Output:

i= 2
i= 3

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 429

Newsletter Updates

Enter your email address below to subscribe to our newsletter