Chapter 10. Polymorphism

By the end of the previous chapter, we had created a DatedStockItem class by inheritance from the StockItem class, adding an expiration date field. This was a solution to the problem of creating a new class based on the existing StockItem class without rewriting all of the already functioning code in that class. Unfortunately, however, it didn't allow us to mix objects of the original StockItem class in the same Vec with those of the new DatedStockItem class and still have the correct Reorder function called for the derived class object. So far in this book, we've seen how and why we use the first two major organizing principles of object-oriented programming: encapsulation and inheritance. Now our requirement of mixing ...

Get C++: A Dialog Programming with the C++ Standard Library 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.