How it works...

If you have ever read a book on C++, you have likely seen the apples and oranges example, which demonstrates how object-oriented programming works. The idea goes as follows:

  • An apple is a fruit.
  • An orange is a fruit.
  • An apple is not an orange but both are fruit.

This example is meant to teach how to organize your code into logical objects using inheritance. A logic that is shared by both an apple and an orange is written into an object called fruit while logic that is specific to an apple or an orange is written into the apple or orange objects that inherit from the base fruit object.

This example is also, however, showing how to extend the functionality of a fruit. By subclassing a fruit, I can create an apple that is capable ...

Get Advanced C++ Programming Cookbook 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.