Operator Overloading for Unary Operator (++)

This is the solution that decreases an integer value by 1 by overloaded operator (T.U. 2073)

#include <iostream>

using namespace std;

class Increment
{
    int a;
public:
    Increment()
    {
        a=0;
    }
    Increment(int x)
    {
        a=x;
    }
    operator++(int)
    {
        a++;
    }
    operator++()
    {
        ++a;
    }
    void print();
};

void Increment::print()
{
    cout << endl << "a=" << a << endl ;
}

int main()
{
    Increment I1(4);
    I1.print();
    I1++;
    I1.print();
    Increment I2(100);
    I2.print();
    ++I2;
    I2.print();
    return 0;
}

No comments:

Post a Comment