Answer to Question #318629 in C++ for Bharat shahi

Question #318629

Creat a class called class1 and class2 which each of having one private data members .Add member function to set value say 'set value' on each class. Add one more function max() that is friendly to both classes and max() function should compare two private member of two classes and show maximum among them


1
Expert's answer
2022-03-26T12:41:00-0400
#include <iostream>

using namespace std;

class class2;
class class1
{
	int data;
public:
	void SetValue(int value)
	{
		cout << "Set value " << value << " for class1"<<endl;
		data = value;
	}
	friend int MAX(class1 a, class2 b);
};


class class2
{
	int data;
public:
	void SetValue(int value)
	{
		cout << "Set value " << value<<" for class2"<<endl;
		data = value;
	}
	friend int MAX(class1 a, class2 b);
};


int MAX(class1 a, class2 b)
{
	if (a.data >= b.data)
		return a.data;
	else
		return b.data;
}

int main()
{
	class1 x;
	class2 y;
	x.SetValue(8);
	y.SetValue(7);
	cout << "Max is " << MAX(x, y)<<endl;
	x.SetValue(3);
	y.SetValue(9);
	cout << "Max is " << MAX(x, y)<<endl;
}

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