avr39ripe

cppInterfacesMultInheritExample

Aug 17th, 2021
793
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 fly... ";
  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 parrot!\n"; }
  30. };
  31.  
  32.  
  33. class Frog : public IJumpable
  34. {
  35. public:
  36.     void jump() override { std::cout << "Froggy jump-jump Kwaaa!!\n";}
  37. };
  38.  
  39.  
  40. class Bird : public IFlyable
  41. {
  42. public:
  43.     void fly() override { std::cout << "Eine kleine Nachtigal! Zer Gut! :)\n"; }
  44. };
  45.  
  46.  
  47. class BaseA
  48. {
  49. public:
  50.     int valA;
  51. public:
  52.     BaseA() : valA { 1 } { std::cout << "BaseA DEFAULT ctor\n"; }
  53.     BaseA(int valAP) : valA{valAP} { std::cout << "BaseA ctor valA\n"; }
  54.     ~BaseA() { std::cout << "BaseA dtor\n"; }
  55.     void info() { std::cout << "I'm BaseA\n"; }
  56. };
  57.  
  58.  
  59. class Child : public BaseA
  60. {
  61. protected:
  62.     int childVal;
  63. public:
  64.     Child() : BaseA{111}, childVal{ 3 } { std::cout << "Child DEFAULT ctor\n"; }
  65.     Child(int childValP) : childVal{ childValP } { std::cout << "ChildA ctor childValP\n"; }
  66.     ~Child() { std::cout << "Child dtor\n"; }
  67.     void info() { BaseA::info(); std::cout << "I'm Child\n"; }
  68. };
  69.  
  70.  
  71. class Animal
  72. {
  73.     std::string name;
  74. public:
  75.     Animal(std::string nameP) : name{ nameP } {}
  76.     void sayHello()const { sound(); std::cout << " Hello, I'm " << name << '\n'; }
  77.     virtual void sound()const = 0;/*{ std::cout << "??????"; }*/
  78. };
  79.  
  80. //void Animal::sound()const
  81. //{
  82. //    std::cout << "White noise....\n";
  83. //}
  84.  
  85. class Cat : public Animal
  86. {
  87. public:
  88.     Cat(std::string nameP) : Animal{ nameP } {}
  89.     virtual void sound()const override { /*Animal::sound();*/  std::cout << "Meow!"; }
  90. };
  91.  
  92. class Dog : public Animal
  93. {
  94. public:
  95.     Dog(std::string nameP) : Animal{ nameP } {}
  96.     virtual void sound()const override{ std::cout << "RRwaFF!"; }
  97. };
  98.  
  99. class Parrot : public Animal
  100. {
  101. public:
  102.     Parrot(std::string nameP) : Animal{ nameP } {}
  103.     virtual void sound()const override{ std::cout << "KrrrrAA!"; }
  104. };
  105.  
  106. void introduce(const Animal& animal)
  107. {
  108.     animal.sayHello();
  109. }
  110.  
  111. //void introduce(const Cat& animal)
  112. //{
  113. //    animal.sound(); std::cout << " Hello, I'm " << animal.name << '\n';
  114. //}
  115. //
  116. //void introduce(const Dog& animal)
  117. //{
  118. //    animal.sound(); std::cout << " Hello, I'm " << animal.name << '\n';
  119. //}
  120. //
  121. //void introduce(const Parrot& animal)
  122. //{
  123. //    animal.sound(); std::cout << " Hello, I'm " << animal.name << '\n';
  124. //}
  125.  
  126. int main()
  127. {
  128.     JumpingParrot jumpParrot;
  129.     IFlyable* flyers[]{ new Bird, &jumpParrot };
  130.     IJumpable* jumpers[]{ &jumpParrot, new Frog };
  131.  
  132.     for (const auto& flyer : flyers) { flyer->fly(); }
  133.     for (const auto& jumper : jumpers) { jumper->jump(); }
  134.  
  135.    // Animal* animals[]{ new Cat{ "Murzik" }, new Dog {"Rex"}, new Parrot { "Kesha" } };
  136.  
  137.     /*Cat cat{ "Murzik" };
  138.     Dog dog{"Rex"};
  139.     Parrot parrot{ "Kesha" };*/
  140.  
  141.  
  142.     //cat.sound(); std::cout << '\n';
  143.     //dog.sound(); std::cout << '\n';
  144.     //parrot.sound(); std::cout << '\n';
  145.  
  146.     /*introduce(cat);
  147.     introduce(dog);
  148.     introduce(parrot);*/
  149.     //Animal an;
  150.  
  151.  
  152.     //for (const auto& animal : animals) { animal->sayHello(); }
  153. }
  154.  
RAW Paste Data