Question: // User-defined exception class. #include #include using namespace std; class negativeNumber { public: negativeNumber() { message = this Number cannot be negative; } negativeNumber(string str)
// User-defined exception class.
#include
#include
using namespace std;
class negativeNumber
{
public:
negativeNumber()
{
message = "this Number cannot be negative";
}
negativeNumber(string str)
{
message = str + " cannot be negative";
}
string what()
{
return message;
}
private:
string message;
};
//////////////////////////////main////////////////
#include
#include
#include "cylinderType.h"
using namespace std;
int main()
{
cylinderType cylinderObject;
double userHeight;
double userRadius;
cout << " enter the height of the cylinder: ";
cin >> userHeight;
cout << " enter the radius: ";
cin >> userRadius;
cylinderObject.setDimension(userHeight, userRadius);
cout << fixed << showpoint << setprecision(2);
cout << " Cylinder dimensions: ";
cylinderObject.print();
cout << endl;
cout << "Volume: "
<< cylinderObject.volume() << endl;
return 0;
}
////////////////////////headerfile//////////////////////
#ifndef _cylinderType_h_
#define _cylinderType_h_
class cylinderType {
public:
void setDimension(double h, double r);
double volume() const;
void print() const;
cylinderType();
private:
double height;
double radius;
};
#endif
//////////////////////////////cppfile/////////////////////
#include
#include
#include "cylinderType.h"
using namespace std;
const double pi = 3.1416;
void cylinderType::setDimension(double h, double r)
{
height = h;
if (r >= 0)
radius = r;
else
radius = 0;
}
double cylinderType::volume() const
{
return pi * pow(radius, 2) * height;
}
void cylinderType::print() const
{
cout << "height = " << height
<< "; radius = " << radius;
}
cylinderType::cylinderType()
{
cout << "*****************************************************************" << endl;
cout << "* Volume and Surface Area of a Cylinder *" << endl;
cout << "***************************************************************** " << endl;
}
Modify the cylinderType and its client (main) program such that negativeNumber class can be incorporated into them and the associated exception can be properly handled.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
