//to check whether one object is less than or not
//by friend function
//class Distance with m and cm as members
#include <iostream>
using namespace std;
class Distance
{
int m,cm;
public:
void read()
{
cout<<"Enter m and cm: ";
cin>>m>>cm;
}
// void display()
// {
// cout<<m<<"m "<<cm<<"cm"<<endl;
// }
friend bool operator<(Distance d1,Distance d2) //friend function
{
float m1=d1.m+(d1.cm/100);
float m2=d2.m+(d2.cm/100);
return ((m1<m2)?true:false);
}
};
int main()
{
Distance d1,d2;
d1.read();
d2.read();
if(d1<d2) //same as operator<(d1,d2);
cout<<"the d1 is less than d2.";
else
cout<<"the d1 is not less than d2.";
return 0;
}
//by friend function
//class Distance with m and cm as members
#include <iostream>
using namespace std;
class Distance
{
int m,cm;
public:
void read()
{
cout<<"Enter m and cm: ";
cin>>m>>cm;
}
// void display()
// {
// cout<<m<<"m "<<cm<<"cm"<<endl;
// }
friend bool operator<(Distance d1,Distance d2) //friend function
{
float m1=d1.m+(d1.cm/100);
float m2=d2.m+(d2.cm/100);
return ((m1<m2)?true:false);
}
};
int main()
{
Distance d1,d2;
d1.read();
d2.read();
if(d1<d2) //same as operator<(d1,d2);
cout<<"the d1 is less than d2.";
else
cout<<"the d1 is not less than d2.";
return 0;
}
No comments:
Post a Comment