* Overloading unary operators:-
A minus operator when used as a unary, takes just one operand . we knows that this operator changes the sign of an operand when applied to a basic item.
we can overload this unary minus operator so that it can be applied to an object in the same way as is applied to an int or float variable.
// C++ program to overload unary minus operator.
#include<iostream>
using namespace std;
class space
{
int x,y,z;
public:
void getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void display()
{
cout<<x<<" "<<y<<" "<<z<<"\n";
}
void operator-()
{
x=-x;
y=-y;
z=-z;
}
};
int main()
{
space s;
s.getdata(10,-23,40);
s.display();
-s; // Operator - function call
s.display();
return 0;
}
using namespace std;
class space
{
int x,y,z;
public:
void getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void display()
{
cout<<x<<" "<<y<<" "<<z<<"\n";
}
void operator-()
{
x=-x;
y=-y;
z=-z;
}
};
int main()
{
space s;
s.getdata(10,-23,40);
s.display();
-s; // Operator - function call
s.display();
return 0;
}
OUTPUT:
10 -23 40
-10 23 -40
10 -23 40
-10 23 -40