Search

Meta

11.5 — Inheritance and access specifiers

In the previous lessons on inheritance, we’ve been making all of our data members public in order to simplify the examples. In this section, we’ll talk about the role of access specifiers in the inheritance process, as well as cover the different types of inheritance possible in C++.

To this point, you’ve seen the private and public access specifiers, which determine who can access the members of a class. As a quick refresher, public members can be accessed by anybody. Private members can only be accessed by member functions of the same class. Note that this means derived classes can not access private members!

When dealing with inherited classes, things get a bit more complex.

First, there is a third access specifier that we have yet to talk about because it’s only useful in an inheritance context. The protected access specifier restricts access to member functions of the same class, or those of derived classes.

Second, when a derived class inherits from a base class, the access specifiers may change depending on the method of inheritance. There are three different ways for classes to inherit from other classes: public, private, and protected.

To do so, simply specify which type of access you want when choosing the class to inherit from:

If you do not choose an inheritance type, C++ defaults to private inheritance (just like members default to private access if you do not specify otherwise).

That gives us 9 combinations: 3 member access specifiers (public, private, and protected), and 3 inheritance types (public, private, and protected).

The rest of this section will be devoted to explaining the difference between these.

Before we get started, the following should be kept in mind as we step through the examples. There are three ways that members can be accessed:

  • A class can always access it’s own members regardless of access specifier.
  • The public accesses the members of a class based on the access specifiers of that class.
  • A derived class accesses inherited members based on the access specifiers of its immediate parent. A derived class can always access it’s own members regardless of access specifier.

This may be a little confusing at first, but hopefully will become clearer as we step through the examples.

Public inheritance

Public inheritance is by far the most commonly used type of inheritance. In fact, very rarely will you use the other types of inheritance, so your primary focus should be on understanding this section. Fortunately, public inheritance is also the easiest to understand. When you inherit a base class publicly, all members keep their original access specifications. Private members stay private, protected members stay protected, and public members stay public.

This is fairly straightforward. The things worth noting are:

  1. Derived classes can not directly access private members of the base class.
  2. The protected access specifier allows derived classes to directly access members of the base class while not exposing those members to the public.
  3. The derived class uses access specifiers from the base class.
  4. The outside uses access specifiers from the derived class.

To summarize in table form:

Public inheritance
Base access specifier Derived access specifier Derived class access? Public access?
Public Public Yes Yes
Private Private No No
Protected Protected Yes No

Private inheritance

With private inheritance, all members from the base class are inherited as private. This means private members stay private, and protected and public members become private.

Note that this does not affect that way that the derived class accesses members inherited from its parent! It only affects the code trying to access those members through the derived class.

To summarize in table form:

Private inheritance
Base access specifier Derived access specifier Derived class access? Public access?
Public Private Yes No
Private Private No No
Protected Private Yes No

Protected inheritance

Protected inheritance is the last method of inheritance. It is almost never used, except in very particular cases. With protected inheritance, the public and protected members become protected, and private members stay private.

To summarize in table form:

Protected inheritance
Base access specifier Derived access specifier Derived class access? Public access?
Public Protected Yes No
Private Private No No
Protected Protected Yes No

Protected inheritance is similar to private inheritance. However, classes derived from the derived class still have access to the public and protected members directly. The public (stuff outside the class) does not.

Summary

The way that the access specifiers, inheritance types, and derived classes interact causes a lot of confusion. To try and clarify things as much as possible:

First, the base class sets it’s access specifiers. The base class can always access it’s own members. The access specifiers only affect whether outsiders and derived classes can access those members.

Second, derived classes have access to base class members based on the access specifiers of the immediate parent. The way a derived class accesses inherited members is not affected by the inheritance method used!

Finally, derived classes can change the access type of inherited members based on the inheritance method used. This does not affect the derived classes own members, which have their own access specifiers. It only affects whether outsiders and classes derived from the derived class can access those inherited members.

A final example:

