1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED
#include "Shape.h"
class Circle : public Shape //Class Circle inherits from Base Class Shape
{
//Member declarations
private:
double radius;
// Constructors
public:
Circle(std::string &, double, std::string &);
// Parent Virtual function
double getArea() const;
//Function prototypes
double getRadius(); //Get the Radius (getter)
void setRadius(double); //Sets Radius (setter)
};
#endif // CIRCLE_H_INCLUDED
#include "Circle.h"
#include "NegativeException.h"
#include "ZeroException.h"
using namespace std;
const double PI = 3.14159;
//Set radius
Circle::Circle (string &name, double r, string &unit) :
Shape(name, "Circle", unit)
{
radius = r;
}
{
try
{
cout<<"Enter the circle's radius: ";
cin>>radius;
if (radius < 0)
{
throw NegativeException("ERROR: Negative number entered.");
}
else if (radius == 0)
throw ZeroException("ERROR: Zero entered");
}
catch (ZeroException ze)
{
radius = 0;
cout << ze.what();
}
catch(NegativeException ne)
{
radius = 0;
cout << ne.what();
}
}
//calculate area
double Circle::getRadius() { return radius; }
double Circle::getArea() const
{
return (PI * (radius * radius));
}
void Circle::setRadius(double r) { radius = r; }
#include "Square.h"
#include "NegativeException.h"
#include "ZeroException.h"
using namespace std;
//Set length
Square::Square(string &name, double len, string &unit) :
Shape(name, "Square", unit)
{
length = len;
}
//Calculate Area
double Square::getArea() const
{
return length * length;
}
{
try
{
cout<<"Enter Square length: ";
cin>>length;
if (length < 0 )
throw NegativeException("ERROR: Negative number entered.");
else if (length == 0)
throw ZeroException("ERROR: Zero entered");
}
catch (ZeroException ze)
{
length = 0;
cout << ze.what();
}
catch(NegativeException ne)
{
length = 0;
cout << ne.what();
}
}
double Square::getLength() const { return length; }
void Square::setLength(double len){ length = len; }
#ifndef SQUARE_H_INCLUDED
#define SQUARE_H_INCLUDED
#include "Shape.h"
class Square : public Shape //Class Square inherits from Base Class Shape
{
//Member declarations
private:
double length;
//Constructors
public:
Square(std::string &, double, std::string &);
//Parent Virtual function
double getArea() const;
//Function prototypes
double getLength() const; //Get length (getter)
void setLength(double); //Set length (setter)
};
#endif // SQUARE_H_INCLUDED
#include "Rectangle.h"
#include "NegativeException.h"
#include "ZeroException.h"
using namespace std;
//Set length, width
Rectangle::Rectangle(string &name, double len, double wid, string &unit) :
Shape(name, "Rectangle", unit)
{
length = len;
width = wid;
}
try
{
cout<<"Enter Rectangle length: ";
cin>>length;
cout<<"Enter Rectangle width: ";
cin>>width;
if (length < 0 || width < 0)
throw NegativeException("ERROR: Negative number entered.");
else if (length == 0 || width == 0)
throw ZeroException("ERROR: Zero entered");
}
catch (ZeroException ze)
{
length = 0;
width = 0;
cout << ze.what();
}
catch(NegativeException ne)
{
length = 0;
width = 0;
cout << ne.what();
}
}
//Calculate Area
double Rectangle::getArea() const
{
return length * width;
}
double Rectangle::getLength() const { return length;}
double Rectangle::getWidth() const {return width;}
void Rectangle::setLength(double len) { length = len;}
void Rectangle::setWidth(double wid) {width = wid;}
|