What is the difference between public, private, and protected inheritance in C++?
All of the questions I've found on SO deal with specific cases.
What is the difference between public, private, and protected inheritance in C++?
All of the questions I've found on SO deal with specific cases.
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;
};
Base is also aware that Base contains publicMember.Base contains protectedMember.Base is aware of privateMember.By "is aware of", I mean "acknowledge the existence of, and thus be able to access".
The same happens with public, private and protected inheritance. Let's consider a class Base and a class Child that inherits from Base.
public, everything that is aware of Base and Child is also aware that Child inherits from Base.protected, only Child, and its children, are aware that they inherit from Base.private, no one other than Child is aware of the inheritance.It has to do with how the public members of the base class are exposed from the derived class.
As litb points out, public inheritance is traditional inheritance that you'll see in most programming languages. That is it models an "IS-A" relationship. Private inheritance, something AFAIK peculiar to C++, is an "IMPLEMENTED IN TERMS OF" relationship. That is you want to use the public interface in the derived class, but don't want the user of the derived class to have access to that interface. Many argue that in this case you should aggregate the base class, that is instead of having the base class as a private base, make in a member of derived in order to reuse base class's functionality.
Protected data members can be accessed by any classes that inherit from your class. Private data members, however, cannot. Let's say we have the following:
class MyClass {
private:
int myPrivateMember; // lol
protected:
int myProtectedMember;
};
From within your extension to this class, referencing this.myPrivateMember won't work. However, this.myProtectedMember will. The value is still encapsulated, so if we have an instantiation of this class called myObj, then myObj.myProtectedMember won't work, so it is similar in function to a private data member.
Summary:
When inheriting, you can (in some languages) change the protection type of a data member in certain direction, e.g. from protected to public.
The private members of a base class can only be accessed by members of that base class .
The public members of a base class can be accessed by members of that base class, members of its derived class as well as the members which are outside the base class and derived class.
The protected members of a base class can be accessed by members of base class as well as members of its derived class.
private: base
protected: base + derived
public: base + derived + any other member
I have tried explaining inheritance using a picture below.
The main gist is that the private members of parent class are never directly accessible from derived/child class but you can use parent class's member function to access the private members of parent class.
Private variables are always present in derived class but it cannot be accessed by derived class. Its like its their but you cannot see with your own eyes but if you ask someone form the parent class then he can describe it to you.

It's essentially the access protection of the public and protected members of the base class in the derived class. With public inheritance, the derived class can see public and protected members of the base. With private inheritance, it can't. With protected, the derived class and any classes derived from that can see them.
In C++, there are three access specifiers:
public - members are accessible from outside the class
private - members cannot be accessed (or viewed) from outside the class
protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes