Write a program to overload operators in the same program by writing suitable operator member functions for following set of expressions: O5= (O1/O2) *(O3+O4) [Here O1,O2,O3,O4 and O5 are objects of a class “overloading”, and this class is having one integer data member]
#include <iostream>
using namespace std;
class Object
{
int x;
public:
Object() {}
Object(int _x) :x(_x) {}
Object operator/ (Object& o)
{
Object tmp;
tmp.x = x/o.x;
return tmp;
}
Object operator+ (Object& o)
{
Object tmp;
tmp.x = x + o.x;
return tmp;
}
Object operator* (Object& o)
{
Object tmp;
tmp.x = x * o.x;
return tmp;
}
friend ostream& operator<< (ostream&, Object&);
};
ostream& operator<< (ostream& os, Object& o)
{
return os << "\nInfo about object: " << "x = " << o.x;
}
int main()
{
Object O1(9);
Object O2(3);
Object O3(2);
Object O4(5);
Object O5 = (O1 / O2) *(O3 + O4);
cout << O5;
}
Comments
Leave a comment