fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. // *****************************************************************************
  5.  
  6. class Figure {
  7. public:
  8. virtual double Area() = 0;
  9. void PrintName() {
  10. std::cout << "Фигура = " << Name() << " ";
  11. }
  12. private:
  13. virtual std::string Name() = 0;
  14. };
  15.  
  16. // *****************************************************************************
  17.  
  18. class Rectangle: public Figure {
  19. public:
  20. explicit Rectangle(double h, double w): H(h), W(w) {}
  21. double Area() override {
  22. return H*W;
  23. }
  24. private:
  25. double H,W;
  26. std::string Name() override {
  27. return "Прямоугольник";
  28. }
  29. };
  30.  
  31. // *****************************************************************************
  32.  
  33. class Square: public Rectangle {
  34. public:
  35. explicit Square(double l): Rectangle (l,l) {}
  36. private:
  37. std::string Name() override {
  38. return "Квадрат";
  39. }
  40. };
  41.  
  42. // *****************************************************************************
  43.  
  44. class Ellipse: public Rectangle {
  45. public:
  46. explicit Ellipse(double a, double b): Rectangle (a,b) {}
  47. double Area() override {
  48. return Rectangle::Area()*3.1415;
  49. }
  50. private:
  51. std::string Name() override {
  52. return "Эллипс";
  53. }
  54. };
  55.  
  56. // *****************************************************************************
  57.  
  58. int main() {
  59. std::vector<Figure*> V;
  60. V.push_back(new Rectangle(2,3));
  61. V.push_back(new Square(4));
  62. V.push_back(new Ellipse(2,3));
  63. for(const auto &i:V) {
  64. i->PrintName();
  65. std::cout << ", площадь: " << i->Area() << std::endl;
  66. delete i;
  67. }
  68. return 0;
  69. }
Success #stdin #stdout 0s 4504KB
stdin
Standard input is empty
stdout
Фигура = Прямоугольник , площадь: 6
Фигура = Квадрат , площадь: 16
Фигура = Эллипс , площадь: 18.849