unary operator overloding
#include<bits/stdc++.h>
using namespace std;
class item
{
int x;
public :
void setval()
{
x=0;
}
void show()
{
cout<<x<<endl;
}
item operator ++()
{ item r;
r.x= ++x;
return r;
}
item operator ++(int)
{
x++;
}
item operator --()
{
--x;
}
item operator --(int)
{
x--;
}
};
int main()
{
item a;
a.setval();
a.show();
++a;
a.show();
a++;
a.show();
--a;
a.show();
a--;
a.show();
item b;
b=++a;
b.show();
return 0;
}
0 Comments