fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. class P {
  8. private:
  9. string name;
  10.  
  11. public:
  12. P(string n) : name(n) {}
  13.  
  14. virtual string toString() const {
  15. return "P_" + name +"\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 Moebel::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 Bauteile {
  49.  
  50. private:
  51. vector<I> teile;
  52. string name;
  53.  
  54. public:
  55. Bauteile(string n) : name(n) {
  56. }
  57.  
  58. void add(const P & p, int amount) {
  59. I i(p, amount);
  60. teile.push_back(i);
  61. }
  62.  
  63. string toString() const {
  64. string s;
  65. for (auto i : teile) {
  66. s += " ";
  67. s += i.desc() + "\n";
  68. }
  69. return s;
  70. }
  71.  
  72.  
  73. };
  74.  
  75.  
  76. class Moebel : public P {
  77.  
  78. private:
  79. Bauteile bauteile;
  80.  
  81.  
  82. public:
  83.  
  84. Moebel(string n) : P(n), bauteile(n) {
  85. }
  86.  
  87. string toString() const {
  88. string s;
  89. s += "Moebel ";
  90. s += P::toString();
  91. s += bauteile.toString();
  92.  
  93. return s;
  94. }
  95.  
  96. void addToBauteile(const P & p, int amount) {
  97. bauteile.add(p, amount);
  98. }
  99.  
  100. Moebel * adapter(double nbFois) {
  101. cout << "Moebel_adapt" << endl;
  102. return this;
  103. }
  104.  
  105.  
  106. };
  107.  
  108. int main() {
  109.  
  110. // Bauteile
  111. P p1("schrauben");
  112. P p2("brett");
  113.  
  114. Moebel regal("Regal");
  115. regal.addToBauteile(p1, 1);
  116. regal.addToBauteile(p2, 4);
  117.  
  118. cout << regal.toString() << endl;
  119.  
  120. P p3("holztuer");
  121. Moebel grosses_regal("Grosses_regal");
  122. grosses_regal.addToBauteile(p3, 2);
  123. grosses_regal.addToBauteile(regal, 2);
  124.  
  125. cout << grosses_regal.toString() << endl;
  126.  
  127.  
  128. return 0;
  129.  
  130. }
  131.  
Success #stdin #stdout 0s 15248KB
stdin
Standard input is empty
stdout
Moebel P_Regal
   1 *  I_P_schrauben

   4 *  I_P_brett


Moebel P_Grosses_regal
   2 *  I_P_holztuer

   2 *  I_Moebel P_Regal
   1 *  I_P_schrauben

   4 *  I_P_brett