Chapter 23. Understanding Polymorphism

<feature>
  • Learn to create abstract classes and define virtual functions

  • See how virtual functions work

  • Use pointers to classes in a hierarchy

  • Discover how to avoid the problem of class slicing

  • Realize the importance of using virtual destructors with polymorphism

</feature>

What Is Polymorphism?

When I was a kid, I used to enjoy playing the game Dungeons and Dragons, and I still play it sometimes with my son. (He likes it, too.) The game allows you to choose one of several types of characters, such as a warrior or a wizard, and take on that character’s role. In the game, I often choose to play the role of a wizard, working magic and casting spells to help fellow players as needed. One of my favorite spells is called ...

Get Practical C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.