Visibility mode is optional. If specified, must be public or private; else by default will be private. It specifies whether the feature of the base class is publicly or privately inherited.
- Visibility Mode: Public
v Inheritance of a base class with visibility mode public, by a derived class, causes public members of the base class to become public members of the derived class and causes protected members of the base class to become protected members of the derived class.
v Member function and objects of the derived class can treat the derived members as though they are defined in the derived class itself.
v The public members of a class can be accessed by the objects of the class.
v Hence, the objects of a derived class can access public members of the base class that are inherited as public using dot operator. However, protected can’t be accessed with the dot operator.
- Visibility Mode: Private
v Inheritance of a base class with visibility mode private by a derived class causes public members of the base class to become private members of the derived class and protected members of the base class become private members of the derived class.
v Member functions and objects of a class can treat the derived members as though they are defined in the derived class with the private modifiers
v Thus, objects of the derived class can’t access these members.
- Visibility Mode of Class Members:
v Private class members in base class can’t be inherited in derived class whether the visibility mode be public or private.
v Protected class members in base class remains protected in derived class when visibility mode is defined public and becomes private in derived class when visibility mode is private.
v Public class members in base class remain public in derived class when visibility mode id public and become private when visibility mode is private.
Key Points
1. The private members of the base class remain private to the base class, whether the base class is inherited publicly or privately.
2. They add to the data items of the derived class and they are not directly accessible to the members of a derived class.
3. Derived classes can access them through the inherited member functions of the base class.
No comments:
Post a Comment