Answer to Question #225539 in C++ for Janvi Shah

Question #225539

Write a C++ Program to take two value from the user and create a menu driven code which will perform following operations using operator overloading.

1. Overload to compare two operands and check which operands have greater value and display it.

2. Overload any arithmetic operator and display its result.



1
Expert's answer
2021-08-12T16:24:36-0400
#include<iostream>
#include<bits/stdc++.h>
using namespace std;


class Class{
	public:
		static int count;
		int a;
		
		void input()
		{
			count++;
			cout<<"Enter value of object"<<count<<" : ";
			cin>>a;
		}
		
		int operator > (Class ob)
		{
			if(ob.a < a)
			{
				return 1;
			}
			else{
				return 0;
			}
		}
};


int Class :: count = 0;


int main()
{
	Class ob1;
	Class ob2;
	
	ob1.input();
	cout<<endl;
	ob2.input();
	cout<<endl;
	
	if(ob1 > ob2)
	{
		cout<<"Value of object1 is greater"<<endl;
	}
	else{
		cout<<"Value of object2 is greater"<<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