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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
main.cpp
#include "main.h"
using namespace std;
int main(int argc, char**argv) {
// Prints welcome message...
double length = 0.0;
//double slength = 0.0;
//double swidth = 0.0;
double width = 0.0;
double radius = 0.0;
string unitOfmeasure = "";
for (int i = 0; i<3; i++)
{
try
{
unitOfmeasure = "";
if(i == 0)
{
cout << "Please enter the unit of measurement for rectangle: "<< endl;
cin >> unitOfmeasure;
cout << "Please enter the width of the rectangle: "<< endl;
cin >> width;
cout << "Please enter the length of the rectangle"<< endl;
cin >> length;
Shape * rect = new Rectangle(width,length, unitOfmeasure);
std::cout
<<"ID = "
<< std::fixed << rect->getId()
<<", Type = "
<< std::fixed << rect->getShapeType()
<<", Area = "
<< std::fixed << rect->getArea()
<< std::fixed << rect->getunitOfmeasure()
<<std::endl;
}
else if(i == 2)
{
cout << "Please enter the unit of measurement for Square: "<< endl;
cin >> unitOfmeasure;
cout << "Please enter the length of the Square: "<< endl;
cin >> length;
//cout << "Please enter the width of the Square"<< endl;
//cin >> swidth;
Shape * squ = new Rectangle(length, length, unitOfmeasure);
std::cout
<<"ID = "
<< std::fixed << squ->getId()
<<", Type = "
<< std::fixed << squ->getShapeType()
<<", Area = "
<< std::fixed << squ->getArea()
<< std::fixed << squ->getunitOfmeasure()
<<std::endl;
}
else
{
cout << "Please enter the unit of measurement for circle: "<< endl;
cin >> unitOfmeasure;
cout << "Please enter the radius of the circle: "<< endl;
cin >> radius;
Shape * cir=new Circle(radius, unitOfmeasure);
std::cout
<<"ID = "
<< std::fixed << cir->getId()
<<", Type = "
<< std::fixed << cir->getShapeType()
<<", Area = "
<< std::fixed << cir->getArea()
<< std::fixed << cir->getunitOfmeasure()
<<std::endl;
}
}
catch (string e)
{
}
}
return 0;
}
main.h
#include <iostream>
#include <string>
#include "Rectangle.h"
#include "Square.h"
#include "Circle.h"
#ifndef MAIN_H
#define MAIN_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif /* MAIN_H */
Circle.cpp
#include <cmath>
#include "Circle.h"
#include <string>
#include "ZeroException.h"
#include "negativeexception.h"
using namespace std;
{
try
{
cout<< "Please enter the diameter: ";
cin>> diameter;
if (radius < 0)
{
throw NegativeException("Negative number entered.");
else if (diameter==0)
throw ZeroException("Zero can't be entered.");
}
catch (ZeroException ze)
{
diameter = 0;
cout<< ze.what();
}
catch (NegativeException ne)
{
diameter = 0;
cout << ne.what();
}
}
}
Circle::Circle(float diameter, string unitOfmeasure) :Shape(CIRCLE, unitOfmeasure){
this->diameter=diameter;
}
float Circle::getArea(){
float radius=diameter/2;
return M_PI *(radius*radius);
}
Circle::~Circle() {
}
Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
#include "Shape.h"
#include <string>
using namespace std;
class Circle :public Shape{
public:;
Circle(float diameter, string unitOfmeasure);
float getArea();
virtual ~Circle();
private:
float diameter;
};
#endif /* CIRCLE_H */
NEGATIVEEXCEPTION.h
#include <exception>
#include <string>
#ifndef NEGATIVEEXCEPTION_H_INCLUDED
#define NEGATIVEEXCEPTION_H_INCLUDED
class NegativeException : public std::exception
{
private:
std::string m_msg = "negative number entered \n";
public:
const char* what() const throw() final override
{
return m_msg.c_str();
}
};
#endif // NEGATIVEEXCEPTION_H_INCLUDED
rectangle.cpp
#include "Rectangle.h"
#include <string>
#include "ZeroException.h"
#include "negativeexception.h"
using namespace std;
{
try
{
cout << "Please enter the length of the rectangle: ";
cin >> length;
cout << "Please enter the width of the rectangle: ";
cin >> width;
if (length < 0 || width < 0)
{
throw NegativeException("Negative number entered.");
else if (length == 0 || width == 0)
throw ZeroException("Zero cannot be entered.");
}
catch (ZeroException ze)
{
length = 0;
width = 0;
cout << ze.what();
}
catch (NegativeException ne)
{
length = 0;
width = 0;
cout << ne.what();
}
}
}
Rectangle::Rectangle(float w, float h, string unitOfmeasure):Shape(RECTANGLE, unitOfmeasure) {
width=w;
height=h;
}
float Rectangle::getArea(){ // Makes the calculation to get the area.
return height*width;
}
Rectangle::~Rectangle() {
}
| |