Protected Data With Private Inheritance
11.5 Write a program to declare protected data in base class. Access data of base class declared under Protected section using member functions of derived class.
Fig: Difference between base and derived class objects
We have learned about the access specifiers in detail. Now before moving on to a new topic, let us revise a few points related to private, public, and protected keywords. Table gives a description of access specifiers followed by an explanation. Figure (Access scope of class members) gives a pictorial representation of access control.
Table: Access specifiers with their scope
Fig: Access scope of class members
- Accessible to the member function of the same class, derived class, and using objects. When a class is derived by private derivation, public members of the base class are private in the derived class.
- Accessible to the member function inside its own class but not in the derived class. The derived class cannot access the private members of the base class directly. The private members of the base class can be accessed only using the public member function of the same class.
- Accessible to the member function of the base and derived classes. When a class is derived privately, the protected members of the base class become private, and when they are derived publicly, the protected members remain protected in the derived class.
- All private members of the class are accessible to public members of the same class. They cannot be inherited.
- The derived class can access the private members of the base class using the member function of the base class.
- All the protected members of the class are available to its derived classes and can be accessed without the use of the member function of the base class. In other words, we can say that all protected members act as public for the derived class.
- If any class is prepared for deriving classes, it is advisable to declare all members of the base class as protected, so that derived classes can access the members directly.
- All the public members of the class are accessible to its derived class. There is no restriction for accessing elements.
- The access specifier required while deriving class is either private or public. If not specified, private is default for classes and public is default for structures.
- Constructors and destructors are declared in the public section of the class. If declared in the private section, the object declared will not be initialized and the compiler will flag an error.
- The private, public, and protected sections (visibility sections) can be defined several times in any sequence. In Figure (Visibility sections), you can observe that the public section is declared twice, the private section is defined at last, and the protected section is declared in between two public sections. The sections can be declared in any sequence for any number of times.
Fig: Visibility sections
Member functions scopeThe following type of functions can have access to the protected and private members of the class. Table describes these functions. Figure Access scope in classes) shows access scope.
Table: Access controls of functions
Fig: Access scope in classes
- The class member function can access the private, protected, and public members.
- The derived class member function cannot access the private members of the base class directly. However, the private members can be accessed using the member functions of the base class.
- The friend function of a friend class can access the private and protected members.
- The member function of a friend class can access the private and protected member of the class.