fork(1) download
  1. #include <iostream>
  2. //#include "Decaf.h"
  3. //#include "Espresso.h"
  4. //#include "SoyDecorator.h"
  5. //#include "CaramelDecorator.h"
  6.  
  7. class Beverage
  8. {
  9. public:
  10.  
  11. virtual std::string GetDescription() const = 0;
  12. virtual int GetCost() const = 0;
  13. };
  14.  
  15. class CondimentDecorator : public Beverage
  16. {
  17. public:
  18.  
  19. Beverage* beverage;
  20. CondimentDecorator(Beverage* beverage) : beverage(beverage) {}
  21. };
  22.  
  23. class Espresso : public Beverage
  24. {
  25. virtual std::string GetDescription() const override
  26. {
  27. return "Espresso";
  28. }
  29.  
  30. virtual int GetCost() const override
  31. {
  32. return 5;
  33. }
  34. };
  35.  
  36. class Decaf : public Beverage
  37. {
  38. virtual std::string GetDescription() const override
  39. {
  40. return "Decaf";
  41. }
  42.  
  43. virtual int GetCost() const override
  44. {
  45. return 4;
  46. }
  47. };
  48.  
  49. class CaramelDecorator : public CondimentDecorator
  50. {
  51. public:
  52.  
  53. CaramelDecorator(Beverage* beverage) : CondimentDecorator(beverage) {}
  54.  
  55. virtual std::string GetDescription() const override
  56. {
  57. return this->beverage->GetDescription() + " with Caramel";
  58. }
  59.  
  60. virtual int GetCost() const override
  61. {
  62. return this->beverage->GetCost() + 2;
  63. }
  64. };
  65.  
  66. class SoyDecorator : public CondimentDecorator
  67. {
  68. public:
  69.  
  70. SoyDecorator(Beverage* beverage) : CondimentDecorator(beverage) {}
  71.  
  72. virtual std::string GetDescription() const override
  73. {
  74. return this->beverage->GetDescription() + " with Soy";
  75. }
  76.  
  77. virtual int GetCost() const override
  78. {
  79. return this->beverage->GetCost() + 1;
  80. }
  81. };
  82.  
  83. int main()
  84. {
  85. Decaf* d = new Decaf;
  86. SoyDecorator* s = new SoyDecorator(d);
  87. CaramelDecorator* c = new CaramelDecorator(s);
  88. CaramelDecorator* cc = new CaramelDecorator(c);
  89.  
  90. std::cout << cc->GetDescription() << std::endl;
  91. std::cout << cc->GetCost() << std::endl;
  92. }
Success #stdin #stdout 0s 4544KB
stdin
Standard input is empty
stdout
Decaf with Soy with Caramel with Caramel
9