C++ Write a Circle class that has the following member variables: radius: a doub
ID: 3675335 • Letter: C
Question
C++
Write a Circle class that has the following member variables:
radius: a double pi: a double initialized with the value of 3.14159
The class shall have the following member functions:
Default Constructor: a default constructor which 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 with driver, implementation, and specification files which demonstrates the Circle class by asking the user for the circle's radius, creating a Circle object, and then reporting the circle's area, diameter, and circumference.
The specification file will contain declarations for the member variables and prototypes for the member functions. The implementation file will contain the fully defined member functions and constructors.
Explanation / Answer
// Circle.h
class Circle{
private:
static double pi;
double radius;
public:
Circle();
Circle(double r);
void setRadius(double r);
double getRadius();
double getArea();
double getDiameter();
double getCircumference();
};
//Driver.cpp
#include<iostream>
#include "Circle.h"
using namespace std;
//default constructor
Circle::Circle(){
radius = 0.0;
}
//parameterized constructor
Circle::Circle(double r){
radius = r;
}
// function to set radius
void Circle:: setRadius(double r){
radius = r;
}
//function to get radius
double Circle:: getRadius(){
return radius;
}
// function to get diameter
double Circle:: getDiameter(){
return 2*radius;
}
//function to get circumference
double Circle:: getCircumference(){
return 2*pi*radius;
}
//function to get area
double Circle:: getArea(){
return pi*radius*radius;
}
// initializing stattic member pi with value
double Circle:: pi= 3.14159;
int main(){
//ceating circle using default constructor
Circle c1;
c1.setRadius(4.5);
cout<<"Diameter: "<<c1.getDiameter()<<endl;
cout<<"Circumference: "<<c1.getCircumference()<<endl;
cout<<"Area: "<<c1.getArea()<<endl;
cout<<endl;
//creating circle using parameterized constructor
Circle c2(9.0);
cout<<"Diameter: "<<c2.getDiameter()<<endl;
cout<<"Circumference: "<<c2.getCircumference()<<endl;
cout<<"Area: "<<c2.getArea()<<endl;
return 0;
}
/*
output:
Diameter: 9
Circumference: 28.2743
Area: 63.6172
Diameter: 18
Circumference: 56.5486
Area: 254.469
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.