Write a circle class that has the following member variables: radius: a double p
ID: 638999 • Letter: W
Question
Write a circle class that has the following member variables:
radius: a double
pi: a double initialized with the value 3.14159
The class should have the following member functions:
Default Constructor: A default constructor that sets radius to0.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 calculatedas
area = pi * radius * radius
getDiameter: Returns the diameter of the circle, which iscalculated as
diameter = radius * 2
getCircumfrence: Returns the circumference of the circle, whichis calculated as
circumference = 2 * pi * radius
write a program that demonstrates the Circle class by asking theuser for the circle
Explanation / Answer
#include <iostream>
using namespace std;
class circle
{private:
double radius;
const static double pi=3.14159;
public:
circle()
{radius=0.0;
}
circle(double r)
{setRadius(r);
}
void setRadius(double r)
{radius=r;
}
double getRadius()
{return radius;
}
double getArea()
{return pi*radius*radius;
}
double getDiameter()
{return radius*2;
}
double getCircumference()
{ return 2.*pi*radius;
}
};
int main()
{double r;
cout<<"Enter radius: ";
cin>>r;
circle mycircle(r);
cout<<"A circle with radius "<<mycircle.getRadius()<<" has an"<<endl;
cout<<"area of: "<<mycircle.getArea()<<endl;
cout<<"diameter of: "<<mycircle.getDiameter()<<endl;
cout<<"circumference of: " << mycircle.getCircumference()<<endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.