[19] Inheritance — basics
(Part of C++ FAQ Lite, Copyright © 1991-2001, Marshall Cline, cline@parashift.com)


FAQs in section [19]:


[19.1] Is inheritance important to C++?

Yep.

Inheritance is what separates abstract data type (ADT) programming from OO programming.

TopBottomPrevious sectionNext sectionSearch the FAQ ]


[19.2] When would I use inheritance?

As a specification device.

Human beings abstract things on two dimensions: part-of and kind-of. A Ford Taurus is-a-kind-of-a Car, and a Ford Taurus has-a Engine, Tires, etc. The part-of hierarchy has been a part of software since the ADT style became relevant; inheritance adds "the other" major dimension of decomposition.

TopBottomPrevious sectionNext sectionSearch the FAQ ]


[19.3] How do you express inheritance in C++?

By the : public syntax:

 class Car : public Vehicle {
 public:
   
// ...
 };

We state the above relationship in several ways:

(Note: this FAQ has to do with public inheritance; private and protected inheritance are different.)

TopBottomPrevious sectionNext sectionSearch the FAQ ]


[19.4] Is it OK to convert a pointer from a derived class to its base class?

Yes.

An object of a derived class is a kind of the base class. Therefore the conversion from a derived class pointer to a base class pointer is perfectly safe, and happens all the time. For example, if I am pointing at a car, I am in fact pointing at a vehicle, so converting a Car* to a Vehicle* is perfectly safe and normal:

 void f(Vehicle* v);
 void g(Car* c) { f(c); }  
// Perfectly safe; no cast

(Note: this FAQ has to do with public inheritance; private and protected inheritance are different.)

TopBottomPrevious sectionNext sectionSearch the FAQ ]


[19.5] What's the difference between public, private, and protected?

TopBottomPrevious sectionNext sectionSearch the FAQ ]


[19.6] Why can't my derived class access private things from my base class?

To protect you from future changes to the base class.

Derived classes do not get access to private members of a base class. This effectively "seals off" the derived class from any changes made to the private members of the base class.

TopBottomPrevious sectionNext sectionSearch the FAQ ]


[19.7] How can I protect derived classes from breaking when I change the internal parts of the base class? UPDATED!

[Recently wordsmithing thanks to Stan Brown (in 8/01). Click here to go to the next FAQ in the "chain" of recent changes.]

A class has two distinct interfaces for two distinct sets of clients:

Unless you expect all your derived classes to be built by your own team, you should declare your base class's data members as private and use protected inline access functions by which derived classes will access the private data in the base class. This way the private data declarations can change, but the derived class's code won't break (unless you change the protected access functions).

TopBottomPrevious sectionNext sectionSearch the FAQ ]


E-Mail E-mail the author
C++ FAQ LiteTable of contentsSubject indexAbout the author©Download your own copy ]
Revised Aug 15, 2001