Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is C++. Write a Circle class that has the following member variables: radiu

ID: 3695558 • Letter: T

Question

This is C++.

Write a Circle class that has the following member variables:

radius: a double

PI: a double initialized with the value 3.14159. You can define this either as a global variable or a public constant.

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. This function should accept nonnegative values for radius. Display an error message if specified value is negative. This function should not be defined inline.  

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

Make appropriate use of private and public members. Accessor functions should be defined as const.

Write a program that demonstrates the Circle class by:

creating a circle object using the default constructor

reporting the circle’s radius, diameter, area, and circumference.

asking the user for a value for the circle’s radius. Then, using appropriate member functions to set the radius to this value.

reporting the circle’s radius, diameter, area, and circumference.

creating a second circle object with radius initialized to 12.5

reporting the circle’s radius, diameter, area, and circumference.

A sample program run is as follows, where user input is shown in red color:

Explanation / Answer

#include<iostream>
using namespace std;
double pi=3.14159;
class circle
{
private:
double radius;
public:
circle()
{
radius=0;
}
circle(double r)
{
radius=r;
}
void setRadius(double r)
{
radius=r;
}
double getRadius()
{
return radius;
}
double getArea()
{
return pi*radius*radius;
}
double getDiameter()
{
return 2*radius;
}
double getCircumference()
{
return 2*pi*radius;
}
};
int main()
{
circle c1;
double r;
cout<<"Here is the first circle's data after using the default constructor for creating the circle:"<<endl;
cout<<"Radius: "<<c1.getRadius()<<endl;
cout<<"Diameter: "<<c1.getDiameter()<<endl;
cout<<"Area: "<<c1.getArea()<<endl;
cout<<"Circumference: "<<c1.getCircumference()<<endl;

cout<<"What value do you want to set the radius to? ";
cin>>r;
c1.setRadius(r);

cout<<"Here is the first circle's data after seeting the radius to "<<r<<":"<<endl;
cout<<"Radius: "<<c1.getRadius()<<endl;
cout<<"Diameter: "<<c1.getDiameter()<<endl;
cout<<"Area: "<<c1.getArea()<<endl;
cout<<"Circumference: "<<c1.getCircumference()<<endl;

circle c2(12.5);

cout<<"Here is the second circle's data:"<<endl;
cout<<"Radius: "<<c2.getRadius()<<endl;
cout<<"Diameter: "<<c2.getDiameter()<<endl;
cout<<"Area: "<<c2.getArea()<<endl;
cout<<"Circumference: "<<c2.getCircumference()<<endl;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote