Derivation in C++





Types of derivation

Inheritance is a process of creating a new class from the existing class. The derived class inherits all capability of the base class but can add that of its own.

When a class inherits some member from another class, there must be a way to control the access of inherited members. Thus, while deriving the new classes, the access control specifier gives the way to control over the inherited members. The access specifier in the definition of the derived class specifies whether the feature of the base class are privately derived or publically derived of protected derived.

 

  • Public derivation

 

In public derivation, each public member in a base class becomes the public members of the derived class and each protected members of the base class become the protected members of the derived class.

Hence in public derivation, each public member of a base class can be accessed by the objects of the derived class. Each protected members of base class are accessed directly by the member functions and friend function of the derived class only. But private members of a base class are not directly accessed through the derived class nut can be accessed indirectly using the public or protected member function of base class.

The general syntax of the public derivation is:

Class base_class_name

{

——————

—————–

};

Class derived_class_name: public base_class_name

{

—————–

—————–

};

 

 

  •  Private derivation

 

In private derivation, each public and protected members of the base class become private member of the derived class. Hence, these members can be accesses only by the member function and friend function of the derived class and can’t be accesses by the object of the derived class.

The general syntax of the private declaration is:-

Class base_class_name

{

—————

—————

};

Class derived_class_name: private base_class_name

{

————–

————–

};

 

[NOTE: - Private derivation is rarely used as it ends a hierarchy of classes. It is because the inherited members become private in the derived class. So, they can’t be inherited further if the derived class happens to be the base class of any other class.]

 

  • Protected derivation

 

In protected derivation, each public and protected members of the base class becomes the protected members of the derived class. This means, these members can be accessed by the member function and friend function of the derived class only; not only by the object of the derived class.

But, unlike private derivation, they can still be inherited and accessed by subsequent served class. Hence, it does not end a hierarchy of classes, as private inheritance does.

The general syntax of the protected derivation is:-

Class base_class_name

{

—————

—————

};

Class derived_class_name: protected base_class_name

{

————–

————–

};

 

How to make a private member of base class inheritable?

 

In inheritance, the derived class inherits the public and protected members of the base class. The private members of the base class are not inherited. If the private members needed to be inherited by a derived class, then it can be accomplished in two ways:-

  1. By making the visibility mode of the private members as public.
  2. By making the visibility mode of the private member as protected.

 

[NOTE: - The derived class is also called as child class or sub class. The base class is also known as super class or parent class. The derived class inherits all the members of a base class; however, the derived class has access privilege only to the non private members of the base class. The private members of the base class can’t be accessed directly from within the derived class. But, the objects of the derived class are able to access them through non-private inherited members.]



Related posts:

  1. Access Specifier In C++ Access specifier A key feature of object oriented programming is...
  2. Inheritance in C++ Inheritance in C++ Inheritance is one of the most important...
  3. Class In C++ Class in c++ Class is a group of similar object....
  4. Array of class objects Array of class objects and single inheritance A program consisting...
  5. Destructor In C++ Destructor in c++ It is used o destroy the object...