Write a Circle class that has the following member variables; radius: a double p
ID: 3714027 • Letter: W
Question
Write a Circle class that has the following member variables;
radius: a double
pi: a double with value 3.14159
The class should have the following member functions:
Default Constructor. A default constructor that sets radius to 0.0.
Constructor. Accepts the radius of the circle as an argument.
setRadius. A mutator function for the radius variable.
getRadius. An accessor function for the radius variable.
getArea. Returns the area of the circle, which is calculated as
area = pi * radius * radius
getDiameter. Returns the diameter of the circle, which is calculated as
diameter = radius * 2
getCircumference. Returns the circumference of the circle, which is calculated as
circumference = 2 * pi * radius
Write a program that demonstrates the Circle class by asking the user for the circles radius, creating the Circle object, and then reporting the circle area, diameter, and circumference.
Create and use separate specification and implementation files for the Circle class. Use the include guard (#ifndef/#define/#endif) within your class specification file to prevent multiple class definitions. • The member variables radius and pi should be declared private and all methods declared public. Only the accessor method for radius may be declared inline.
Explanation / Answer
-------------------------Header------------------------------
#ifndef CIRCLE_H
#define CIRCLE_H
using namespace std;
class Circle{
private:
double radius;
double pi;
public:
Circle();
Circle(double radius);
void setRadius(double radius);
double getRadius();
double getArea();
double getDiameter();
double getCircumference();
};
#endif
-------------------------Header Ends------------------------------
---------------DEfinition-------------------------------------
#include "circle.h"
#include <iostream>
#include <string>
using namespace std;
Circle::Circle(){
pi = 3.14159;
radius = 0.0;
}
Circle::Circle(double radius){
pi = 3.14159;
this->radius = radius;
}
void Circle::setRadius(double radius){
this->radius = radius;
}
double Circle::getRadius(){
return radius;
}
double Circle::getArea(){
return ( pi * radius * radius);
}
double Circle::getDiameter(){
return (radius * 2);
}
double Circle::getCircumference(){
return (2 * pi * radius);
}
---------------DEfinition Ends-------------------------------------
---------------Driver -------------------------------------
#include "circle.h"
#include <iostream>
#include <string>
using namespace std;
int main(){
Circle c;
double radius;
cout << "Please enter radius of circle1" << endl;
cin >> radius;
c.setRadius(radius);
cout << "Radius: " << c.getRadius() << " Diameter: " << c.getDiameter() << " Circumference: " << c.getCircumference() << " Area: " << c.getArea() << endl;
cout << "Please enter radius of circle2" << endl;
cin >> radius;
Circle c2(radius);
cout << "Radius: " << c2.getRadius() << " Diameter: " << c2.getDiameter() << " Circumference: " << c2.getCircumference() << " Area: " << c2.getArea() << endl;
return (0);
}
---------------Driver Ends-------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.