Question: Is my program correct? How do I get the area diameter and circumference to output with only 2 decimal places and still define pi and

Is my program correct?

How do I get the area diameter and circumference to output with only 2 decimal places and still define pi and radius as floats?

I am using visual studio 2013 and answers need to adhere to lateset c++ formatting and common use rules(no code from 2012).

This is the homework problem below.

Write a C++ program that will use a class called Circle that has the following member variables:

radius: a float

pi: a float initialized with the value 3.14159

The class should have the following member functions:

constructor - sets the radius value = 0.0.

setRadius - a mutator function for the radius variable.

getRadius - an accessor function for the radius variable.

getArea - returns the area of a circle, which is calculated as:

area = pi * radius * radius

getDiameter - returns the diameter of a 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 circle's radius, creating a Circle object, and then reporting the circle's area, diameter, and circumference with 2 decimal places each.

Here is what i have so far.

//Header files (Preprocessor directives) #include

//Namespaces using namespace std;

class Circle //Class Declaration { private: //Access Specifier float radius; //Member Variables float pi = 3.14159;

public: Circle(); Circle(double); void setRadius(double); double getRadius(); double getArea(); double getDiameter(); double getCircumference(); ~Circle(); };

Circle::Circle() { radius = 0.0; }

Circle::Circle(double r) { setRadius(r); }

//Mutator Memeber Function void Circle::setRadius(double r) { radius = r; }

//Accessor Functions double Circle::getRadius() { return radius; }

double Circle::getArea() { double area = pi * radius * radius; return area; }

double Circle::getDiameter() { double diameter = 2.0 * radius; return diameter; }

double Circle::getCircumference() { double circumference = 2.0 * pi * radius; return circumference; }

Circle::~Circle() {

}

void display(Circle circ);

double getInput();

void validate(double& n);

int main() { double rad;

Circle circle;

rad = getInput();

circle.setRadius(rad);

display(circle);

return 0;

}

double getInput() { double n;

cout << " \tEnter the radius of the circle ";

cin >> n;

validate(n);

return n; }

void validate(double& n) { while (!cin || n <= 0) { cout << "\tInvalid input, please enter a number > 0 \t"; fseek(stdin, 0, SEEK_END); cin.clear(); cin >> n; } }

void display(Circle circ) {

cout << " \tThe Area of the circle is " << circ.getArea(); cout << " \tThe diameter of the circle is " << circ.getDiameter(); cout << " \tThe circumference of the circle is " << circ.getCircumference(); cout << " "; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!