Circle (using classes and exception handler) Posted by JanWan Last Updated: November 13, 2012 2662 J.W. PRODUCTION Font NameReal font size JanWan I'm currently working on this question... Delete Comment Samath Check this out. http://sourcecodeera.com/blogs/Samath/Circle-Class-in-C.aspx Delete Comment JanWan The interface for this question (Circle.h) class Circle Delete Comment JanWan #include <iostream> #include <cmath> using namespace std; class Circle { private: //Private member double radius; //Declare variable public: //Public data members void setRadius(double); //Mutator double getRadius(); //Accessor //Other member function double getArea(); double getDiameter(); double getCircumference(); //Constructors Circle(double); Circle(); }; Delete Comment JanWan This is the implementation of the circle Circle.cpp Delete Comment JanWan #include <cstdlib> #include <iostream> #include <cmath> #include "Circle.h" #include <stdexcept> #include "ZeroException.h" using namespace std; const double pi = 3.14159; //Radius function/ Mutator void Circle::setRadius(double R) { //Throw exception if radius is 0 if(R == 0.0) { throw ZeroException(); //Throw an exception } radius = R; //set R value to variable radius } //Radius function/ Accessor double Circle::getRadius() { return radius; //return value of radius } //Area function double Circle::getArea() { double area; //Declare variable return area = pi * radius * radius; //calculation of area } //Diameter function double Circle::getDiameter() { double dia; //Declare variable return dia = radius * 2; //calculate and return diameter } //Circumference function double Circle::getCircumference() { double CirF; //Declare variable return CirF = 2 * pi * radius; //Calculate and return the circumference } //Constructor with argument Circle::Circle(double R) { setRadius(R); //set variable in constructor } //Default constructor Circle::Circle() { radius = 1.0; //set radius to 1.0 } J.W. PRODUCTION Delete Comment JanWan Main/Tester for the program Delete Comment JanWan #include <cstdlib> #include <iostream> #include <cmath> #include "Circle.h" #include "ZeroException.h" using namespace std; int main(int argc, char *argv[]) { double rad; //Declare variable cout<<"Enter the circle radius: "; //Try contain code that might cause exception and code that //should not execute if exception occur try { cin>>rad; //input value Circle cork(rad); //instant of the class cout<<endl; //Display result cout<<"Radius: \t\t "<<cork.getRadius()<<endl; cout<<"Area of circle: \t "<<cork.getArea()<<endl; cout<<"Diameter of circle: \t "<<cork.getDiameter()<<endl; cout<<"Circumference of circle: "<<cork.getCircumference()<<endl; cout<<endl; } //ZeroException handles by Exception Handler catch(ZeroException &Zero) //catch an exception { cout<<"Exception occured: "<<Zero.what()<<endl; cout<<endl; } system("PAUSE"); return 0; } Delete Comment JanWan Screen Shot for Class Circle Program Enter the value radius Results of Radius Error message from Exception Handling Zero can not be enter as a valid value