Overloading operator > (greater than) by using member function:
If
d1,d2 are objects of class ‘Distance’ then
//header files
class Distance
{
Private:
int feet;
float inches;
public:
Distance(int i,
float f)
{
feet=i;
inches=f;
}
bool Distance::operator >(Distance); //prototype
for overloading the > operator
};
bool
Distance :: operator > (Distance d2)
{
if(feet > d2.feet)
return true;
else
return false;
}
void
main()
{
Distance d1(4,5), d2(6,7);
if d1> d2;
// > operator is overloaded
cout<<”d1 is greater than d2”;
if d2< d1;
cout<<”d2 is greater than d1”;
}
Over loading relational operators
No comments:
Post a Comment