I am new to C++ and currently learning operator overloading of postfix operators, Here in the below program if i use one argument i am getting the result fine but if i go with two arguments the program is displaying incorrect output. I request the people to solve my issue and clear my doubt.
#include<iostream>
using namespace std;
class num
{
int a;
public:
num(int _a = 0)
{
a = _a;
}
num operator ++ ()
{
return a++;
}
num operator ++ (int)
{
return (a++);
}
void show()
{
cout << a << endl;
}
};
/*
class num
{
int a, b;
public:
num(int _a = 0, int _b = 0)
{
a = _a;
b = _b;
}
num operator ++ ()
{
a++;
b++;
return *this;
}
num operator ++ (int)
{
a++;
b++;
return *this;
}
void show()
{
cout << a << b << endl;
}
};
*/
int main()
{
num ob(10);
num z,y;
ob.show();
z = ob++;
z.show();
y = ++ob;
y.show();
getchar();
return 0;
}
The commented code i used to increment two numbers using postfix operator. There is some problem with that code i am getting incorrect results.