Base can access it’s own members without restriction. The public can only access m_nPublic. Derived classes can access m_nPublic and m_nProtected.

D2 can access it’s own members without restriction. D2 can access Base’s members based on Base’s access specifiers. Thus, it can access m_nPublic and m_nProtected, but not m_nPrivate. Because D2 inherited Base privately, m_nPublic, m_nPrivate, and m_nProtected are now private when accessed through D2. This means the public can not access any of these variables when using a D2 object, nor can any classes derived from D2.

D3 can access it’s own members without restriction. D3 can access D2’s members based on D2’s access specifiers. Thus, D3 has access to m_nPublic2 and m_nProtected2, but not m_nPrivate2. D3 access to Base members is controlled by the access specifier of it’s immediate parent. This means D3 does not have access to any of Base’s members because they all became private when D2 inherited them.

11.6 -- Adding, changing, and hiding members in a derived class
Index
11.4 -- Constructors and initialization of derived classes

39 comments to 11.5 — Inheritance and access specifiers

  • Cornel

  • Priya

    very comprehensive tutorial.. kudos to you!!

  • Amazing Tutorials. No more words.

    I have been working as Software engineer of Real Time Embedded for 7 years and I discovered that there is many things I never knew.

    Thank you
    Wael

  • Sean

    Very clear, thanks a lot!

  • Ben

    In the first example under the title “Public inheritance” the Pub constructor should be public. Apart from that, great tutorial (judging from what i’ve seen until now).
    Ben

    • jason

      Good catch, the code will not compile because the examples clearly state “not ok …”, which in case will give compiler errors. If those examples are commented out, it is true, you will still get an error since the Pub default constructor is private by default.

  • Hi Alex,

    I think the following table may help to clarify the access mechanisms for the various cases.
    By sub_derived I mean a class derived from a class already derived from a base class. The subtly
    of the difference between protected and private inheritance only comes into play at that level.
    The table assumes your three base class members with differing access levels.

    The inheritance access is applied when the derived class is created from the base class. The
    inheritance of the sub-derived class from the derived class is public.
    So we have:

    class Base
    {
    public:
    int m_nPublic;
    private:
    int m_nPrivate;
    protected:
    int m_nProtected;
    }
    class derived : xxx Base // xxx is public, protected or private
    {
    }
    class sub_derived : public derived
    {
    }

    xxx is public
    derived sub_derived public
    m_nPrivate3 No No No
    m_nProtected3 Yes Yes No
    m_nPublic3 Yes Yes Yes

    xxx is protected
    derived sub_derived public
    m_nPrivate3 No No No
    m_nProtected3 Yes Yes No
    m_nPublic3 Yes Yes No

    xxx is private
    derived sub_derived public
    m_nPrivate3 No No No
    m_nProtected3 Yes No No
    m_nPublic3 Yes No No

    Oh dear, the tags didn’t work and it’s squashed my tables! Grrr.

    • Sorry it squashed your table. As the note says just below the comment box:

      Other tags get stripped out, including tables. This is done for the protection of the readers and the integrity of the site -- malformed tables can easily make all comments that follow completely unreadable.

    • jason

      To bad the tables didn’t work. Thanks for the added summary. A very nice compliment to the tutorial.

  • Kinten

    so, private and protected inheritance only differ in classes that are derived more than one time (multiple inheritance), it’s that right?

    • I suppose you could look at it that way. Really the derived class does differ, since members will have different access specifiers. However, as you imply, since private and protected have the same access restrictions within a class, there’s no practical difference until you derive the class again.

      Just so you know, the term multiple inheritance should not be used when referring to a class that has been derived multiple times. It has a different meaning: a class that is derived from multiple parents simultaneously. We cover multiple inheritance in a few more lessons.

  • SureshMP

    It’s very useful doc for those who are beginner in c++.

  • Purushothman

    Excellent article…

    Thanks,
    Purushothman J

  • jt

    excellent tutorial…

  • jeremy

  • Hayyan

    Very informative… some stuff i didnt know…thnaks a lot

  • very good example
    here u willget complete view of the exact inheritance is

  • Hazel

    very nice example

  • I have neve seen such a clear definitions and examples before. Thanks Alex!

  • Really amazing tutorial… I got many-things new here…
    thank you very much…..

  • Shalinee Singh

    Very good explanation done. Thanks.

  • Sandeep Aggarwal

    Thanx a tonne..Beautiful Tutorial..Finally understood what all class specifiers are after 3 yeras of graduation??

  • migmar dolma

    thanks it made the topic more clear
    migmar dolma

  • Vikas

    Excellent.. amazing simplicity in way of teaching… kudos to you…

  • Renuka S

  • ozzy

    Very nice explanation !

  • auasp

    Is the first table on Public inheritance correct?
    I think all the entries in “derived access specifier” column should be Public.

  • Rollie

    Great explanation -- thanks!

  • shruthi

    Thanks!. Good explanation of the topic.

  • Kevin

    Excellent tutorial.
    The best I’ve seen so far.
    Helped me a lot.
    Thank you!

  • ROHITH

    ITS SUPERB AND THANKS A LOT. IT GIVES A OVER ALL VIEW OF THE ACCESS SPECIFIERS STORY.

    THANKS.

  • Peter Bulyaki

    Very nice and thorough tutorial, however I found one very misleading point:
    For public inheritance, your table says private will become private in the derived class (and you introduce an extra column to say that it is not accessible in the derved class explicitly). This is anything but true, because private will become *inaccessible* in the derived class -- not private. Beginners who would take the easy way of learning, would memorize your tables, and they would easily get confused by it.

    What you say is:
    Public -> Public
    Protected -> Protected
    Private -> Private

    This is how I would change your table, putting a “-” to the appropriate cell:
    public -> public
    protected -> protected
    private -> --

    Same applies to protected inheritance:
    public -> protected
    protected -> protected
    private -> --

    And for private inheritance:
    public -> private
    protected -> private
    private -> --

    So in my interpretation, there is an even easier way to memorize all 3*3 cases:
    First, you’d need to point out, that private is private. No matter how you inherit, a private member will NOT be accessible in the derived class.

    So here it is how I’d let people memorize it:
    0. Inheritance specifier is uninterpretable for private members.

    1. PUBLIC inheritance: Everything remains the SAME in the derived class:
    public -> public
    protected -> protected

    2. PROTECTED inheritance: Everything becomes PROTECTED in the derived class:
    public -> protected
    protected -> protected

    3. PRIVATE inheritance: Everything becomes PRIVATE in the derived class:
    public -> private
    protected -> private

    This is how I was taught it, and I think that it is much simpler this way.

    • Nikhil Singhal

      Thanks Peter

      your comment makes Alex’s article more understandable, specially for Beginners, most of the times they become confused at this point, but if they follow your table they never confused LIKE ME :)

      Thanks & Regards
      Nikhil Singhal

    • singhchauhan.nitin

      Peter you made it more clear to understand the hierarchy of inheritance .. thanks a lot..

  • qaiser abbas

    You have two classes, Class A and Class B. Being a programmer either you can implement Class B as nested class of Class A or you can inherit Class B from Class A.

    These are two ways to describe the relationship between classes. Discuses the situations in which, concept of public inheritance and nested class is more appropriate to use. Justify with solid reasons.

    please provide the answer

  • Matheus

    Thanks for the last example. It was simple concise, and cleared all my doubts about the subject. Keep up with updating your tutorial!! ^^

  • LW

    typo in private inheritance:
            // The derived class always uses the immediate parent’s class access specifications
            // Thus, Pub uses Base’s access specifiers

    Here the "Pub uses" should be "Pri uses".

Leave a Comment

  

  

  

1 + 1 =

Put C++ code inside [code][/code] tags to use the syntax highlighter