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.
// 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;
}
// CircleClassUse.cpp
#include <iostream>
#include "Circle.h"
usingnamespace 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;
}
//CircleClassUse.cpp
#include <iostream>
#include "Circle.h"
usingnamespace 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;
}