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
|
#include <iostream>
#include <string>
#include <exception> // <===== Possibly need this header
using namespace std;
// The header files are included to use the Class in main
// #include "shape.h"
// #include "circle.h"
// #include "square.h"
// #include "zeroexception.h" // The header is required as exception(Zero value) is raised while calculating area
// #include "negativeexception.h" // The header is required as exception (negative value) is raised while calculating area
// THESE ARE FUDGES OF POSSIBLE HEADER CLASSES - JUST TO GET YOUR CODE TO COMPILE ********************
const float PI = 3.14159;
class Shape
{
protected:
int id;
string unit;
string Stype;
public:
Shape() {}
~Shape() {}
int getId() { return id; }
string getUnit() { return unit; }
virtual float getArea() {}
virtual void display() {}
};
class Circle : public Shape
{
float radius;
public:
Circle( float r, int i, string un ) { radius = r; id = i; unit = un; Stype = "Circle"; }
~Circle() {}
float getRadius() { return radius; }
float getArea() { return PI * radius * radius; }
void display() { cout << Stype << ", id=" << id << " radius=" << radius << " area=" << getArea() << endl; }
};
class Square : public Shape
{
float side;
public:
Square( float s, int i, string un ) { side = s; id = i; unit = un; Stype = "Square"; }
~Square() {}
float getSide() { return side; }
float getArea() { return side * side; }
void display() { cout << Stype << ", id=" << id << " side=" << side << " area=" << getArea() << endl; }
};
class Triangle : public Shape
{
float base;
float height;
public:
Triangle( float b, float h, int i, string un ) { base = b; height = h; id = i; unit = un; Stype = "Triangle"; }
~Triangle() {}
float getBase() { return base; }
float getHeight() { return height; }
float getArea() { return 0.5 * base * height; }
void display() { cout << Stype << ", id=" << id << " base=" << base << " height=" << height << " area=" << getArea() << endl; }
};
class ZeroException: public exception { public: const char* what() const throw() { return "Zero exception happened"; } };
class NegativeException: public exception { public: const char* what() const throw() { return "Negative exception happened"; } };
// END OF FUDGES **********************************************************************************
int main()
{
int menuOption;
int shape_id = 0;
do
{ // Starting of while loop, do-while loop is used so that the menu is shown at least once unless user select exit
shape_id++;
cout << endl << "=========CALCULATE AREA================" << endl;
cout<< endl
<< "Select an Object" << endl
<< " 1: Circle" << endl
<< " 2: Square" << endl
<< " 3: Triangle" << endl
<< " 0: Exit" << endl
<< " Enter Your Choice: ";
cin >> menuOption;
string unit = "Meter";
try
{
switch(menuOption)
{
case 1:
{ // <<===== added brace because of local variables c1 and area1 in this scope
cout<< shape_id << ": Enter radius (in "<<unit<<") : " ;
float radius;
cin >> radius;
/* Demonstration of instantiation Circle class */
Circle c1(radius,shape_id, unit);
/* Demonstration of calling method */
float area1 = c1.getArea();
c1.display();
break;
} // <====== added closing brace
case 2:
{ // <<===== added brace because of local variables s1 and area 2 in this scope
cout<< shape_id << ": Enter length of one side : ";
float length ;
cin >> length;
/* Demonstration of instantiation Square class and storing the reference to base class object */
Shape *s1 = new Square(length,shape_id, unit);
/* Demonstration of calling virtual method, using base object reference of Square class */
float area2 = s1->getArea();
s1->display();
break;
} // <====== added closing brace
case 3:
{ // <<===== added brace because of local variables (eventually!) in this scope
cout<< shape_id << ": Enter base : ";
float base,height ;
cin >> base;
cout<< shape_id << ": Enter height : ";
cin >> height;
cout << "You haven't decided what to do with a triangle yet!" << endl;
// <<===== add whatever you want for a triangle here
} // <====== added closing brace
}
}
catch(ZeroException& exp) // <===== removed extra semicolon
{
cout<<"!! ZeroException caught: " << exp.what() << endl;
}
catch(NegativeException& exp)
{
cout<<"!! NegativeException caught: " << exp.what() << endl;
}
catch(exception& exp)
{
cout<<"!! Standard Exception caught: " << exp.what() << endl;
}
} while(menuOption!=0);
cout << endl<< endl << "============== THANK YOU ===================" << endl<< endl;
return 0;
}
//<===== Removed stray trailing brace
|