Is there any difference between overloading unary and binary operator.
I have doubt with following codes:
overloading binary operator:
#include<iostream>
using namespace std;
class ex
{
int i;
int j;
public:
ex operator+(ex ob)
{
ex temp;
temp.i=this->i+ob.i;
temp.j=this->j+ob.j;
return temp;
}
ex& operator=(ex ob)
{
this->i=ob.i;
this->j=ob.j;
return *this;
}
void setij(int x,int y)
{
this->i=x;
this->j=y;
}
int geti()
{
return i;
}
int getj()
{
return j;
}
};
int main()
{
ex ob1,ob2,ob;
ob1.setij(1,1);
ob2.setij(1,1);
ob=ob1+ob2; //ob1 is passed through this pointer
cout<<"i="<<ob.geti()<<"\nj="<<ob.getj();
}
overloading unary operator:
#include<iostream>
using namespace std;
class ex
{
int i,j;
public:
ex(){}
ex &operator++();
void setij(int x,int y);
int geti();
int getj();
};
ex &ex::operator++()
{
this->i++;
this->j++;
return *this;
}
void ex::setij(int x,int y)
{
i=x;
j=y;
}
int ex::geti()
{
return i;
}
int ex::getj()
{
return j;
}
int main()
{
ex ob;
ob.setij(1,1);
cout<<"i="<<ob.geti()<<"\nj="<<ob.getj();
++ob; //ob is passed through this pointer
cout<<"\nAfter ++\n";
cout<<"i="<<ob.geti()<<"\nj="<<ob.getj();
}
MY question is:
1)in binary operator the this pointer passed to function is left of the operator.
ob1+ob2; //ob1 is passed through this pointer
2)in unary operator the this pointer to function is right of the operator.
++ob; //ob is passed through this pointer
3)Why int is put in postfix increment operator like ex &ex::operator++(int)
Why the this pointer passed to function vary in left and right?
output for first code :
i=2
j=2
Process returned 0 (0x0) execution time : 0.417 s
Press any key to continue.
output for second code :
i=1
j=1
After ++
i=2
j=2
Process returned 0 (0x0) execution time : 0.424 s
Press any key to continue.