Answer to Question #332767 in C++ for Tkh^2

Question #332767

Write a program to overload the expression (ob1*ob2) %ob3. Where ob1, ob2 and ob3 are the objects of a class. Assume 2 number of integer data members in the class.


1
Expert's answer
2022-04-23T09:04:12-0400
#include <iostream>

using namespace std;

class Object
{
	int x;
	int y;
public:
	Object(){}
	Object(int _x,int _y):x(_x),y(_y){}
	Object operator* (Object& o)
	{
		Object tmp;
		tmp.x = x*o.x;
		tmp.y = y*o.y;
		return tmp;
	}
	Object operator% (Object& o)
	{
		Object tmp;
		tmp.x = x%o.x;
		tmp.y = y%o.y;
		return tmp;
	}
	friend ostream& operator<< (ostream&, Object&);
};

ostream& operator<< (ostream& os, Object& o)
{
	return os<<"\nInfo about object: " << "x = " << o.x << " y = " << o.y;
}

int main()
{
	Object ob1(5, 9);
	Object ob2(2, 4);
	Object ob3(3, 8);
	Object ob4 = (ob1*ob2) % ob3;
	cout << ob4;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS