c++inheritanceencapsulationaccess-specifierc++-faq

What is the difference between public, private, and protected inheritance?


What is the difference between public, private, and protected inheritance in C++?


Solution

  • To answer that question, I'd like to describe member's accessors first in my own words. If you already know this, skip to the heading "next:".

    There are three accessors that I'm aware of: public, protected and private.

    Let:

    class Base {
        public:
            int publicMember;
        protected:
            int protectedMember;
        private:
            int privateMember;
    };
    

    By "is aware of", I mean "acknowledge the existence of, and thus be able to access".

    next:

    The same happens with public, private and protected inheritance. Let's consider a class Base and a class Child that inherits from Base.