Define a class named circle. The class should have one private member, radius (d
ID: 3676583 • Letter: D
Question
Define a class named circle. The class should have one private member, radius (double). The class should have a constructor that accepts one argument. The constructor argument shoud be assigned to the member radius.
There should also be a setter (mutator) and getter (accessor) defined for the private member.
Finally, define three functions as public members of the class as well:
- getArea() that calculates the circle's area
- getCircumference() that calculates the circle's circumference
- getDiameter() that calculates the diameter of the circle.
Explanation / Answer
<HEADER FILE> CIRCLE.H
#include <cmath>
using namespace std;
class Circle{
public:
Circle(int x, int y, int r);
int getX();
void setX(int x);
int getY();
void setY(int y);
int getRadius();
void setRadius(int r);
void setCenter(int x, int y);
double area();
double circumference();
private:
int _x;
int _y;
int _r;
static const double PI = 3.14;
};
Circle::Circle(int x, int y, int r){
_x = x;
_y = y;
_r = r;
}
int Circle::getX(){
return _x;
}
void Circle::setX(int x){
_x = x;
}
int Circle::getY(){
return _y;
}
void Circle::setY(int y){
_y = y;
}
int Circle::getRadius(){
return _r;
}
void Circle::setRadius(int r){
_r = r;
}
void Circle::setCenter(int x, int y){
_x = x;
_y = y;
}
double Circle::area(){
return PI*double(pow(_r, 2));
}
double Circle::circumference(){
return double(2 * PI * _r);
}
double Circle::diameter(){
return double(2 * _r);
}
CODE
#include "circle.h"
#include <iostream>
using namespace std;
int main(){
Circle c(0, 0, 5);
cout << "Center: (" << c.getX() << ", " << c.getY() << ")" << endl;
cout << "Radius: " << c.getRadius() << endl;
cout << "Area: " << c.area() << endl;
cout << "Circumference: " << c.circumference() << endl << endl;
cout << " diameter: " << c. diameter () << endl << endl;
c.setX(2);
c.setY(2);
c.setRadius(10);
cout << "Center: (" << c.getX() << ", " << c.getY() << ")" << endl;
cout << "Radius: " << c.getRadius() << endl;
cout << "Area: " << c.area() << endl;
cout << "Circumference: " << c.circumference() << endl << endl;
cout << " diameter: " << c. diameter () << endl << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.