fork download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. using namespace std;
  5.  
  6. class Content {
  7. public:
  8. virtual void info() = 0;
  9. };
  10.  
  11. class ContentA : public Content {
  12. public:
  13. void info() override { cout << "Content A" << endl; }
  14. int serial() { return 123; }
  15. int num() { return 456; }
  16. };
  17.  
  18. class ContentB : public Content {
  19. public:
  20. void info() override { cout << "Content B" << endl; }
  21. int identifier() { return 789; }
  22. };
  23.  
  24. class Container {
  25. public:
  26. Container(shared_ptr<Content> content) : m_content(content) {}
  27. virtual void info() = 0;
  28. void contentInfo() { m_content->info(); }
  29. protected:
  30. shared_ptr<Content> m_content;
  31. };
  32.  
  33. class ContainerA : public Container {
  34. public:
  35. ContainerA(shared_ptr<ContentA> content) : Container(content) {}
  36. void info() override {
  37. auto identifier = m_content->serial() * m_content->num();
  38. cout << "Container A: " << identifier << endl;
  39. }
  40. };
  41.  
  42. class ContainerB : public Container {
  43. public:
  44. ContainerB(shared_ptr<ContentB> content) : Container(content) {}
  45. void info() override {
  46. cout << "Container B: " << m_content->identifier() << endl;
  47. }
  48. };
  49.  
  50. int main() {
  51. ContainerA containerA(make_shared<ContentA>());
  52. containerA.info();
  53. containerA.contentInfo();
  54.  
  55. ContainerB containerB(make_shared<ContentB>());
  56. containerB.info();
  57. containerB.contentInfo();
  58.  
  59. return 0;
  60. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In member function ‘virtual void ContainerA::info()’:
prog.cpp:37:35: error: ‘class Content’ has no member named ‘serial’
      auto identifier = m_content->serial() * m_content->num();
                                   ^~~~~~
prog.cpp:37:57: error: ‘class Content’ has no member named ‘num’
      auto identifier = m_content->serial() * m_content->num();
                                                         ^~~
prog.cpp: In member function ‘virtual void ContainerB::info()’:
prog.cpp:46:44: error: ‘class Content’ has no member named ‘identifier’
      cout << "Container B: " << m_content->identifier() << endl;
                                            ^~~~~~~~~~
stdout
Standard output is empty