Create two C++ custom exception classes that inherit from std::exception to handle invalid dimensions for shapes (such as 0 and negative numbers). Revise the code
Create two C++ custom exception classes that inherit from std::exception to handle invalid dimensions for shapes (such as 0 and negative numbers). Revise the code in your program's classes and main() to use these custom exception classes. Include meaningful comments.
Here's the code:
//main.cpp
#include
//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<<"Enter shape id : "; cin>>shapeid;//accept the input from the standard input device circle.setShapeID(shapeid);//base class
cout<<"Enter shape type (shape name) : "; cin>>shapetype;//accept the input from the standard input device circle.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 circle.setUnit(shapeunit);
cout << "Shape ID - " << circle.getShapeID() << endl; cout << "Shape type - " << circle.getShape_type() << endl; cout<< "Area of the circle is "< cout<<"Enter shape id : ";//user enter values cin>>shapeid;//accept the input from the standard input device square.setShapeID(shapeid);//base class cout<<"Enter shape type (shape name): "; cin>>shapetype;//accept the input from the standard input device square.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 square.setUnit(shapeunit); cout<< "Enter length of one side of the square : "< 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 : "< rectangle.setLength(length); cout<< "Enter width of one side of the rectangle : "< //shape.cpp #include "shape.h"//including the header file #include 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 //circle.cpp #include "circle.h"//include the header file #include 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 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 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 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 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 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
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started