fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3.  
  4. class RestaurantEmployee {
  5. public:
  6. virtual void washDishes() {
  7. std::cout << "Washing dishes...\n";
  8. }
  9.  
  10. virtual void serveFood() {
  11. std::cout << "Serving food...\n";
  12. }
  13.  
  14. virtual void cookFood() {
  15. std::cout << "Cooking food...\n";
  16. }
  17.  
  18. virtual ~RestaurantEmployee() = default;
  19. };
  20.  
  21. // Dishwasher overrides unnecessary methods
  22. class Dishwasher : public RestaurantEmployee {
  23. public:
  24. void washDishes() override {
  25. std::cout << "Dishwasher: Washing dishes...\n";
  26. }
  27.  
  28. void serveFood() override {} // Not needed, but must be implemented
  29. void cookFood() override {} // Not needed, but must be implemented
  30. };
  31.  
  32. // Waiter overrides unnecessary methods
  33. class Waiter : public RestaurantEmployee {
  34. public:
  35. void serveFood() override {
  36. std::cout << "Waiter: Serving food...\n";
  37. }
  38.  
  39. void washDishes() override {} // Not needed, but must be implemented
  40. void cookFood() override {} // Not needed, but must be implemented
  41. };
  42.  
  43. // Chef overrides unnecessary methods
  44. class Chef : public RestaurantEmployee {
  45. public:
  46. void cookFood() override {
  47. std::cout << "Chef: Cooking food...\n";
  48. }
  49.  
  50. void washDishes() override {} // Not needed, but must be implemented
  51. void serveFood() override {} // Not needed, but must be implemented
  52. };
  53.  
  54. //above is not following interface segregation principles because wash dishes anf cook food is meaning less
  55. //for waiter so we don't need to implement them
  56.  
  57.  
  58. //Now lets make it follow the ISP
  59.  
  60. // Separate interfaces for specific tasks
  61. class Dishwashing {
  62. public:
  63. virtual void washDishes() const = 0;
  64. virtual ~Dishwashing() = default;
  65. };
  66.  
  67. class FoodServing {
  68. public:
  69. virtual void serveFood() const = 0;
  70. virtual ~FoodServing() = default;
  71. };
  72.  
  73. class Cooking {
  74. public:
  75. virtual void cookFood() const = 0;
  76. virtual ~Cooking() = default;
  77. };
  78.  
  79. // Dishwasher implements only Dishwashing
  80. class Dishwasher : public Dishwashing {
  81. public:
  82. void washDishes() const override {
  83. std::cout << "Dishwasher: Washing dishes...\n";
  84. }
  85. };
  86.  
  87. // Waiter implements only FoodServing
  88. class Waiter : public FoodServing {
  89. public:
  90. void serveFood() const override {
  91. std::cout << "Waiter: Serving food...\n";
  92. }
  93. };
  94.  
  95. // Chef implements only Cooking
  96. class Chef : public Cooking {
  97. public:
  98. void cookFood() const override {
  99. std::cout << "Chef: Cooking food...\n";
  100. }
  101. };
  102.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:80:7: error: redefinition of ‘class Dishwasher’
 class Dishwasher : public Dishwashing {
       ^~~~~~~~~~
prog.cpp:22:7: note: previous definition of ‘class Dishwasher’
 class Dishwasher : public RestaurantEmployee {
       ^~~~~~~~~~
prog.cpp:88:7: error: redefinition of ‘class Waiter’
 class Waiter : public FoodServing {
       ^~~~~~
prog.cpp:33:7: note: previous definition of ‘class Waiter’
 class Waiter : public RestaurantEmployee {
       ^~~~~~
prog.cpp:96:7: error: redefinition of ‘class Chef’
 class Chef : public Cooking {
       ^~~~
prog.cpp:44:7: note: previous definition of ‘class Chef’
 class Chef : public RestaurantEmployee {
       ^~~~
stdout
Standard output is empty