Extending functionality

Extending functionality with function overloading

Create a base class Bird with a method fly() that outputs "This bird flies." Create a derived class Eagle that overrides the fly() method to first call the base class’s fly() method and then adds "The eagle soars high in the sky."

Implement both classes and create an object of Eagle. Call the fly() method to demonstrate extending the base functionality.

Hint

Answer

#include <iostream>

class Bird {
public:
    void fly() {
        std::cout << "This bird flies." << std::endl;
    }
};

class Eagle : public Bird {
public:
    void fly() override {
        Bird::fly(); // Call the base class method
        std::cout << "The eagle soars high in the sky." << std::endl;
    }
};

int main() {
    Eagle myEagle;
    myEagle.fly();  // Output will be "This bird flies." followed by "The eagle soars high in the sky."
  
    return 0;
}
Questions