Difference between private, public, and protected inheritance

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

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

相关标签:
16条回答
  • 2020-11-21 06:49

    Private:

    The private members of a base class can only be accessed by members of that base class .

    Public:

    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.

    Protected:

    The protected members of a base class can be accessed by members of base class as well as members of its derived class.


    In short:

    private: base

    protected: base + derived

    public: base + derived + any other member

    0 讨论(0)
  • 2020-11-21 06:50

    1) Public Inheritance:

    a. Private members of Base class are not accessible in Derived class.

    b. Protected members of Base class remain protected in Derived class.

    c. Public members of Base class remain public in Derived class.

    So, other classes can use public members of Base class through Derived class object.

    2) Protected Inheritance:

    a. Private members of Base class are not accessible in Derived class.

    b. Protected members of Base class remain protected in Derived class.

    c. Public members of Base class too become protected members of Derived class.

    So, other classes can't use public members of Base class through Derived class object; but they are available to subclass of Derived.

    3) Private Inheritance:

    a. Private members of Base class are not accessible in Derived class.

    b. Protected & public members of Base class become private members of Derived class.

    So, no members of Base class can be accessed by other classes through Derived class object as they are private in Derived class. So, even subclass of Derived class can't access them.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 06:54

    Public inheritance models an IS-A relationship. With

    class B {};
    class D : public B {};
    

    every D is a B.

    Private inheritance models an IS-IMPLEMENTED-USING relationship (or whatever that's called). With

    class B {};
    class D : private B {};
    

    a D is not a B, but every D uses its B in its implementation. Private inheritance can always be eliminated by using containment instead:

    class B {};
    class D {
      private: 
        B b_;
    };
    

    This D, too, can be implemented using B, in this case using its b_. Containment is a less tight coupling between types than inheritance, so in general it should be preferred. Sometimes using containment instead of private inheritance is not as convenient as private inheritance. Often that's a lame excuse for being lazy.

    I don't think anyone knows what protected inheritance models. At least I haven't seen any convincing explanation yet.

    0 讨论(0)
提交回复
热议问题