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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.