[19] Inheritance — basics
(Part of C++ FAQ Lite, Copyright © 1991-2000, 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 section ]


[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 section ]


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

[Recently added "derived class of" to the list of synonyms (on 7/00). Click here to go to the next FAQ in the "chain" of recent changes.]

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 section ]


[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 section ]


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

TopBottomPrevious sectionNext section ]


[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 section ]


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

[Recently renamed "subclass" to "derived class" (on 7/00). 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 consider making your base class's bits be private:, and use protected: inline access functions by which derived classes will access the private data in the base class. This way the private bits can change, but the derived class's code won't break unless you change the protected access functions.

TopBottomPrevious sectionNext section ]


E-Mail E-mail the author
C++ FAQ LiteTable of contentsSubject indexAbout the author©Download your own copy ]
Revised Jul 10, 2000