fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Menu {
  5. private:
  6. int price;
  7. public:
  8. Menu(int p=0) : price{p} {}
  9. virtual string description() = 0;
  10. virtual int getPrice() {
  11. return price;
  12. }
  13. virtual ~Menu() {}
  14. };
  15. class WithLemon : public Menu {
  16. private:
  17. Menu* meniu;
  18.  
  19. public:
  20. WithLemon() = default;
  21. WithLemon(Menu* n) :
  22. meniu{ n } {}
  23. string description() override {
  24. return meniu->description() + " with lemon ";
  25. }
  26. };
  27.  
  28. class WithCoffee : public Menu {
  29. private:
  30. Menu* meniu;
  31. public:
  32. WithCoffee(Menu* n) :
  33. meniu{ n } {
  34. }
  35. string description() override{
  36. return meniu->description() + " with coffee ";
  37. }
  38. int getPrice() override{
  39. return meniu->getPrice()+5;
  40. }
  41. };
  42.  
  43. class Breakfast : public Menu {
  44. private:
  45. string name;
  46. public:
  47. Breakfast(string n, int p) :
  48. name{ n }, Menu{ p} {
  49. }
  50. string description() override {
  51. return name;
  52. }
  53. };
  54.  
  55. int main() {
  56. Breakfast a{"eggs", 10};
  57. WithCoffee breakfast_with_coffee { &a };
  58. cout << breakfast_with_coffee.description() << " " << breakfast_with_coffee.getPrice() << endl;
  59. return 0;
  60. }
Success #stdin #stdout 0s 4384KB
stdin
Standard input is empty
stdout
eggs with coffee  15