fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <memory>
  4. #include <math.h>
  5.  
  6. using namespace std;
  7.  
  8. class Shape {
  9. float x, y;
  10. public:
  11. virtual float area() = 0;
  12. };
  13.  
  14. class Circle : public Shape {
  15. float radius;
  16. public:
  17. Circle(float radius) : radius{radius} { }
  18.  
  19. virtual float area() {
  20. return M_PI * radius * radius;
  21. }
  22. };
  23.  
  24. class Rect : public Shape {
  25. float width;
  26. float height;
  27. public:
  28. Rect(float width, float height) : width{width}, height{height} { }
  29.  
  30. virtual float area() {
  31. return width * height;
  32. }
  33. };
  34.  
  35. int main()
  36. {
  37. vector<unique_ptr<Shape>> shapes;
  38.  
  39. shapes.push_back(make_unique<Circle>(5));
  40. shapes.push_back(make_unique<Rect>(4,6));
  41.  
  42. float total_area = 0;
  43. for(auto&& shape: shapes) {
  44. total_area += shape->area();
  45. }
  46. cout << "Total area is " << total_area << endl;
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0s 4360KB
stdin
Standard input is empty
stdout
Total area is 102.54