Introduction to Inheritance

In the previous chapter, we covered object composition, where sophisticated classes are assembled from simpler classes and types. Object composition is ideal for creating objects with "has-a" relationships. However, composition represents only one of two primary techniques C++ offers for constructing complex classes. The second technique is inheritance, which models an "is-a" relationship between entities.

While object composition creates new objects by combining and connecting existing objects, inheritance creates new objects by directly adopting the characteristics and capabilities of existing objects and then customizing or extending them. Like composition, inheritance appears everywhere in our world. When you were born, you inherited genetic traits from your parents and gained physical characteristics from both of them -- yet you developed your own unique personality. Technology products (smartphones, tablets, computers) inherit features from their predecessors (often to maintain compatibility). For instance, the AMD Ryzen processor inherited many capabilities from earlier AMD processors, which themselves inherited features from even older designs. C++ inherited numerous features from C, and C inherited capabilities from programming languages that preceded it.

Consider guitars and pianos. Although guitars and pianos are different instruments, they both share something fundamental: they are musical instruments. Because guitars and pianos are instruments, logic tells us that anything true about instruments applies to both guitars and pianos. For example, all instruments produce sound, have a pitch range, and require tuning. Therefore, guitars and pianos also produce sound, have a pitch range, and require tuning. We can say that guitars and pianos inherit (acquire) all properties of instruments because they are instruments. We also understand that instruments undergo a maintenance process to keep them playable. Since guitars and pianos are instruments, they inherit this maintenance behavior as well.

Visualized as a diagram, the relationship between guitars, pianos, and instruments might look like this:

This diagram illustrates a hierarchy.

Hierarchies

A hierarchy is a diagram depicting how various entities relate to one another. Most hierarchies either display a progression through time (8086 -> 80286 -> 80386) or organize items from broad to narrow (instrument -> guitar -> acoustic guitar). If you've studied biology, you're familiar with the classic taxonomy: domain, kingdom, phylum, class, order, family, genus, and species (from general to specific).

Here's another hierarchy example: an electric car is a car, which is a vehicle, which is a mode of transportation. A bicycle is also a vehicle, which is also a mode of transportation. Organized as a hierarchy diagram, this would progress from general (top) to specific (bottom), with each level inheriting the properties and behaviors from the level above it.

A look ahead

In this chapter, we'll examine the fundamentals of how inheritance operates in C++.

In the following chapter, we'll discover how inheritance enables polymorphism (a key concept in object-oriented programming) using virtual functions.

As we advance, we'll also discuss inheritance's primary advantages, along with some potential drawbacks.

Summary

Inheritance is one of two primary techniques for constructing complex classes in C++, modeling "is-a" relationships between entities. While object composition combines existing objects, inheritance creates new objects by directly adopting characteristics and capabilities from existing objects, then customizing or extending them.

Real-world examples of inheritance include guitars and pianos (both are instruments, inheriting properties like producing sound and requiring tuning), technology products inheriting features from predecessors, and programming languages inheriting capabilities from earlier languages. These relationships demonstrate how specific entities inherit all properties and behaviors from more general categories.

Hierarchies organize entities from general to specific, showing relationships through diagrams that progress from broad categories at the top to narrow specializations at the bottom. These hierarchies can display progressions through time or taxonomic organization from general to specific classifications.

The following lessons will explore how inheritance functions in C++, how it enables polymorphism through virtual functions, and both the advantages and potential drawbacks of using inheritance in your programs.