CS 105 (C++)

Discussion 10
C++: Chapter 5, 6: composition/derivation, constructors/destructors

public vs private inheritance

When deriving one class from another, you can specify an access level of public, protected, or private.  To keep it (relatively) simple, we'll only consider public and private derivations.  (And note that public is by far the most common choice.)

So how do these derivation access levels work?  This can seem overwhelming if you consider that accessiblity may vary along multiple dimensions:
Luckily, the results can be fully specified with just two rules.
  1. private members of the base class: To preserve encapsulation, these are always inaccessible to everyone but the base class itself.  (Neither the derived class nor users of the derived class can access these members, and this is true for both public and private derivation.)
  2. public members of the base class: These behave exactly as either public or private members of the derived class, depending on whether the derivation is public or private.
In this course (and probably in most of the C++ programming you ever do), you'll only have to deal with public derivation, so let's make sure that the results are at least clear in that case.  Answer the following questions assuming that class Base exists, and that class Derived is publicly derived from Base.

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.