Difference between private, public, and protected inheritance

后端 未结 16 1796
灰色年华
灰色年华 2020-11-21 06:15

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

16条回答
  •  醉酒成梦
    2020-11-21 06:50

    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.

提交回复
热议问题