fork download
  1. #include <iostream>
  2. #include <stdexcept>
  3.  
  4. class Bird {
  5. public:
  6. virtual void fly() {
  7. std::cout << "Flying" << std::endl;
  8. }
  9. };
  10.  
  11. class Sparrow : public Bird {
  12. public:
  13. void fly() override {
  14. std::cout << "Sparrow is flying" << std::endl;
  15. }
  16. };
  17.  
  18. class Ostrich {
  19. public:
  20. void walk() {
  21. std::cout << "Ostrich is walking" << std::endl;
  22. }
  23. };
  24.  
  25. int main() {
  26. Bird* bird = new Sparrow();
  27. bird->fly(); // Correct behavior
  28.  
  29. // Replacing with Ostrich will cause issues as Ostrich can't fly
  30. // Bird* ostrich = new Ostrich();
  31.  
  32. delete bird;
  33. return 0;
  34. }
  35.  
  36.  
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
Sparrow is flying