 Private Inheritance

Access Control and Inheritance

Posted on

Access Control and Inheritance:

  • A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.
Access public protected private
Same class yes yes yes
Derived classes yes yes No
Outside classes yes no no
  • A derived class inherits all base class methods with the following exceptions:
  • Constructors, destructors and copy constructors of the base class.
  • Overloaded operators of the base class.
  • The friend functions of the base class.
  • When deriving a class from a base class, the base class may be inherited through public, protected or private We hardly use protected or private inheritance, but public inheritance is commonly used. While using different type of inheritance, following rules are applied:

Type of Inheritance:

  • Public Inheritance: When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class’s private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.
  • Protected Inheritance: When deriving from a protected base class, public and protected members of the base class become protected members of the derived class.
  • Private Inheritance: When deriving from a private base class, public and protected members of the base class become private members of the derived class.

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

Advertisements