Program to overload a single Unary Operator.
Code :
#include<iostream.h>
#include<conio.h>
class optr
{
public:
int a;
optr()
{
a=0;
}
optr(int x)
{
a=x;
}
void show()
{
cout<<"Addition : "<<a;
}
optr operator +(optr p)
{
optr temp;
temp.a=p.a+a;
return temp;
}
};
void main()
{
clrscr();
int x,y;
cout<<"Program will give addition of given two values by overloading unary '+' operator"<<endl;
cout<<"Enter Value of X : ";
cin>>x;
cout<<"Enter Value of Y : ";
cin>>y;
optr z1(x),z2(y),z3;
z3=z1+z2;
z3.show();
getch();
}
Output :
No comments:
Post a Comment