Tuesday, January 18, 2011

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!

1class Base
2{
3private:
4    int m_nPrivate; // can only be accessed by Base member functions (not derived classes)
5public:
6    int m_nPublic; // can be accessed by anybody
7};
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.
01class Base
02{
03public:
04    int m_nPublic; // can be accessed by anybody
05private:
06    int m_nPrivate; // can only be accessed by Base member functions (but not derived classes)
07protected:
08    int m_nProtected; // can be accessed by Base member functions, or derived classes.
09};
10 
11class Derived: public Base
12{
13public:
14    Derived()
15    {
16        // Derived's access to Base members is not influenced by the type of inheritance used,
17        // so the following is always true:
18 
19        m_nPublic = 1; // allowed: can access public base members from derived class
20        m_nPrivate = 2; // not allowed: can not access private base members from derived class
21        m_nProtected = 3; // allowed: can access protected base members from derived class
22    }
23};
24 
25int main()
26{
27    Base cBase;
28    cBase.m_nPublic = 1; // allowed: can access public members from outside class
29    cBase.m_nPrivate = 2; // not allowed: can not access private members from outside class
30    cBase.m_nProtected = 3; // not allowed: can not access protected members from outside class
31}
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:
01// Inherit from Base publicly
02class Pub: public Base
03{
04};
05 
06// Inherit from Base privately
07class Pri: private Base
08{
09};
10 
11// Inherit from Base protectedly
12class Pro: protected Base
13{
14};
15 
16class Def: Base // Defaults to private inheritance
17{
18};
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.
01class Base
02{
03public:
04    int m_nPublic;
05private:
06    int m_nPrivate;
07protected:
08    int m_nProtected;
09};
10 
11class Pub: public Base
12{
13    // Public inheritance means:
14    // m_nPublic stays public
15    // m_nPrivate stays private
16    // m_nProtected stays protected
17 
18    Pub()
19    {
20        // The derived class always uses the immediate parent's class access specifications
21        // Thus, Pub uses Base's access specifiers
22        m_nPublic = 1; // okay: anybody can access public members
23        m_nPrivate = 2; // not okay: derived classes can't access private members in the base class!
24        m_nProtected = 3; // okay: derived classes can access protected members
25    }
26};
27 
28int main()
29{
30    // Outside access uses the access specifiers of the class being accessed.
31    // In this case, the access specifiers of cPub.  Because Pub has inherited publicly from Base,
32    // no access specifiers have been changed.
33    Pub cPub;
34    cPub.m_nPublic = 1; // okay: anybody can access public members
35    cPub.m_nPrivate = 2; // not okay: can not access private members from outside class
36    cPub.m_nProtected = 3; // not okay: can not access protected members from outside class
37}
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.
01class Base
02{
03public:
04    int m_nPublic;
05private:
06    int m_nPrivate;
07protected:
08    int m_nProtected;
09};
10 
11class Pri: private Base
12{
13    // Private inheritance means:
14    // m_nPublic becomes private
15    // m_nPrivate stays private
16    // m_nProtected becomes private
17 
18    Pri()
19    {
20        // The derived class always uses the immediate parent's class access specifications
21        // Thus, Pub uses Base's access specifiers
22        m_nPublic = 1; // okay: anybody can access public members
23        m_nPrivate = 2; // not okay: derived classes can't access private members in the base class!
24        m_nProtected = 3; // okay: derived classes can access protected members
25    }
26};
27 
28int main()
29{
30    // Outside access uses the access specifiers of the class being accessed.
31    // Note that because Pri has inherited privately from Base,
32    // all members of Base have become private when access through Pri.
33    Pri cPri;
34    cPri.m_nPublic = 1; // not okay: m_nPublic is now a private member when accessed through Pri
35    cPri.m_nPrivate = 2; // not okay: can not access private members from outside class
36    cPri.m_nProtected = 3; // not okay: m_nProtected is now a private member when accessed through Pri
37 
38    // However, we can still access Base members as normal through Base:
39    Base cBase;
40    cBase.m_nPublic = 1; // okay, m_nPublic is public
41    cBase.m_nPrivate = 2; // not okay, m_nPrivate is private
42    cBase.m_nProtected = 3; // not okay, m_nProtected is protected
43}
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:
1class Base
2{
3public:
4    int m_nPublic;
5private:
6    int m_nPrivate;
7protected:
8    int m_nProtected;
9};
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.
1class D2: private Base
2{
3public:
4    int m_nPublic2;
5private:
6    int m_nPrivate2;
7protected:
8    int m_nProtected2;
9}
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.
1class D3: public D2
2{
3public:
4    int m_nPublic3;
5private:
6    int m_nPrivate3;
7protected:
8    int m_nProtected3;
9};
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.

No comments: