Reference no: EM13163959
The Table Q3 on the next page is the code of a class named Circle. Study the code and implement the operator overloading for these relational operators (<, <=, ==, !=, >, >=) for the Circle class. Then, write a test program that creates two instances of the Circle class and compare these two instances using the operators (<, <=, ==, !=, >, >=) overloaded for the Circle class.
More specifically, you should develop the following files as the answer for this question.
(1) Revising the Circle class by adding a set of functions implementing operator overloading. That is, the updated class should include:
A revised header file
It should have the definitions of the following new functions:
- For each relational operator listed above, the prototype of the corresponding operator function.
- Any new additional variables and/or the prototypes of helper functions.
The definition of each function prototype and variable listed in Table Q3 must be included in this revised header file as well.
For each operator function you added to the header file, specifying its interface, including:
- Begins with a short description of the module.
- Purpose - the purpose of the function
- Parameters(s) - the parameters of the function
- Precondition(s) - statements about the parameters or the internal state of the module that are assumed to be true (must be true for the function to work correctly)
- Return - the return value of the function
- Side Effect - any effect produced by this function that is not described by the return vale
For each new variable in the header file, an inline comment must be provided to explain the purpose of the variable prior to its definition.
A revised C++ program
It should contain the implementation of each new function defined in the header file. The definition of each function listed in Table Q3 must be included in this revised C++ program as well.
(2) A C++ test program
A C++ test program that is used to test the performance of the C++ class you have developed. Its working procedure can be:
- Create two instances of the Circle class.
- Compare two instances using each operator function defined in your class
- Once a comparison is finished, show the result to the user. For example, telling the user if two instances are equal.
Each function in your test program, specifying its interface, including:
- Begins with a short description of the module.
- Purpose - the purpose of the function
- Parameters(s) - the parameters of the function
- Precondition(s) - statements about the parameters or the internal state of the module that are assumed to be true (must be true for the function to work correctly)
- Return - the return value of the function
- Side Effect - any effect produced by this function that is not described by the return vale
For each variable in your test program, an inline comment must be provided to explain the purpose of the variable prior to its definition. 4
Circle Class
//CircleWithConstantMemberFunctions.h
#ifndef CIRCLE_H
#define CIRCLE_H
class Circle
{
public:
Circle();
Circle(double);
double getArea() const;
double getRadius() const;
void setRadius(double);
static int getNumberOfObjects();
private:
double radius;
static int numberOfObjects;
};
#endif
//CircleWithConstantMemberFunctions.cpp
#include "CircleWithConstantMemberFunctions.h"
int Circle::numberOfObjects = 0;
// Construct a circle object
Circle::Circle()
{
radius = 1;
numberOfObjects++;
}
// Construct a circle object
Circle::Circle(double newRadius)
{
radius = newRadius;
numberOfObjects++;
}
// Return the area of this circle
double Circle::getArea() const
{
return radius * radius * 3.14159;
}
// Return the radius of this circle
double Circle::getRadius() const
{
return radius;
}
// Set a new radius
void Circle::setRadius(double newRadius)
{
radius = (newRadius >= 0) ? newRadius : 0;
}
// Return the number of circle objects
int Circle::getNumberOfObjects()
{
return numberOfObjects;
}