Write a program to overload unary (++) operator and unary (--) operator using friend function.
#include<iostream>
using namespace std;
class Unary
{
int x,y;
public:
Unary(int m,int n)
{
x=m;
y=n;
}
void show()
{
cout<<"x="<<x;
cout<<"\ny="<<y;
}
friend void operator --(Unary &);
friend void operator ++(Unary &);
};
void operator --(Unary &a)
{
a.x=--a.x;
a.y=--a.y;
}
void operator ++(Unary &a)
{
a.x=++a.x;
a.y=++a.y;
}
int main()
{
Unary a(4,6);
Unary b(4,6);
--a;
++b;
cout<<"Displaying -- unary operator\n";
a.show();
cout<<"\nDisplaying ++ unary operator\n";
b.show();
return 0;
}
Comments
Leave a comment