Decrement Operator Overloading In C++

Hello guys, In this post today we are discussing decrement 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-Decrement Operator Overloading
  2. Pre-Decrement 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 Decrement 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 decrement 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.

Read More: Increment Operator Overloading In C++

Note

Post Decrement Operator Overloading:

// post decrement operator overloading

#include <iostream>

using namespace std;

class PostDecre
{

public:
    double i;
    void setData(double i = 0)
    {
        this->i = i;
    }

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

    // post deccrement operator overloading
    PostDecre operator--(int)//syntaxically Declaration 
    {
        PostDecre P;
        P.i = i--;
        return P;
    }
};

int main()
{
    PostDecre P;
    P.setData(2.2);
    P.display(); // 2;
    PostDecre P2 = P--;
    P2.display(); // 2
    P.display();  // 1
    return 0;
}

Output:

i= 2.2
i= 2.2
i= 1.2

Pre-Decrement Operator Overloading:

// pre decrement operator overloading

#include <iostream>

using namespace std;

class PreDecre
{
public:
    int i;
    void setData(int i = 0)
    {
        this->i = i;
    }

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

    // pre-decrement operator overloading
    PreDecre operator--()
    {
        PreDecre P;
        P.i = --i;
        return P;
    }
};

int main()
{
    PreDecre P;
    P.setData(2);
    P.display();       // 2
    PreDecre P2 = --P; // pre decrement operator overloading
    P2.display();      // 1
    P.display();       // 1
    return 0;
}

Output:

i= 2
i= 1
i= 1

Default image
Lingaraj Senapati
Hey There! I am Lingaraj Senapati, the Co-founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.
Articles: 253

Newsletter Updates

Enter your email address below to subscribe to our newsletter