Polymorphism is another key concept in object-oriented programming. It allows subclasses to have their own implementation for the functions that are derived from the base class. Let's suppose we have the Musician class, which has the play() member function:
class Musician {
public:
void play() { std::cout << "Play an instrument"; }
};
Now, let's declare the Guitarist class, which has the play_guitar() function:
class Guitarist {
public:
void play_guitar() { std::cout << "Play a guitar"; }
};
This is the obvious case of using inheritance because the Guitarist just screams that it is-a Musician. It would be natural for the Guitarist to not extend the Musician by adding a new function (such as play_guitar()); instead, it should provide its own implementation of the play() function derived from the Musician. To accomplish this,...