#include <iostream.h>
#include<conio.h>
/*----------------------Class definition-------------------------*/
class sample
{
int a;
int b;
public :
sample() { } // default empty constructor
sample(int, int);
void operator - (); // Overloaded unary minus
void print();
};
sample::sample(int x, int y)
{
a = x;
b = y;
}
void sample::operator - ()
{
a = -a;
b = -b;
}
void sample::print()
{
cout << "a = " << a << "\n";
cout << "b = " << b << "\n";
}
/*----------------------Definition ends here---------------------*/
void main(void)
{
sample A(10,20);
clrscr();
cout << "Before the application of operator '-'(unary minus) :\n";
A.print();
-A; // operator function gets invoked
cout << "After the application of operator '-'(unary minus) :\n";
A.print();
A.operator -(); // another way of invoking - as a member function
cout << "After the second application of operator '-'(unary minus) :\n";
A.print();
getch();
}
0 comments:
Post a Comment