<< Inheritance in C++ Programming - Part 1
Public, Protected and Private Inheritance in C++ Programming
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
- 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 derived class?
|
no
|
yes
|
yes
|
Accessible
outside derived class?
|
no
|
no
|
yes
|
Accessibility in Protected Inheritance
Accessibility
|
private
|
protected
|
public
|
Accessible
from own class?
|
yes
|
yes
|
yes
|
Accessible
from derived class?
|
no
|
yes
|
yes
|
Accessible
outside derived class?
|
no
|
no
|
no
|
Accessibility in Private Inheritance
Accessibility
|
private
|
protected
|
public
|
Accessible
from own class?
|
yes
|
yes
|
yes
|
Accessible
from derived class?
|
no
|
yes
|
yes
|
Accessible
outside derived class?
|
no
|
no
|
no
|
No comments:
Post a Comment