fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <memory>
  4. using namespace std;
  5.  
  6. using ElementTypes = int;
  7. const int DOT=1;
  8.  
  9. class Element {
  10. public:
  11. ElementTypes type = DOT;
  12.  
  13. Element() {}
  14. Element(ElementTypes type) : type(type) {}
  15.  
  16. virtual void Draw() { }
  17. };
  18. class Dot : public Element {
  19. public:
  20. int x, y;
  21.  
  22. Dot(int x, int y) : x(x), y(y) {}
  23.  
  24. void Draw() override {
  25. cout<<"DrawCircle("<<x<<","<< y<<", 2.f, BLACK);"<<endl;
  26. }
  27. ~Dot() { x=-1; y=-1; cout<<"Destroyed"<<endl; } //just to demonstrate before and after destruction
  28. };
  29. class Drawing {
  30. public:
  31. std::vector<shared_ptr<Element>> Elements;
  32.  
  33. void AddDot(shared_ptr<Element> dot) {
  34. Elements.emplace_back(dot);
  35. }
  36.  
  37. void Draw() {
  38. for (auto element : Elements) {
  39. element->Draw();
  40. }
  41. }
  42. };
  43. void init (Drawing&d) {
  44. auto dt1 = make_shared<Dot>(10,30);
  45. d.AddDot(dt1);
  46. }
  47. int main() {
  48. Drawing d;
  49. init (d);
  50. d.Draw();
  51. }
Success #stdin #stdout 0.01s 5524KB
stdin
Standard input is empty
stdout
DrawCircle(10,30, 2.f, BLACK);
Destroyed