Public, Protected and Private Inheritance in C++ Programming

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

Accessibilityprivateprotectedpublic
Accessible from own class?yesyesyes
Accessible from dervied class?noyesyes
Accessible outside dervied class?nonoyes

Accessibility in Protected Inheritance

Accessibilityprivateprotectedpublic
Accessible from own class?yesyesyes
Accessible from dervied class?noyesyes
Accessible outside dervied class?nonono

Accessibility in Private Inheritance

Accessibilityprivateprotectedpublic
Accessible from own class?yesyesyes
Accessible from dervied class?noyesyes
Accessible outside dervied class?nonono
Previous
Next Post »
Thanks for your comment