fastread homefastrread library fastread menu

C++ : Public, Protected and Private Inheritance

Tutorial by:Manisha Dubey      Date: 2016-06-18 01:05:00

❰ Previous Next ❱

You can declare a derived class from a base class with different access control, i.e., public inheritance, protected inheritance or private inheritance.

class base
{
.... ... ....
};

class derived : access_specifier base
{
.... ... ....
};

/* Note: Either keyword public, protected or private is used in place of access_specifier. */

Things to remember while Using Public, Protected and Private Inheritance

 

  1. Protected and public members(data and function) of a base class are accessible from a derived class(for all three: public, protected and private inheritance).
  2. Objects of derived class with private and protected inheritance cannot access any data member of a base class.
  3. Objects of derived class with public inheritance can access only public member of a base class.

 

 

 
 

Public, protected and private inheritance in C++ programming with different access combinations

Summary of Public, Protected and Private Inheritance

Accessibility in Public Inheritance

Accessibility private protected public
Accessible from own class? yes yes yes
Accessible from dervied class? no yes yes
Accessible outside dervied class? no no yes

Accessibility in Protected Inheritance

Accessibility private protected public
Accessible from own class? yes yes yes
Accessible from dervied class? no yes yes
Accessible outside dervied class? no no no

Accessibility in Private Inheritance

Accessibility private protected public
Accessible from own class? yes yes yes
Accessible from dervied class? no yes yes
Accessible outside dervied class? no no no

 

❰ Previous Next ❱


C++

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website