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
{
.... ... ....
};
Things to remember while Using Public, Protected and Private Inheritance
- 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).
- Objects of derived class with private and protected inheritance cannot access any data member of a base class.
- Objects of derived class with public inheritance can access only public member of a base class.
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 |
No comments:
Post a Comment