Loading....
Coupon Accepted Successfully!

 

Protected Data With Private Inheritance

The member function of the derived class cannot access the private member variables of the base class. The private members of the base class can be accessed using the public member function of the same class. This approach makes the program lengthy. To overcome the problems associated with private data, the creator of C++ introduced another access specifier protected. The protected is similar to private, but it allows the derived class to access the private members directly. Consider the following example:

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.
 
OUTPUT
x=30
y=40
 
Explanation: In this program, the data member variable of class A is declared under the protected section. The class A contains neither default constructor nor any member function. The class b is derived from class A. The member function of the derived class B can access the protected member of the base class.
 
Thus, the protected mechanism reduces the program size. The derived class need not depend on the member function of the base class to access the data. The protected data safeguard the data from direct use and allow only the derived classes to access the data.
 
The protected access specifier should be used when it is known in advance that a particular class can be used as a base class. The data members of the classes that can be used as a base class should be declared as protected. In such classes, the programmer need not define functions. The member functions of the base class are rarely useful.
 
In the inheritance mechanism, the derived classes have more number of members as compared with the base class. The derived class contains the properties of a base class and a few of its own. The object of the derived class can access the members of both base and derived classes. However, the object of the base can access the members of only the base class and not any of the derived class, as shown in Figure (Difference between base and derived class objects) Hence, the programmer always uses objects of the derived classes and performs operations. The member function of the base class cannot access the data of the derived class. Obviously, the programmer uses the object of the derived classes and possibly avoids defining the member function in the base 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 privatepublic, 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

Sr.No.

Base class
access mode

Derived class access mode

Private derivation

Public derivation

Protected derivation

A

public

private

public

protected

B

private

Not inherited

Not inherited

Not inherited

C

protected

private

protected

protected


Fig: Access scope of class members

  1. 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.
  2. 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.
  3. 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.
The syntax of public, private, and protected access specifiers is as follows:
 
 
​If the member variables of a class are protected, the access specifier should be private. A protected member can be considered a hybrid of public and private members. A protected member is public for its derived class and private for other class members. Member functions and friends can use the member classes derived from the declared class. Only objects of the derived class do this task. It is possible to override the default struct access with private or protected. However, it is not possible to override the default union access. Figure (Access scope of class members) shows a pictorial representation of the access control in classes.
  1. All private members of the class are accessible to public members of the same class. They cannot be inherited.
  2. The derived class can access the private members of the base class using the member function of the base class.
  3. 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.
  4. 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.
  5. All the public members of the class are accessible to its derived class. There is no restriction for accessing elements.
  6. 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.
  7. 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.
  8. 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 scope

The 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

 

Sr. No.

Type of Functions

Access modes

Private

Protected

Public

A

Class member function

B

Derived class member

C

Friend function

D

Friend class member



Fig: Access scope in classes

  1. The class member function can access the private, protected, and public members.
  2. 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.
  3. The friend function of a friend class can access the private and protected members.
  4. The member function of a friend class can access the private and protected member of the class.




Test Your Skills Now!
Take a Quiz now
Reviewer Name