Overloading the unary minus operator by using member function
overloading the unary minus
operator is as shown below :
<header files>
class A
{
int x;
public:
A(int=0);
A operator –();
};
A A :: operator –()
{
return
A(-x);
}
/*end of A .cpp*/
void main()
{
int y;
A a1,
a2;
cout<<”Enter
the value of y”;
cin>>y;
a2.x=y;
a1=
-a2;
cout<<a1.x
}
No comments:
Post a Comment