Create two C++ custom exception classes that inherit from std::exception to hand
ID: 3904648 • Letter: C
Question
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.
Fix suggested but still throwing a lot of error: expected . . .
Here's the code:Tested on circle shape only for now.
//main.cpp
#include <iostream>//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" <<endl;
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
circle.setRadius(radius);
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();
}
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 "<<circle.getArea() << " " << circle.getUnit() << endl;
break;//the loop is immediately terminated and program control resumes at the next statement following the loop
}
case 2://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
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 : "<<endl;
cin >> 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 "<<square.getArea() << " " <<square.getUnit() << endl;
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 : "<<endl;
cin >> length;
rectangle.setLength(length);
cout<< "Enter width of one side of the rectangle : "<<endl;
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 "<<rectangle.getArea() << " " << rectangle.getUnit() << endl;
break;//the loop is immediately terminated and program control resumes at the next statement following the loop
}
}
}
while(menuOption!=0);//until the value is greater than 0
cout<<" ________________ END PROGRAM ___________________" <<endl;
}
//exception.h
#include <exception>
using namespace std;//telling the compiler to use names pace where C++ library is declared
class NegativeValueException : public exception
{
virtual const char * what () const throw ()
{
return "Error:Negative value";
}
};
class ZeroValueException : public exception
{
virtual const char * what () const throw ()
{
return "Error:Zero value";
}
};
//shape.cpp
#include "shape.h"//including the header file
#include <iostream>//need this header file to support the C++ I/O system
#include <string>//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 <string>
#include <iostream>
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 <iostream>//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 <iostream>//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 <iostream>//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 <iostream>//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 <iostream>//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
Explanation / Answer
The problem is solved u forgot to include '}' in line 48 at catch block in main.cpp and one more error you get is what() is private in exception.h so make it as public
main.cpp
#include <iostream>//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" <<endl;
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
circle.setRadius(radius);
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();
}
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 "<<circle.getArea() << " " << circle.getUnit() << endl;
break;//the loop is immediately terminated and program control resumes at the next statement following the loop
}
case 2://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
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 : "<<endl;
cin >> 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 "<<square.getArea() << " " <<square.getUnit() << endl;
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 : "<<endl;
cin >> length;
rectangle.setLength(length);
cout<< "Enter width of one side of the rectangle : "<<endl;
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 "<<rectangle.getArea() << " " << rectangle.getUnit() << endl;
break;//the loop is immediately terminated and program control resumes at the next statement following the loop
}
}
}
while(menuOption!=0);//until the value is greater than 0
cout<<" ________________ END PROGRAM ___________________" <<endl;
}
exception.h
#include <exception>
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";
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.