Tuesday, April 1, 2014

C++ Program to demonstrate operator overloading

C++ Program to demonstrate operator overloading function (Unary Operator) Using friend function
#include<iostream>
class A
{
int x,y;
public:
void input ()
{
cout<<"Enter X and Y values\n";
cin>>x>>y;
}
void disp()
{
cout<<"x is "<<x<<endl<<"y= "<<y<<endl;
}
friend void operator --(A &o);
friend void operator ++(A &o);
};
void operator --(A &o)
{
o.x=--o.x;
o.y=--o.y;
}
void operator ++(A &o)
{
o.x++;
o.x=o.x;
o.y++;
o.y=o.y;
}
int main()
{
A a;
a.input();
a.disp();
cout<<"After decrimenting\n";
//accessing unary minus operator
--a;//calling operator -()
a.disp();
++a;
cout<<"After incrimenting\n";
a.disp();
return 0;
}


No comments:

Post a Comment