Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

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 //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" <> 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 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 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 : "<> length;//accept the input from the standard input device square.setLength(length); cout << "Shape ID - " << square.getShapeID() << endl; cout << "Shape type - " << square.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 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 : "<> length;

rectangle.setLength(length); cout<< "Enter width of one side of the rectangle : "<> 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 "<

//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 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

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Implementing Ai And Machine Learning For Business Optimization

Authors: Robert K Wiley

1st Edition

B0CPQJW72N, 979-8870675855

More Books

Students explore these related Databases questions

Question

Discuss the techniques of job analysis.

Answered: 3 weeks ago

Question

Explain the guideline for job description.

Answered: 3 weeks ago

Question

What are the objectives of job evaluation ?

Answered: 3 weeks ago