Chapter 28. Inheriting a Class
In This Chapter
Introducing inheritance
Implementing inheritance in C++
Reviewing an example program
Comparing HAS_A to IS_A
Inheritance occurs all around us every day. I am a human. I inherit certain properties from the class Human, such as my ability to converse intelligently (more or less) and my dependence on air, water, and carbohydrate-based nourishment like Twinkies. These latter properties are not unique to humans. The class Human inherits these from class Mammal (along with something about bearing live young), which inherited them from class Animal, and so on.
The capability to pass down properties is a powerful one. It enables you to describe things in an economical way. For example, if my son asks me, "What's a duck?" I might say, "It's a bird that floats and goes quack." Despite your first reaction, that answer actually conveys a significant amount of knowledge. My son knows what a bird is. He knows that birds have wings, that birds can fly (he doesn't know about ostriches yet), and that birds lay eggs. Now, he knows all those same things about a duck plus the facts that ducks can float and make a quacking sound. (This might be a good time to refer to Chapter 21 for a discussion about Microwave ovens and their relationship to ovens and kitchen appliances.)
Object-oriented languages express this relationship by allowing one class to inherit from another. Thus, in C++ the class Duck might well inherit from Bird, and that class might also inherit ...
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