(i) public inheritance:
i) When the visibility-mode is public the base class is publicly inherited.
ii) In public inheritance, the public members of the base class become public members of the derived class and therefore they are accessible to the objects of the derived class.
iii) When deriving a class from a public base class, protected members of the base class become protected members of the derived class.
iv)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.
v) Syntax:
class A
{
Public:
Member variables;
Member function;
};
class B : public A
{
Members of class B
};
(ii) protected inheritance:
1. If we want to inherit private data by a class, the only option is to change the visibility limit from private to public, but this will eliminate the advantage of data hiding.
2. Therefore to achieve data hiding, C++ provides a third visibility modifier, protected which has limited purpose in inheritance.
3. A member declared protected is accessible by the member functions within its class and any class immediately derived from it.
It cannot be accessed by the functions outside these two classes.
4. In protected inheritance, protected and public members of base class become protected members of derived class.
5. When a protected member is inherited in public mode, it becomes protected in the derived class too, and therefore is accessible by the member.
Syntax:
class A
{
protected :
Member variables;
Member function;
};
class B : protected A
{
Members of class B
};