Friday, 31 August 2012

Operator overloading (unary) in C++

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

No comments:

Post a Comment

There was an error in this gadget