Loading....
Coupon Accepted Successfully!

 

Access Specifiers And Simple Inheritance

We have studied the access specifiers private and public in detail and briefly studied the protected access specifiers.
 
The public members of a class can be accessed by an object directly outside the class. Directly means when objects access the data member without the member function of the class. The private members of the class can only be accessed by the public member function of the same class. The protected access specifier is the same as private. The only difference is that it allows its derived classes to access the protected members directly without the member functions. This difference is explained later with suitable examples.
 
A new class can be defined as per the syntax given below. The derived class is indicated by associating with the base class. A new class also has its own set of member variables and functions. The syntax given below creates the derived class.
 

class name of the derived class: access specifiers - name of the base class

{

Description: 146493.png Description: 146518.png

Description: 146516.png // member variables of new class (derived class)

Description: 146496.png Description: 146520.png

}

 
The names of the derived and base classes are separated by a colon (:). The access specifiers may be private or public. The keyword private or public is specified followed by a colon. Generally, the access specifier is to be specified. In the absence of an access specifier, the default is private. The access specifiers decide whether the characteristics of the base class are derived privately or publicly. The derived class also has its own set of member variables and functions. The following are the possible syntaxes of declaration:
  1.  
    In the above syntax, class A is a base class, and class B is a derived class. Here, the class B is derived publicly.
  2.  
    In the above syntaxes, the class B is derived privately from the base class A. If no access specifier is given, the default mode is private. The use of protected access specifier is the same as private access. If struct is used instead of class, the default derivation is public. It is important to note the following points:
    1. When a public access specifier is used (example 1), the public members of the base class are public members of the derived class. Similarly, the protected members of the base class are protected members of the derived class.
    2. When a private access specifier is used, the public and protected members of the base class are the private members of the derived class.

Public Inheritance

The class can be derived either publicly or privately. No third type exists. When a class is derived publicly, all the public members of the base class can be accessed directly in the derived class. However, in private derivation, an object of the derived class has no permission to directly access even public members of the base class. In such a case, the public members of the base class can be accessed using public member functions of the derived class.
 
In case the base class has private member variables and a class derived publicly, the derived class can access the member variables of the base class using only member functions of the base class. The public derivation does not allow the derived class to access the private member variable of the class directly as is possible for public member variables. The following example illustrates public inheritance where base class members are declared public and private.

11.1 Write a program to derive a class publicly from base class. Declare the base class with its member under public section.

OUTPUT
Member of A : 20
Member of B : 30

 
Explanation: In the above program, two classes are defined, and each contains one public member variable. The class B is derived publicly from the class A. Consider the following statement:

class B : public A

The above statement is used to derive the new class. The keyword public is used to derive the class publicly. The access specifier is followed by the name of the base class.


In function
main(), b is an object of class B. The object b can access the members of class A as well as of class B through the following statements:

b.x=20; // Access to base class members

b.y=30; // Access to derived class members

 

Thus, the derived class holds the members of the base class, and its object has the permission to access the members of the base class.

11.2 Write a program to derive a class publicly from base class. Declare the base class member under private section.

 

OUTPUT
x=20
y=30
 
Explanation: In the above program, the class A has one private member, default constructor, and member function showx(). The class B is derived from the class A publicly. The class B contains one public member variable y, default constructor, and member function show().

In function main(), b is an object of the derived class B. Though the class B is derived publicly from the class A, the status of the members of the base class remains unchanged. The object b can access public members, but cannot access the private members directly. The private members of the base class can be accessed using the public member function of the base class.

The object b invokes the member function show() of the derived class. The function show() invokes the showx() function of the base class.

The object b can access the member function defined in both base and derived classes. The following statements are valid:

b.showx(); // Invokes member function of base class

b.show(); // Invokes member function of derived class

 

The constructor and member functions of the derived class cannot access the private members. Due to this, separate constructor and member functions are defined in both base and derived classes.

Private Inheritance

The objects of the privately derived class cannot access the public members of the base class directly. Hence, the member functions are used to access the members.

11.3 Write a program to derive a class privately. Declare the member of base class under public section.
 
OUTPUT
x=20
y=40
 

Explanation: In the above program, the class B is derived privately from the class A. The member variable x is a public member of the base class. However, the object b of the derived class cannot directly access the variable x. The following statements are invalid:

b.x=30; // cannot access

 

The class B is derived privately. Hence, its access is restricted. The member function of the derived class can access the members of the base class. The function show() does the same.

11.4 Write a program to derive a class privately.

 


OUTPUT
x=20
y=40

 
Explanation: In the above program, the member variables of the base class and derivation methods are private. The derived class and its objects cannot access the private members of the base class directly. Hence, separate constructor and member functions are defined in both base and derived classes. The object b invokes the member function show() to display the members. The constructors are used to initialize the member variables.
 
From the last few programs, it is clear that handling the public data of a class is not a tough task for the programmer. However, while accessing the private data, difficulties are faced by the programmer, because they cannot be accessed without the member functions. Even the derived class has no permission to access the private data directly. Hence, the programmer needs to define separate constructor and member functions to access the private data of that particular base class. Due to this, the length of the program is increased.





Test Your Skills Now!
Take a Quiz now
Reviewer Name