fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. class P {
  8. private:
  9. string nom;
  10.  
  11. public:
  12. P(string n) : nom(n) {
  13.  
  14. }
  15.  
  16. virtual string toString() const {
  17. return "P_" + nom;
  18. }
  19. };
  20.  
  21.  
  22.  
  23. class I {
  24. public:
  25. I(const P &p_, int a) : p(p_), amount(a) {
  26. }
  27.  
  28. string desc() const {
  29. string s;
  30. s += " I_";
  31. // call P::toString or PC::toString() ?
  32. s += p.toString();
  33.  
  34. return s;
  35. }
  36.  
  37. private:
  38. const P &p; // could be PC or P
  39. int amount;
  40.  
  41. };
  42.  
  43.  
  44. class R {
  45.  
  46. private:
  47. vector<I> ps;
  48. string nom;
  49.  
  50. public:
  51. R(string n) : nom(n) {
  52. }
  53.  
  54. void add(const P & p) {
  55. I i(p, 100);
  56. ps.push_back(i);
  57. }
  58.  
  59. string toString() const {
  60. string s;
  61. for (auto i : ps) {
  62. s += i.desc();
  63. }
  64. return s;
  65. }
  66.  
  67.  
  68. };
  69.  
  70.  
  71. class PC : public P {
  72.  
  73. private:
  74. R r;
  75.  
  76.  
  77. public:
  78.  
  79. PC(string n) : P(n), r(n) {
  80. }
  81.  
  82. virtual string toString() const override {
  83. string s;
  84. s += "PC ";
  85. s += P::toString();
  86. s += r.toString();
  87.  
  88. return s;
  89. }
  90.  
  91. void addToR(const P & p) {
  92. r.add(p);
  93. }
  94.  
  95.  
  96. };
  97.  
  98. int main() {
  99.  
  100. P p1("abc");
  101. P p2("def");
  102.  
  103. PC pc("abc_def");
  104. pc.addToR(p1);
  105. pc.addToR(p2);
  106.  
  107. cout << pc.toString() << endl;
  108.  
  109. P p3("hij");
  110. PC pc2("hij_abc_def");
  111. pc2.addToR(p3);
  112. pc2.addToR(pc);
  113.  
  114. cout << pc2.toString() << endl;
  115.  
  116. return 0;
  117.  
  118. }
  119.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
PC P_abc_def I_P_abc I_P_def
PC P_hij_abc_def I_P_hij I_PC P_abc_def I_P_abc I_P_def