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

I already have the codes, but there is a error in it, need some help on my C++ p

ID: 3700846 • Letter: I

Question

I already have the codes, but there is a error in it, need some help on my C++ program assignment, this is the question below:

Write a program that demonstrates the Circle class by asking the users for the circle's radius, creating a Circle object, and then reporting the circle's area, diameter, and circumference.

I hope someone can help me fix the error i have in my code, Thank you!

class Circle{

private:

double radius;

double pi = 3.14159;

public:

Circle();

void setRadius(double);

double getRadius() const

{ return radius; }

double getArea() const

{ return pi * radius * radius; }

double getDiameter() const

{ return radius * 2; }

double getCircumference() const

{ return 2 * pi * radius; }

};

#include <iostream>

#include <cstdlib>

#include <iomanip>

using namespace std;

Circle::Circle(){

radius = 0.0;

}

void Circle::setRadius(double rad){

if(rad >= 0)

   radius = rad;

else{

cout << "Invalid radius ";

exit(EXIT_FAILURE);

}

}

int main(){

double radius;

cout << "Enter the radius of the circle:" << endl;

cin >> radius;

  

Circle c(radius);

  

cout << "Area of the circle:" << c.getArea() << endl;

cout << "Diameter of the circle:" << c.getDiameter() << endl;

cout << "Circumference of the circle is:" << c.getCircumference() << endl;

return 0;

}

Explanation / Answer

#include <iostream>
#include <cstdlib>

#include <iomanip>
using namespace std;
class Circle{

private:

double radius;

double pi = 3.14159;

public:
Circle(){

radius = 0.0;

}
Circle(double r){

setRadius(r);

}

double getRadius()
{ return radius; }

double getArea() const

{ return pi * radius * radius; }

double getDiameter() const

{ return radius * 2; }

double getCircumference() const

{ return 2 * pi * radius; }
  
void setRadius(double rad){

if(rad >= 0)

radius = rad;

else{

cout << "Invalid radius ";

exit(EXIT_FAILURE);

}

}

};

int main(){

double radius;

cout << "Enter the radius of the circle:" << endl;

cin >> radius;

  

Circle c(radius);

  

cout << "Area of the circle:" << c.getArea() << endl;

cout << "Diameter of the circle:" << c.getDiameter() << endl;

cout << "Circumference of the circle is:" << c.getCircumference() << endl;

return 0;

}

Output: