May 4, 2013 at 2:04am UTC
1 2 3 4 5 6 7 8 9 10 11
class myFloat
{
float x;
float y;
public :
// ...
bool operator < ( const myFloat& that ) const ;
bool operator > ( const myFloat& that ) const ;
// ...
};
1 2 3 4 5 6 7 8 9 10 11
bool myFloat::operator < ( const myFloat& that ) const
{
if ( this ->x < that.x ) return true ;
else if ( that.x < this ->x ) return false ;
else return this ->y < that.y ;
}
bool myFloat::operator > ( const myFloat& that ) const
{
return that < *this ;
}
Last edited on May 4, 2013 at 2:07am UTC
May 11, 2013 at 2:45am UTC
That worked great!
I am having some trouble with using the overloaded operator<() to sort five objects based on the variable x that all objects have.
#include "FinalFloat.h"
int main ()
{
myFloat obj1;
myFloat obj2(2.2,5.1);
myFloat obj3(4.2, 11.1);
myFloat obj4(13.45, 1.01);
myFloat obj5(3.32, 8.9);
obj1.setX(5.5);
obj1.setY(6.4);
cout << "obj1 is " << obj1;
cout << "obj2 is " << obj2;
cout << "obj3 is " << obj3;
cout << "obj4 is " << obj4;
cout << "obj5 is " << obj5;
if(obj1<obj2) cout << obj1 << obj2 <<endl;
else cout << obj2 << obj1 << endl;
if(obj3<obj2) cout << obj3 << endl;
return 0;
}
I think a for loop would be easier but I am not sure how to use one in this instance.
Last edited on May 11, 2013 at 2:46am UTC