Question: Create two C++ custom exception classes that inherit from std::exception to handle invalid dimensions for shapes (such as 0 and negative numbers). The problem is
Create two C++ custom exception classes that inherit from std::exception to handle invalid dimensions for shapes (such as 0 and negative numbers).
The problem is that the program is not executing the try/catch
Here's the code:try/catch added to circle shape only untl problem is solved.
//main.cpp
#include //Need this header file to support the C++ I/O system #include "circle.cpp"// including the header and implementation files #include "square.cpp" #include "rectangle.cpp" #include "shape.cpp" using namespace std; //Telling the compiler to use name space where C++ library is declared int main( ) { double radius = 0; double length = 0; double width = 0; Square square; Circle circle; Rectangle rectangle; int shapeid;//shape properties string shapetype; string shapeunit; int menuOption;//declare menu selection variable do { // Initialize while loop cout << endl << "_______________Calculate Area______________" << endl; cout << "Select an Object" << endl; cout << " 1: Circle" << endl; cout << " 2: Square" << endl; cout << " 3: Rectangle" < cout << " 0: Exit" << endl; cout << " Enter Your Choice: "; cin >> menuOption; switch(menuOption)//test variables against a list of values by calling a case { case 1://cycle a potential number of times until the while condition is true { //cout<< "Enter the radius : " ;//user enter values //cin>> radius;//accept the input from the standard input device
try { cout<< "Enter the radius : " ;//user enter values cin>> radius;//accept the input from the standard input device if (radius < 0) { NegativeValueException e; throw e; if (radius == 0) { ZeroValueException e; throw e; } } } catch(NegativeValueException &e) { e.what(); } catch(ZeroValueException &e) { e.what(); } circle.setRadius(radius);
cout<<"Enter shape id : "; cin>>shapeid;//accept the input from the standard input device circle.setShapeID(shapeid);//base class cout<<"Enter shape name: "; cin>>shapetype;//accept the input from the standard input device circle.setShape_type(shapetype); cout<<"Enter shape unit of measure of 'mm^2' for area: ";//Square Millimeter (mm2) is a metric unit of area equal to 0.000001 square meters cin>>shapeunit;//accept the input from the standard input device circle.setUnit(shapeunit); cout << "Shape ID - " << circle.getShapeID() << endl; cout << "Shape type - " << circle.getShape_type() << endl; cout<< "Area of the circle is "< break;//the loop is immediately terminated and program control resumes at the next statement following the loop } case 3://cycle a potential number of times until the while condition is true { cout<<"Enter shape id : ";//user enter values cin>>shapeid;//accept the input from the standard input device rectangle.setShapeID(shapeid);//base clase cout<<"Enter shape type (shape name): "; cin>>shapetype;//accept the input from the standard input device rectangle.setShape_type(shapetype); cout<<"Enter shape unit of measure for area (mm^2): ";//Square Millimeter (mm2) is a metric unit of area equal to 0.000001 square meters cin>>shapeunit;//accept the input from the standard input device rectangle.setUnit(shapeunit); cout<< "Enter length of one side of the rectangle : "< cin >> length;
rectangle.setLength(length); cout<< "Enter width of one side of the rectangle : "< cin>> width;//accept the input from the standard input device rectangle.setWidth(width); cout << "Shape ID - " << rectangle.getShapeID() << endl; cout << "Shape type - " << rectangle.getShape_type() << endl; cout<< "Area of the rectangle is "<} } } while(menuOption!=0);//until the value is greater than 0 cout<<" ________________ END PROGRAM ___________________" < }()>
//exception.h
#include using namespace std;//telling the compiler to use names pace where C++ library is declared class NegativeValueException : public exception { public: virtual const char * what () const throw () { return "Error:Negative value"; } }; class ZeroValueException : public exception { public: virtual const char * what () const throw () { return "Error:Zero value"; } };
//shape.cpp
#include "shape.h"//including the header file #include //need this header file to support the C++ I/O system #include //telling the compiler to use name space where C++ library is declared
using namespace std;//telling the compiler to use names pace where C++ library is declared Shape::Shape(){//declare method area = 0; } double Shape::getArea(){ return area; } void Shape::setShapeID(int Shape_ID) { ShapeID = Shape_ID; } int Shape::getShapeID(void) { return ShapeID; } void Shape::setShape_type(string Shape_type)//writing to output parameters; control returns back to the caller { ShapeType = Shape_type; } string Shape::getShape_type(void) { return ShapeType; } void Shape::setUnit(string unit) { unit = unitM; } string Shape::getUnit(void) { return unitM; }
//shape.h
#ifndef SHAPE_H #define SHAPE_H #include #include using namespace std; class Shape { // Base Class public: Shape(); double getArea(); void setShapeID(int Shape_ID); void setShape_type(string Shape_type); void setUnit(string unit); int getShapeID(void); string getShape_type(void); string getUnit(void); int ShapeID; string ShapeType; string unitM; private: double area; }; #endif // SHAPE_H_INCLUDED
//circle.cpp
#include "circle.h"//include the header file #include //need this header file to support the C++ I/O system
using namespace std;//telling the compiler to use names pace where C++ library is declared
Circle::Circle (double r=0)// declare circle method { radius = r; } double Circle::getRadius() {//iterate through the list of attributes return radius;//returns the count of attributes } void Circle::setRadius (double r){//writing to output parameters; control returns back to the caller radius = r;//Parameterized constructor } double Circle::getArea(){ return radius * radius * 3.14159;//retrieve and store value of the area }
//circle.h
#ifndef CIRCLE_H #define CIRCLE_H #include "shape.h"//including the header file #include "exception.h"
using namespace std;//telling the compiler to use names pace where C++ library is declared
class Circle : public Shape//circle structure class { public://declare circle access specifier Circle (double r);//parameterized constructor double getRadius();//retrieve radius value void setRadius (double r);//set radius value double getArea();//retrieve area value private: double radius;//declare value for the radius access specifiers }; #endif // CIRCLE_H INCLUDED
//square.cpp
#include "square.h"//including the header file #include //need this header file to support the C++ I/O system using namespace std;//telling the compiler to use names pace where C++ library is declared
Square::Square(double len)//declare method used for square shape { length = len;//parameterized constructor }
double Square::getLength() {//iterate through the list of attributes return length;//returns the count of attributes }
void Square::setLength(double len){//writing to output parameters; control returns back to the caller length = len; }
double Square::getArea(){ return length * length;//returning length & width }
//square.h
#ifndef SQUARE_H #define SQUARE_H
#include //need this header file to support the C++ I/O system #include "shape.h"//including the header file
using namespace std;//telling the compiler to use names pace where C++ library is declared class Square : public Shape//square class { public://declare square access specifier Square (double length =0);//parameterized constructor double getLength();//retrieve length void setLength(double length);//set length virtual double getArea ();//retrieve area private: double length;// length value declaration access specifier }; #endif // SQUARE_H INCLUDED
//rectangle.cpp
#include "rectangle.h"//including the header file #include //need this header file to support the C++ I/O system
using namespace std;//telling the compiler to use names pace where C++ library is declared Rectangle::Rectangle(double len, double wid)//declare method used for rectangle shape { length = len; width = wid; } double Rectangle::getLength() {//iterate through the list of attributes return length;//returns the count of attributes } void Rectangle::setLength(double len){//writing to output parameters; control returns back to the caller length = len; } double Rectangle::getWidth() { return width; } void Rectangle::setWidth(double wid){ width= wid; } double Rectangle::getArea(){ return length * width;//returning length and width }
//rectangle.h
#ifndef RECTANGLE_H_INCLUDED #define RECTANGLE_H_INCLUDED
#include //need this header file to support the C++ I/O system #include "shape.h"//including the header file
class Rectangle: public Shape//square structure class { public://declare square access specifier Rectangle (double length =0, double width =0);//parameterized constructor double getLength();//retrieve length double getWidth();//retrieve width void setLength(double length);//set length void setWidth(double width);//set width virtual double getArea ();//retrieve area private: double length;//length value declaration access specifier double width;//width value declaration access specifier }; #endif // RECTANGLE_H_INCLUDED
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
