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 »

Still not found what you are looking for? Try again here.