Operator Overloading (Unary operator)

#include <iostream.h>
#include <conio.h>
class Point
{
private :
int x;
int y;
public :
Point( )
{
x=y=0;
}
Point(int a, int b)
{
x = a;
y = b;
}
void show( )
{
cout<<”x =”<<x<<”y=”<<y<<”\n”;
}
void operator ++ ( )
{
x++;
y++;
}
void operator -- ( )
{
x--;
y--;
}
};
void main( )
{
Point p1(12,15);
p1.show( );
p1++;
p1++;
p1.show( );
p1--;
p1.show( );
}
Output :
x = 12 y = 15
x = 13 y = 16
x = 14 y = 17
x = 13 y = 16

0 comments: