C++ Program to find the greatest weight of two objects using operator overloading
/* 5. Write a program to find the greatest weight of two objects using operator
overloading*/
#include<iostream.h>
#include<conio.h>
class weight
{
private:
int a;
public:
weight()
{
a=0;
}
void input_weight()
{
cout<<endl<<“Enter weight:”<<endl;
cin>>a;
}
int operator>=(weight w)
{
if(a>w.a)
return 1;
else if(a==w.a)
return 0;
else
return -1;
}
};
void main()
{
clrscr();
int comp;
weight w1,w2;
w1.input_weight();
w2.input_weight();
comp=w1>=w2;
if(comp==1)
cout<<“W1 is greatest”<<endl;
else if(comp==0)
cout<<“Same weight”<<endl;
else if(comp==-1)
cout<<“W2 is greatest”<<endl;
getch();
}