avr39ripe

cppInterfacesViaMultipleInheritance

Aug 12th, 2021
804
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. class IJumpable
  4. {
  5. public:
  6.     virtual void jump() = 0;
  7. };
  8.  
  9. void IJumpable::jump()
  10. {
  11.     std::cout << "Jumper bumper ";
  12. }
  13.  
  14. class IFlyable
  15. {
  16. public:
  17.     virtual void fly() = 0;
  18. };
  19.  
  20. void IFlyable::fly()
  21. {
  22.     std::cout << "I belive I can Flyyy ";
  23. }
  24.  
  25. class JumpingParrot : public IJumpable, public IFlyable
  26. {
  27. public:
  28.     void jump() override { std::cout << "Jumping parrot jump-jump...\n"; }
  29.     void fly() override { IFlyable::fly(); std::cout << " I'm a jumping Parrrot!\n"; }
  30. };
  31.  
  32. class Frog : public IJumpable
  33. {
  34. public:
  35.     void jump() override { std::cout << "Froggy jump-jump! Kwaaaa!\n"; }
  36. };
  37.  
  38. class Bird : public IFlyable
  39. {
  40. public:
  41.     void fly() override { std::cout << "Ein kleine Nachtigal! Zehr gut!\n"; }
  42. };
  43.  
  44. int main()
  45. {
  46.     IFlyable* flyers[]{ new Bird{}, new JumpingParrot{} };
  47.     IJumpable* jumpers[]{ new JumpingParrot{}, new Frog{} };
  48.  
  49.     for (const auto& flyer : flyers) { flyer->fly(); }
  50.     for (const auto& jumper : jumpers) { jumper->jump(); }
  51.  
  52.     return 0;
  53. }
  54.  
RAW Paste Data