Public inheritance in C ++ private and protected inheritance of difference

Public inheritance public), private inheritance (private) and protected inheritance (protected) are three common inheritance.


1. Public inheritance:
  the base class member visibility to its visibility and its general class of objects into the same, visible public members, the other members are not visible. Here to protect members of private members of the same.
  Base class member visibility derived class is derived class , members of the public and protected members of the base class can be found: the members of the public and protected members of the base members of the derived class as a class, they remain in their original state; base class private members are not visible: private members of the base class is still proprietary, the derived class can not access private members of the base class.
  Base class member visibility derived class object to a derived class object, the public members of the base class is visible, the other members are not visible.
So, when public inheritance, object of a derived class can access public members of the base class; a member of a derived class can access public members of the base class and protected members.


2. Private inheritance:
  the base class member visibility to its visibility and its general class of objects into the same, visible public members, the other members are not visible.
  Base class member visibility for the derived class is the derived class , members of the public and protected members of the base class are visible: the members of the public and protected members of the base class are as private members of the derived class, and can not be the derived class subclass access; private members of the base class is invisible: the derived class can not access private members of the base class.
  Base class member visibility derived class object to a derived class object, all members of the base class are not visible.
So, when private inheritance, only members of the base class can no longer be inherited by derived classes direct access down.


3. Protection of inheritance:
  this inheritance in the case of private inheritance is the same. The difference is only for members of the derived class,
  the base class members its visibility and visibility general class and its object is the same object, seen public members, other members are not visible.
  Base class member visibility for the derived class is the derived class , members of the public and protected members of the base class are visible: the members of the public and protected members of the base class are as protected members of the derived class, the derived class and can be subclass access; private members of the base class is invisible: the derived class can not access private members of the base class.
  Base class member visibility derived class object to a derived class object, all members of the base class are not visible.
So, in the protection of inheritance, only members of the base class can no longer be inherited by derived classes direct access down.

Guess you like

Origin www.cnblogs.com/GoldenEllipsis/p/11145411.html