cout « "\n \n";
ml.show ( );
}
Making a Private Member Inheritable
Basically we have visibility modes to specify that in which mode you are deriving the another class
from the already existing base class. They are:
a. Private: when a base class is privately inherited by a derived class, 'public
members' of the base class become private members of the derived class and
therefore the public members of the base class can be accessed by its own
objects using the dot operator. The result is that we have no member of base
class that is accessible to the objects of the derived class.
b. Public: On the other hand, when the base class is publicly inherited, 'public
members' of the base class become 'public members' of derived class and
therefore they are accessible to the objects of the derived class.
c. Protected: C++ provides a third visibility modifier, protected, which serve a
little purpose in the inheritance. A member declared as protected is accessible
by the member functions within its class and any class immediately derived
from it. It cannot be accessed by functions outside these two classes.
The below mentioned table summarizes how the visibility of members undergo modifications when
they are inherited
Base Class Visibility Derived Class Visibility
Public Private Protected
Private X X X
Public Public Private Protected
Protected Protected Private Protected
The private and protected members of a class can be accessed by:
a. A function i.e. friend of a class.
b. A member function of a class that is the friend of the class.
c. A member function of a derived class.
Student Activity
1. Define Inheritance. What is the inheritance mechanism in C++?
2. What are the advantage of Inheritance?
3. What should be the structure of a class when it has to be a base for other classes?