liveBook · Manning

Multiple inheritance

As explained in Object-Oriented Software Design in C++

Multiple inheritance is inheritance in which a class derives directly from more than one superclass.

Multiple inheritance lets a class derive directly from more than one superclass. It can combine a domain superclass with reusable behavior classes, but it may preserve hardcoded behavior choices and add coupling. section 7.3

Toy behaviors

A ToyCar can inherit from Toy, Roll, and Engine; ModelAirplane can inherit from Toy, Fly, and Engine; TrainSet can inherit from Toy, Roll, and ChooChoo. The subclass forwards play() and sound() explicitly with Roll::play(), Fly::play(), Engine::sound(), or ChooChoo::sound(). This shares behavior implementations, but each toy still hardcodes its choices. section 7.3

Use multiple inheritance when:

  • A class must retain an is-a relationship with a primary superclass while sharing independent behavior classes. section 7.3
  • An Adapter needs to implement a target interface while subclassing the adaptee. section 10.3

How Object-Oriented Software Design in C++ covers it

How deep it goes
Explains inheritance structure, explicit member qualification, duplication reduction, and hardcoded behavior limits before deferring to composition.
Examples
Uses ToyCar, ModelAirplane, and TrainSet classes with Roll, Fly, Engine, and ChooChoo behavior classes.
What it assumes
Assumes familiarity with classes, superclasses, inheritance, and is-a relationships while contrasting them with has-a relationships.
What you can do after
Evaluate when shared behavior inheritance reduces duplication and when composition better supports runtime flexibility and loose coupling.
How it connects
Connects inheritance, is-a relationships, has-a relationships, composition, and the Adapter Design Pattern.

Also explained in

Compare how 4 books explain multiple inheritance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.