shapes
Complete the derived shape classes.
99
 
1
#include <iostream>
2
#include <cmath>
3
#include <string>
4
#include <vector>
5
using namespace std;
6
7
class Shape
8
{
9
 public:
10
  virtual ~Shape() { }
11
  virtual double getArea() = 0;
12
  virtual double getPerimeter() = 0;
13
  virtual string getType() = 0;
14
};
15
16
class RightTriangle :                
17
{
18
 public:
19
  RightTriangle(double b, double h) 
20
21
  ~RightTriangle() { }
22
  double getArea() { return              }
23
  double getPerimeter() { return sqrt(_b*_b + _h*_h) + _h + _b; }
24
  string getType() { return "Right Triangle"; }
25
private:
26
27
};
28
29
class Rectangle :                
30
{
31
public:
32
  Rectangle(double b, double h)
33
34
  ~Rectangle() { }
35
  double getArea() { return _b * _h;  }
36
  double getPerimeter() { return               ; }
37
  string getType() { return "Rectangle"; }
38
private:
39
40
};
41
42
class Square :                    
43
{
44
public:
45
  Square(double s)
46
47
  // Override other virtual functions as needed
48
49
50
};
51
52
int main()
53
{
54
  vector<Shape *> shapeList;
55
56
  int selection = -1;
57
  while(selection != 0){
58
    cout << "Choose an option:" << endl;
59
    cout << "=================" << endl;
60
    cout << "Enter '0' to quit" << endl;
61
    cout << "Enter '1 base height' for a right triangle with given base and height" << endl;
62
    cout << "Enter '2 base height' for a rectangle with given base and height" << endl;
63
    cout << "Enter '3 side' for a square with given side length" << endl;
64
    cout << "> ";
65
    cin >> selection;
66
    // Right Triangle case
67
    if(selection == 1){
68
      double b, h;
69
      cin >> b >> h;
70
      shapeList.push_back(new                      );
71
    }
72
    // Rectangle case
73
    else if(selection == 2){
74
      double b, h;
75
      cin >> b >> h;
76
      // Add the rest of the code to allocate a new rectangle
77
      //  and add it to the shapeList
78
      shapeList.push_back(new                 );
79
    }
80
    // Square case
81
    else if(selection == 3){
82
      double s;
83
      cin >> s;
84
      // Add the rest of the code to allocate a new square
85
      //  and add it to the shapeList
86
      shapeList.push_back(new             );
87
    }
88
  }
89
  cout << endl;
90
  for (vector<Shape *>::iterator it = shapeList.begin() ;
91
       it != shapeList.end();
92
       ++it)
93
    {
94
      Shape *s = *it;
95
      cout << s->getType() << ": Area=" << s->getArea() << " Perim=" << s->getPerimeter() << endl;
96
      delete s;
97
    }
98
  return 0;
99
}