OPERATOR OVERLOADING INCLUDING UNARY OPERATOR

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class sample   
{
  int x,y,z;
  public:
    void getdata(int a,int b,int c);
    void display();
    void operator-();
};
void sample::getdata(int a,int b,int c)
{
  x=a; y=b; z=c;
}
void sample::display()
{
  cout<<"\n"<<"X="<<x<<"\tY="<<y<<"\tZ="<<z<<"\n";
}
void sample::operator-()
{
  x=-x; y=-y; z=-z;
}
void main()
{
  sample s;
  clrscr();
  s.getdata(10,-20,30);
  s.display();
  -s;
  s.display();
  getch();
}

OUTPUT:

Input    X=10    Y=-20   Z=30

Output    X=-10   Y=20    Z=-30
Previous
Next Post »

If you still didn't find what you're looking for, you can search this website below: