I was just reading about unary operator overloading using minus(-),increment(++).etc. I thought to write a code for the same just for practice.But when I tried to run it it give me error for both minus and increment operator.I think the problem is the way I'm calling the operator in main.Can anyone please what's the correct way of doing this ?
#include<iostream>
using namespace std;
class c{
int x;
int y;
public:
c(int a,int b){
x=a;
y=b;
}
void operator -(){
x=x+1;
y=y+1;
}
void display(){
cout<<x<<" "<<y<<"\n";
}
};
int main()
{
c obj(2,3);
obj.display();
obj- ; //I think the error is on this line
obj.display();
return 0;
}
If I replace obj- with -obj the code works fine.Why is it so ? Same is the problem with ++ operator overloading(using ++obj works fine but obj++ doesn't work),why ?
Thanks.