18. Polymorphism: Object Independence

If you’ve made it this far, congratulations! You’re about to scale the highest peak of all in the mountain range of object-oriented concepts: polymorphism. It is the key to making objects truly independent.

Remember that objects—which are instances of classes—are data items that have built-in behavior. More than anything else, this ability of an unknown object to bring in new kinds of behavior—essentially plugging new software into an existing framework—makes good on the promise of object orientation.

A Different Approach to the FloatFraction Class

Virtual functions are at the core of polymorphism. To understand why they are useful, let’s look at an example building on the Fraction class.

The FloatFraction ...

Get C++ Without Fear: A Beginner’s Guide That Makes You Feel Smart, Second Edition 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.