In: Computer Science
in c++ a Circle should be created with the default constructor, then its area, diameter, and circumference printed to the screen for verification that it has the correct state and those respective methods work correctly. Then, output of the state should be repeated after calling each mutator to ensure that the object’s new state reflects the change
Create a Circle class with the class implementation in a file named circle.cpp and the class declaration in a file named circle.h (or circle.hpp) with
- private data member for double parameter: ‘radius’
- a default constructor which has no parameters
- an overloaded constructor which takes in a single double parameter for ‘radius’
-accessor and mutator for ‘radius’ named getRadius(…) and setRadius(…)
- member functions to return doubles for calculated parameters: area( ), diameter( ) and circumference( ) PI should be assumed to be exactly 3.141592. You may define this as a static const double at the class scope, as a const double inside any method that uses it, or as a literal in the expression in which it’s used
Circle.hpp
#include <iostream>
using namespace std;
class Circle {
private:
double radius;
static const double PI;
public:
Circle();
Circle(double);
double getRadius();
void setRadius(double);
double area();
double diameter();
double circumference();
};
Circle.cpp
#include <iostream>
#include "Circle.hpp"
using namespace std;
const double Circle::PI = 3.141592;
Circle::Circle(){
radius = 0;
}
Circle::Circle(double radius) {
this->radius = radius;
}
double Circle::getRadius() {
return radius;
}
void Circle::setRadius(double radius) {
this->radius = radius;
}
double Circle::area() {
return PI * radius * radius;
}
double Circle::diameter() {
return 2 * radius;
}
double Circle::circumference() {
return 2 * PI * radius;
}
int main() {
Circle c(14);
cout << "Diameter = " << c.diameter() << endl;
cout << "Circumference = " << c.circumference() << endl;
cout << "Area = " << c.area() << endl;
}