In: Computer Science
Note- can you rewrite the code in C++.
Circle Class c++ code 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 to 0.0.
• Constructor. Accepts the radius of the circle as an argument
. • setRadius. A mutator function for the radius variable.
• getRadius. An acccssor 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
• getcircumforence. 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 circle's radius, creating a circle object, and then reporting the circle's area, diameter, and circumference.
Explanation:
Here is the code with a Circle class with appropriate attributes, constructors, and other functions to calculate area, diameter, circumference, etc.
also, in the main function , user is asked for the radius and the circle object is created and the details are printed.
Code:
#include <iostream>
using namespace std;
class Circle
{
double radius;
double pi = 3.14159;
public:
Circle()
{
radius = 0.0;
}
Circle(double radius)
{
this->radius = radius;
}
void setRadius(double r)
{
radius = r;
}
double getRadius()
{
return radius;
}
double getArea()
{
double area = pi*radius*radius;
return area;
}
double getDiameter()
{
double diameter = radius*2;
return diameter;
}
double getCircumference()
{
double circumference = 2*pi*radius;
return circumference;
}
};
int main()
{
double radius;
cout<<"Enter radius: ";
cin>>radius;
Circle c(radius);
cout<<"Area: "<<c.getArea()<<endl;
cout<<"Diameter: "<<c.getDiameter()<<endl;
cout<<"Circumference:
"<<c.getCircumference()<<endl;
return 0;
}
Output:
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!