Home C++ Introduction Decisions Loops Input/Output Functions Stack and Heap References Arrays Searching and Sorting Recursion Pointers Character and Strings Structures Classes Inheritance Exceptions Templatess STL Modern C++ Misc Books ----

Inheritance


Contents

Protected Inheritance



File: i4.cpp
class Base
{
    public:
     int m_public;
    private:
     int m_private;
    protected:
     int m_protected;
};

class Protect: protected Base
{
    // Protected inheritance means:
    // Public inherited members become protected
    //(so m_public is treated as protected)
    // Protected inherited members become protected
    //(so m_protected is treated as protected)
    // Private inherited members stay inaccessible
    //(so m_private is inaccessible)public:
    public:
    Protect()
    {
        m_public = 1;
        // okay: m_public is now protected in Protect
        //m_private = 2;
        // not okay: derived classes can't access private members
        //in the base class
        m_protected = 3;
        // okay: m_protected is protected in Protect
    }
};
class Derived : public Protect
{
    public:
     Derived()
     {
         m_public = 1 ;
         m_protected = 5 ;
     }
};


int main()
{
    // Outside access uses the access specifiers of the class
    //being accessed.
    // In this case, the access specifiers of base.
    Base base;
    base.m_public = 1;
    // okay: m_public is public in Base
    //base.m_private = 2;
    // not okay: m_private is private in Base
    //base.m_protected = 3;
    // not okay: m_protected is protected in Base
    Protect pri;
    //pri.m_public = 1;
    // not okay: m_public is now protected in Protect
    //pri.m_private = 2;
    // not okay: m_private is inaccessible in Protect
    //pri.m_protected = 3;
    // not okay: m_protected is now protected in Protect
}