O'Reilly logo

Sams Teach Yourself C++ in 24 Hours, Third Edition by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Polymorphism Implemented With Virtual Methods

The previous chapter emphasized the fact that a Dog object is a Mammal object. So far that has meant only that the Dog object has inherited the attributes (data) and capabilities (methods) of its base class. In C++, the is-a relationship runs deeper than that, however.

Polymorphism allows you to treat derived objects as if they were base objects. For example, suppose you create a number of specialized Mammal types: Dog, Cat, Horse, and so forth. All of these derive from Mammal, and Mammal has a number of methods which are factored out of the derived classes. One such method might be Speak(). All mammals can make noise.

You'd like to teach each of the derived types to specialize how they speak. A Dog ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required