Difference between private, public, and protected inheritance

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

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

16条回答
  •  悲&欢浪女
    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.

提交回复
热议问题