Answer to Question #193859 in C++ for Gautham

Question #193859

To evaluate the following equation A=B%3, where A and B are two objects of the same class. Develop a C++ program to implement this using operator overloading with friend function.


1
Expert's answer
2021-05-15T13:52:15-0400


#include<iostream>


using namespace std;


class Class
{


	public:
		int a;
		static int count;
		
		Class()
		{
			++count;
		}
		void set_a(int a)
		{
			this->a =a;
		}


		void get_a()
		{
			cout<<"Value of object"<<Class::count<<" is : "<< a <<endl;
		}
		
		friend float operator %(const Class& ob1, const Class& ob2);	
		
};


int Class :: count =0;


float operator%(const Class& ob1, const Class& ob2)
{
	return (int)ob1.a % (int)ob2.a;
}




int main()
{
	Class A;
	cout<<"Enter value for object"<<Class::count<<" : ";
	int val;
	cin>>val;
	A.set_a(val);
	
	cout<<"Enter value for object"<<Class::count<<" : ";
	int val1;
	cin>>val1;
	cout<<endl;
	A.get_a();
	Class B;
	B.set_a(val1);
	int ans = operator %(A,B);


	B.get_a();
	cout<<endl<<"The value after operator overloading friend function % for object1 and object2 is : ";
	cout<<ans;
}




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