Chapter 8

Discovering Inheritance Techniques

WHAT’S IN THIS CHAPTER?

  • How to extend a class through inheritance
  • How to employ inheritance to reuse code
  • How to build interactions between superclasses and subclasses
  • How to use inheritance to achieve polymorphism
  • How to work with multiple inheritance
  • How to deal with unusual problems in inheritance

Without inheritance, classes would simply be data structures with associated behaviors. That alone would be a powerful improvement over procedural languages, but inheritance adds an entirely new dimension. Through inheritance, you can build new classes based on existing ones. In this way, your classes become reusable and extensible components. This chapter will teach you the different ways to leverage the power of inheritance. You will learn about the specific syntax of inheritance as well as sophisticated techniques for making the most of inheritance.

The portion of this chapter relating to polymorphism draws heavily on the spreadsheet example discussed in Chapters 6 and 7. If you have not read Chapters 6 and 7, you may wish to skim the sample code in those chapters to get a background on this example. This chapter also refers to the object-oriented methodologies described in Chapter 3. If you have not read that chapter and are unfamiliar with the theories behind inheritance, you should review Chapter 3 before continuing.

BUILDING CLASSES WITH INHERITANCE

In Chapter 3, you learned that an “is-a” relationship recognizes the pattern that ...

Get Professional C++, 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.