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

Write a Circle class that has the following member variables:

• radius: double

• 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.

• 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: 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()
             {
                  radius = 1.0;   
             }
              Circle::Circle(double radi)
             {
                 if( radi == 0 || radi == 0.0)
                 {
                     ZeroException ex;
                     ex.Checkradius(); 
                 }
                 else
                 {
                   radius = radi;  
                 }                   
               
             }
            
             
             void Circle::setRadius(double r)
             { 
              if( r == 0 || r == 0.0)
                 {
                     ZeroException ex;
                     ex.Checkradius(); 
                 }
                 else
                 {
                   radius = r;  
                 } 
             }
             
             double Circle::getRadius()
             {
                return radius;   
             }
             
             double Circle::getArea()
             {
                return pi * radius * radius;
             }
             
             double Circle::getDiameter()
             {
                   return radius * 2;
             }
             
             double Circle::getCircumference()
             {
                  return 2 * pi * radius;  
             }
             
             
             void  ZeroException::Checkradius()
            {
                    throw 0.0;                   
            }

 

Circle.h

#ifndef CIRCLEH
#define CIRCLEH
 
#include <iostream>
#include <cstdlib>
 
using namespace std;
 
class Circle
{
      public:
             Circle();
             
             Circle(double radi);
             
             void setRadius(double r);
             
             double getRadius();
             
             double getArea();
             
             double getDiameter();
             
             double getCircumference();
             
             
      private:
              double radius;
             static const  double pi = 3.14159;     
};
 
#endif

 

ZeroException.h

#ifndef ZeroExceptionH
#define ZeroExceptionH
 
#include <iostream>
#include <cstdlib>
 
using namespace std;
 
class ZeroException
{
      public:
            void  Checkradius();
                
};
 
#endif

 

Tester.cpp

#include <cstdlib>
#include <iostream>
#include "Circle.h"
 
using namespace std;
 
int main(int argc, char *argv[])
{
    
           for(;;)
           {
            try
             {
               double radius;
               cout<<"Enter the radius: ";
               cin>>radius;  
              Circle cir(radius);
              cout<<"Radius: "<<cir.getRadius()<<endl;
              cout<<"Area: "<<cir.getArea()<<endl;
              cout<<"Diameter: "<<cir.getDiameter()<<endl;
              cout<<"Circumference: "<<cir.getCircumference()<<endl;
              break;      
              } 
              catch(double e)
              {
                    cout<<"The radius cannot be 0.0"<<endl<<endl;
                    continue; 
              } 
              }
   
    
    system("PAUSE");
    return 0;
}
Related Content
Simple Data Class using C++
Simple Data Class using C++
Samath | Jan 07, 2021
Account Class in C++
Account Class in C++
Samath | Jan 07, 2021
Car Class using C++
Car Class using C++
Samath | Jan 07, 2021
Invoice class using Java
Invoice class using Java
Samath | Jan 17, 2024
Movie Class in C++
Movie Class in C++
Samath | Jan 20, 2024
Rectangle Class in C++
Rectangle Class in C++
Samath | Jan 17, 2024
Movie Class in Java
Movie Class in Java
Samath | Jan 20, 2024
Student Class in Java
Student Class in Java
Samath | Jan 20, 2024
Glossary Class using Java
Glossary Class using Java
Samath | Jan 20, 2024
Vehicle Class using Java
Vehicle Class using Java
Samath | Jan 18, 2024
Phone Class using Java
Phone Class using Java
Samath | Jan 17, 2024
Airplane Class in Java
Airplane Class in Java
Samath | Jan 17, 2024