fork(1) 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. virtual string toString() const {
  15. return "P_" + nom +"\n" ;
  16. }
  17.  
  18. virtual P * adapter(double nbFois) {
  19. return this;
  20. }
  21.  
  22. };
  23.  
  24.  
  25.  
  26. class I {
  27. public:
  28. I(const P & p_, int a) : p(p_), amount(a) {
  29. }
  30.  
  31. string desc() const {
  32. string s;
  33. s += " " + to_string(amount) + " * ";
  34. s += " I_";
  35. // call P::toString or PC::toString() ?
  36. s += p.toString();
  37.  
  38. return s;
  39. }
  40.  
  41. private:
  42. const P & p;
  43. int amount;
  44.  
  45. };
  46.  
  47.  
  48. class R {
  49.  
  50. private:
  51. vector<I> is;
  52. string nom;
  53.  
  54. public:
  55. R(string n) : nom(n) {
  56. }
  57.  
  58. void add(const P & p, int amount) {
  59. I i(p, amount);
  60. is.push_back(i);
  61. }
  62.  
  63. string toString() const {
  64. string s;
  65. for (auto i : is) {
  66. s += " ";
  67. s += i.desc() + "\n";
  68. }
  69. return s;
  70. }
  71.  
  72.  
  73. };
  74.  
  75.  
  76. class PC : public P {
  77.  
  78. private:
  79. R r;
  80.  
  81.  
  82. public:
  83.  
  84. PC(string n) : P(n), r(n) {
  85. }
  86.  
  87. string toString() const {
  88. string s;
  89. s += "PC ";
  90. s += P::toString();
  91. s += r.toString();
  92.  
  93. return s;
  94. }
  95.  
  96. void addToR(const P & p, int amount) {
  97. r.add(p, amount);
  98. }
  99.  
  100. PC * adapter(double nbFois) {
  101. cout << "PC_adapt" << endl;
  102. return this;
  103. }
  104.  
  105.  
  106. };
  107.  
  108. int main() {
  109.  
  110. P p1("abc");
  111. P p2("def");
  112.  
  113. PC pc("abc_def");
  114. pc.addToR(p1, 1);
  115. pc.addToR(p2, 1);
  116.  
  117. cout << pc.toString() << endl;
  118.  
  119. P p3("hij");
  120. PC pc2("hij_abc_def");
  121. pc2.addToR(p3, 1);
  122. pc2.addToR(pc, 2);
  123.  
  124. cout << pc2.toString() << endl;
  125.  
  126.  
  127. return 0;
  128.  
  129. }
  130.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
PC P_abc_def
   1 *  I_P_abc

   1 *  I_P_def


PC P_hij_abc_def
   1 *  I_P_hij

   2 *  I_PC P_abc_def
   1 *  I_P_abc

   1 *  I_P_def