fork download
  1. #include <cstdio>
  2. #include <vector>
  3.  
  4. enum eShapeType{
  5. SPHERE,
  6. CONE,
  7. HULL,
  8. CONVEX,
  9. COMPLEX
  10. };
  11.  
  12. class Shape{
  13. public:
  14. ~Shape(void){};
  15. virtual eShapeType type(void)const = 0;
  16. };
  17.  
  18. class Convex: public Shape {
  19. public:
  20. virtual ~Convex(void){};
  21. virtual void print(void)const = 0;
  22. virtual eShapeType type(void)const{
  23. return CONVEX;
  24. }
  25. };
  26.  
  27. class Sphere: public Convex{
  28. public:
  29. void print(void)const{
  30. printf("This is a sphere!\n");
  31. }
  32. virtual eShapeType type(void)const{
  33. return SPHERE;
  34. }
  35. };
  36.  
  37. class Cone: public Convex{
  38. public:
  39. void print(void)const{
  40. printf("This is a cone!\n");
  41. }
  42. virtual eShapeType type(void)const{
  43. return CONE;
  44. }
  45. };
  46.  
  47. void print_shape(const Shape& shape);
  48.  
  49. class Complex: public Shape{
  50. public:
  51. Complex(const std::vector<Shape*>& shapes):
  52. shapes_(shapes)
  53. {}
  54. void print_shapes(void)const{
  55. printf("This is a complex!\n");
  56. for(auto shape: shapes_) print_shape(*shape);
  57. }
  58. virtual eShapeType type(void)const{
  59. return COMPLEX;
  60. }
  61. const std::vector<Shape*>& get_shapes(void)const{
  62. return shapes_;
  63. }
  64. private:
  65. std::vector<Shape*> shapes_;
  66. };
  67.  
  68. class Hull: public Convex{
  69. public:
  70. Hull(const std::vector<Convex*>& shapes):
  71. shapes_(shapes)
  72. {}
  73. void print(void)const{
  74. printf("This is a hull!\n");
  75. for(const auto& shape: shapes_) shape->print();
  76. }
  77. virtual eShapeType type(void)const{
  78. return HULL;
  79. }
  80. const std::vector<Convex*>& get_shapes(void)const{
  81. return shapes_;
  82. }
  83. private:
  84. std::vector<Convex*> shapes_;
  85. };
  86.  
  87. void print_shape(const Shape& shape){
  88. if(shape.type() < CONVEX) static_cast<const Convex&>(shape).print();
  89. else static_cast<const Complex&>(shape).print_shapes();
  90. }
  91.  
  92. int main(int argc, char* argv[]){
  93.  
  94. Sphere sphere;
  95. Cone cone;
  96.  
  97. Hull hull(std::vector<Convex*>{
  98. &sphere, &cone
  99. });
  100.  
  101. Complex comp1(std::vector<Shape*>{
  102. &sphere, &cone
  103. });
  104.  
  105. Complex comp2(std::vector<Shape*>{
  106. &sphere, &cone, &comp1
  107. });
  108.  
  109. print_shape(hull);
  110. print_shape(comp1);
  111. print_shape(comp2);
  112.  
  113. return 0;
  114. }
  115.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
This is a hull!
This is a sphere!
This is a cone!
This is a complex!
This is a sphere!
This is a cone!
This is a complex!
This is a sphere!
This is a cone!
This is a complex!
This is a sphere!
This is a cone!