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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access