O'Reilly logo

Sams Teach Yourself C++ in 24 Hours, fifth edition by Rogers Cadenhead, 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

Hour 18. Making Use of Advanced Polymorphism

Problems with Single Inheritance

In the previous hours, we discussed treating derived objects polymorphically with their base classes. You saw that if the base class has a member function speak() that is overridden in the derived class, a pointer to a base object that is assigned to a derived object will do the right thing. The Mammal12 program in Listing 18.1 illustrates this idea.

Listing 18.1 The Full Text of Mammal12.cpp

 1: #include <iostream> 2: 3: class Mammal 4: { 5: public: 6:     Mammal():age(1) { std::cout << "Mammal constructor ...\n"; ...

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