fork download
  1.  
  2. #include "iostream"
  3. #include "string"
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. std::cout << "Object Oriented Pattern:\n\n";
  10.  
  11. MallardDuck Duck1;
  12. Duck1.fly();
  13. Duck1.quack();
  14.  
  15. RubberDuck Duck2;
  16. Duck2.fly();
  17. Duck2.quack();
  18.  
  19. std::cin.get();
  20. return 0;
  21. };
  22.  
  23. class IFlyBehavior
  24. {
  25. public:
  26. virtual void fly();
  27. };
  28.  
  29. class IQuackBehavior
  30. {
  31. public:
  32. virtual void quack();
  33. };
  34.  
  35. class Duck
  36. {
  37. protected:
  38. IFlyBehavior FlyBehavior;
  39. IQuackBehavior QuackBehavior;
  40.  
  41. public:
  42. void fly()
  43. {
  44. FlyBehavior.fly();
  45. }
  46. void quack()
  47. {
  48. QuackBehavior.quack();
  49. }
  50. };
  51.  
  52. class RubberDuck : Duck
  53. {
  54. public:
  55. RubberDuck() : Duck()
  56. {
  57. NoFly FlyBehavior;
  58. Squeak QuackBehavior;
  59. }
  60. };
  61.  
  62. class MallardDuck : Duck
  63. {
  64. public:
  65. MallardDuck() : Duck()
  66. {
  67. FlyWithWings FlyBehavior;
  68. Quack QuackBehavior;
  69. }
  70. ~MallardDuck() { };
  71. };
  72.  
  73. class FlyWithWings : IFlyBehavior
  74. {
  75. void fly()
  76. {
  77. cout << "Flapping wings.";
  78. }
  79. };
  80.  
  81. class NoFly : IFlyBehavior
  82. {
  83. void fly()
  84. {
  85. cout << "Can't fly.";
  86. }
  87. };
  88.  
  89. class Quack : IQuackBehavior
  90. {
  91. void quack()
  92. {
  93. cout << "Quack!";
  94. }
  95. };
  96.  
  97. class Squeak : IQuackBehavior
  98. {
  99. void quack()
  100. {
  101. cout << "Squeak!";
  102. }
  103. };
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:11:5: error: ‘MallardDuck’ was not declared in this scope
     MallardDuck Duck1;
     ^~~~~~~~~~~
prog.cpp:12:5: error: ‘Duck1’ was not declared in this scope
     Duck1.fly();
     ^~~~~
prog.cpp:15:5: error: ‘RubberDuck’ was not declared in this scope
     RubberDuck Duck2;
     ^~~~~~~~~~
prog.cpp:16:5: error: ‘Duck2’ was not declared in this scope
     Duck2.fly();
     ^~~~~
prog.cpp: In constructor ‘RubberDuck::RubberDuck()’:
prog.cpp:57:9: error: ‘NoFly’ was not declared in this scope
         NoFly FlyBehavior;
         ^~~~~
prog.cpp:58:9: error: ‘Squeak’ was not declared in this scope
         Squeak QuackBehavior;
         ^~~~~~
prog.cpp: In constructor ‘MallardDuck::MallardDuck()’:
prog.cpp:67:9: error: ‘FlyWithWings’ was not declared in this scope
         FlyWithWings FlyBehavior;
         ^~~~~~~~~~~~
prog.cpp:68:9: error: ‘Quack’ was not declared in this scope
         Quack QuackBehavior;
         ^~~~~
stdout
Standard output is empty