Q2: Write a program to overload operators in the same program by writing suitable operator member
functions for following expression:
O7= ((O1 % O2)>(O3 || O4) - (O5>O6)) [Here O1,O2,O3,O4,O5,O6 and O7 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= (int v)
{
Object tmp;
tmp.value = v;
return tmp;
}
int operator% (Object& obj)
{
int tmp;
tmp = value % obj.value;
return tmp;
}
int operator|| (Object& obj)
{
int tmp;
tmp = value || obj.value;
return tmp;
}
int operator> (Object& obj)
{
int tmp;
tmp = value > obj.value;
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(0);
Object O5(1);
Object O6(2);
Object O7 = ((O1 % O2)>(O3 || O4) - (O5>O6));
cout << O7 << endl;
}
Comments
Leave a comment