Write a program to overload operators in the same program by writing suitable operator friend functions for following expression:
O6=((O1+O2) -(O3*O4) * (++O5)) [Here O1,O2,O3,O4,O5 and O6 are objects of a class “overloading”, and this class is having one integer data member]
#include<iostream>
using namespace std;
class Object
{
int value;
public:
Object(int _value = 0) :value(_value) {}
Object operator+(Object& obj)
{
Object tmp;
tmp.value = value + obj.value;
return tmp;
}
Object operator++ ()
{
Object tmp;
tmp.value = value + 1;
return tmp;
}
Object operator-(Object& obj)
{
Object tmp;
tmp.value = value - obj.value;
return tmp;
}
Object operator* (Object& obj)
{
Object tmp;
tmp.value = value * obj.value;
return tmp;
}
Object operator= (int v)
{
Object tmp;
tmp.value = v;
return tmp;
}
friend ostream& operator<< (ostream&, Object&);
};
ostream& operator<< (ostream& os, Object& obj)
{
return os << obj.value;
}
int main()
{
Object O1(5);
Object O2(3);
Object O3(2);
Object O4(1);
Object O5(1);
Object O6 = ((O1 + O2) - (O3*O4) * (++O5));
cout << O6 << endl;
}
Comments
Leave a comment