A program to find the greatest among the two numbers entered by using overloading operator > in C++

#include<iostream>
#include<conio.h>
using namespace std;
class Demo
{
int x;
public:
Demo()
{
x=0;
}
Demo(int y)
{
x=y;
}
int operator >(Demo d)
{
if(x>d.x)
{
return true;
}
else
{
return false;
}
}
void display()
{
cout<<x;
}
};
main()
{
Demo d1(15);
Demo d2(7);
if(d1>d2)
{
cout<<“Greater number is:”;
d1.display();
}
else
{
cout<<“Greater number is:”;
d2.display();
}
getch();
}

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *