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