5.2. A Tour of Object-Oriented Programming

Let’s implement a simple three-level class hierarchy to introduce the C++ language constructs and programming idioms that support object-oriented programming. We root our class hierarchy with the abstract LibMat base class. We derive a Book class from LibMat and in turn derive an AudioBook class from Book. We limit our interface to a single function, print(), together with a constructor and destructor. I’ve instrumented each member function to output its presence so that we can trace the behavior of the program.

By default, a member function is resolved statically at compile-time. To have a member function resolved dynamically during run-time, we preface its declaration with the virtual keyword. The ...

Get Essential 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.