What is the difference between
public
,private
, andprotected
inheritance in C++?
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.