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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 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 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 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;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// CircleClassUse.cpp

#include <iostream>
#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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 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 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 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();
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//CircleClassUse.cpp
#include <iostream>
#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.