### Implementing relational operators to order objects

All,

I got stuck with a homework assignment. I have a Circle class and 2 Circle objects (one of them has its values changed, but that doesn't really matter). I need to use relational operators to order by radius their appearance on screen.

 ``12345678910111213141516171819`` ``````// Circle.h #ifndef CIRCLE_H #define CIRCLE_H class Circle { public: Circle(); Circle(double); double getArea(); double getRadius(); void setRadius(double); private: double radius; }; #endif ``````

 ``123456789101112131415161718192021222324252627282930313233`` ``````// Circle.cpp #include "Circle.h" // Construct a default circle object Circle::Circle() { radius = 1; } // Construct a circle object Circle::Circle(double newRadius) { radius = newRadius; } // Return the area of this circle double Circle::getArea() { return radius * radius * 3.14159; } // Return the radius of this circle double Circle::getRadius() { return radius; } // Set a new radius void Circle::setRadius(double newRadius) { radius = (newRadius >= 0) ? newRadius : 0; }``````

 ``1234567891011121314151617181920212223`` ``````// CircleClassUse.cpp #include #include "Circle.h" using namespace std; int main() { Circle circle1; Circle circle2(5.0); cout << "The area of the circle of radius " << circle1.getRadius() << " is " << circle1.getArea() << endl; cout << "The area of the circle of radius " << circle2.getRadius() << " is " << circle2.getArea() << endl; //Modify circle radius circle2.setRadius(100); cout << "The area of the circle of radius " << circle2.getRadius() << " is " << circle2.getArea() << endl; return 0; }``````

Can anybody help me? Greatly appreciated.
 ``1234567891011121314151617181920`` ``````class Circle { public: //[...] bool operator<(Circle& that); private: //[...] }; bool Circle::operator<(Circle& that) { return getRadius() < that.getRadius(); } //[...] if (circle1 < circle2) cout << circle1.getArea() << ' ' << circle2.getArea(); else cout << circle2.getArea() << ' ' << circle1.getArea(); //[...] ``````
Thanks! Here is my final code:

 ``12345678910111213141516171819`` ``````// Circle.h #ifndef CIRCLE_H #define CIRCLE_H class Circle { public: Circle(); Circle(double); double getArea(); double getRadius(); void setRadius(double); bool operator<(Circle& that); private: double radius; }; #endif ``````

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````// Circle.cpp #include "Circle.h" // Construct a default circle object Circle::Circle() { radius = 1; } // Construct a circle object Circle::Circle(double newRadius) { radius = newRadius; } // Return the area of this circle double Circle::getArea() { return radius * radius * 3.14159; } // Return the radius of this circle double Circle::getRadius() { return radius; } // Set a new radius void Circle::setRadius(double newRadius) { radius = (newRadius >= 0) ? newRadius : 0; } bool Circle::operator<(Circle& that) { return getRadius() < that.getRadius(); } ``````

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````//CircleClassUse.cpp #include #include "Circle.h" using namespace std; int main() { Circle circle1; Circle circle2(5.0); if (circle1 < circle2) { cout << "The area of the circle of radius " << circle1.getRadius() << " is " << circle1.getArea() << endl; cout << "The area of the circle of radius " << circle2.getRadius() << " is " << circle2.getArea() << endl << endl; } else { cout << "The area of the circle of radius " << circle2.getRadius() << " is " << circle2.getArea() << endl; cout << "The area of the circle of radius " << circle1.getRadius() << " is " << circle1.getArea() << endl << endl; } circle2.setRadius(100); cout << "After changing circle2's radio to 100, we get: " << endl << endl; if (circle1 < circle2) { cout << "The area of the circle of radius " << circle1.getRadius() << " is " << circle1.getArea() << endl; cout << "The area of the circle of radius " << circle2.getRadius() << " is " << circle2.getArea() << endl; } else { cout << "The area of the circle of radius " << circle2.getRadius() << " is " << circle2.getArea() << endl; cout << "The area of the circle of radius " << circle1.getRadius() << " is " << circle1.getArea() << endl; } return 0; }``````
Topic archived. No new replies allowed.