Circle Class in C++
Posted by Samath
Last Updated: January 07, 2021

Write a Circle class that has the following member variables:

• pi: a double initialized with the value 3.14159

The class should have the following member function:

• Default constructor: A default constructor that sets radius to 1.0

• Constructor: Accepts the radius of the circle as argument. You are required to use exception handling techniques as discussed in class, if a user enters 0.0 for the radius, throw an exception informing the user that the radius cannot be 0.0 and allow them to enter a new value.

• getArea: Returns the area of a circle, which is calculated as area = pi* radius * radius.

• getDiameter: return the diameter of the circle, which is calculated as radius * 2.

• getCircumference: returns the circumference of the circle, which is calculated as circumference = 2 * pi * radius.

You are required to demonstrate your class in a driver program.

Deliverables: Circle.h, Circle.cpp, tester.cpp, ZeroException.h

Output:

Circle.cpp

```#include <iostream>
#include <cstdlib>
#include "Circle.h"
#include "ZeroException.h"

using namespace std;

Circle::Circle()
{
}
{
{
ZeroException ex;
}
else
{
}

}

{
if( r == 0 || r == 0.0)
{
ZeroException ex;
}
else
{
}
}

{
}

double Circle::getArea()
{
}

double Circle::getDiameter()
{
}

double Circle::getCircumference()
{
return 2 * pi * radius;
}

{
throw 0.0;
}```

Circle.h

```#ifndef CIRCLEH
#define CIRCLEH

#include <iostream>
#include <cstdlib>

using namespace std;

class Circle
{
public:
Circle();

double getArea();

double getDiameter();

double getCircumference();

private:
static const  double pi = 3.14159;
};

#endif```

ZeroException.h

```#ifndef ZeroExceptionH
#define ZeroExceptionH

#include <iostream>
#include <cstdlib>

using namespace std;

class ZeroException
{
public:

};

#endif```

Tester.cpp

```#include <cstdlib>
#include <iostream>
#include "Circle.h"

using namespace std;

int main(int argc, char *argv[])
{

for(;;)
{
try
{
cout<<"Area: "<<cir.getArea()<<endl;
cout<<"Diameter: "<<cir.getDiameter()<<endl;
cout<<"Circumference: "<<cir.getCircumference()<<endl;
break;
}
catch(double e)
{
continue;
}
}

system("PAUSE");
return 0;
}
```
C program that Calculate Area and Circumference of circle
Samath | Dec 28, 2015
Simple Data Class using C++
Samath | Jan 07, 2021
Java program that use radius to calculate area and circumference of a circle and volume and surface area of a sphere
Samath | Feb 09, 2021
Account Class in C++
Samath | Jan 07, 2021
Car Class using C++
Samath | Jan 07, 2021
Invoice class using Java
Samath | Jan 17, 2024
Movie Class in C++
Samath | Jan 20, 2024
Rectangle Class in C++
Samath | Jan 17, 2024
Movie Class in Java
Samath | Jan 20, 2024
Student Class in Java
Samath | Jan 20, 2024
Glossary Class using Java
Samath | Jan 20, 2024
Vehicle Class using Java
Samath | Jan 18, 2024
Phone Class using Java
Samath | Jan 17, 2024
Airplane Class in Java
Samath | Jan 17, 2024