What is Inheritance in C++ ?
C++ strongly supports the concept of re-usability. The C++ classes can be used again in several ways. Once a class has been written and tested, it can be adopted by another programmers. This is basically created by defining the new classes, reusing the properties of existing ones. The mechanism of deriving a new class from an old one is called ‘INHERTTENCE’.
How it Works?
This is often referred to as ‘IS-A’ relationship. The old class is called ‘BASE’ class and the new one is called ‘DERIVED’ class. The Base class is like a parent class and Derived class is like a child. Child inherits the properties from Parents, same way one class can acquire the properties of another class.
Syntax:
class derived-class-name : visibility-mode base-class-name
{
………
………
}
The colon (:) indicates that the derived class name is derived from the base-class-name. the visibility mode (access specifiers) is optional and if present, may be either private or protected or public. The default is private. Visibility mode specifies whether the features of the base class are privately derived or publicly derived.
Inheritance is the way to accept some of the necessary data of one class to another class, that satisfies the concept of re-usability of resources or data. A class can also inherit properties from more than one class or from more than one level.
Types of Inheritance in C++
- Single level Inheritance
- Multi level Inheritance
- Multiple Inheritance
- Hybrid Inheritance
- Hierarchical Inheritance
Example:
class xyz //base class
{
members of xyz
};
class ABC : public xyz //public derivation
{
members of ABC
};
class ABC : private XYZ //private derivation (by default)
{
members of ABC
};
Here, keyword public and private are access specifiers. which defines the level of access of base class data into the derived class. Three types of access specifiers are used in inheritance to specify the access level.
List of Access Specifiers (access modifiers)
- Public
- Private
- Protected
Role of Access modifiers in Inheritance
In Inheritance, Base class data can be inherited by derived class using any one of the access specifiers. Based on the use of access modifiers derivation will perform differently.


1. Public Derivation
class ABC : public XYZ //public derivation
{
members of ABC;
};
Here, the base class is publicly inherited to the derived class.
- Public members of the base class is publicly inherited.
- Public members of the base class become public members of the derived class and therefore they are accessible to the objects of the derived class.
- Private members are not inherited and therefore, the private members of a base class will never become the members of its derived class.
- Protected members of the base class will become protected members for the derived class.
2. Private Derivation
class ABC : private XYZ //private derivation
{
members of ABC;
};
Here, the private inheritance of base class to the derived class.
- When a base class is privately inherited by a derived class, public members of the base class can only be accessed by the member functions of the derived class.
- Private members of base class are inaccessible to the objects of the derived class.
- Protected members of base class will become a private members of derived class.
3. Protected Derivation
class ABC:protected XYZ // protected derivation
{
members of ABC;
};
- When a base class is protected inherited by a derived class, public members of the base class can only be accessed by the member functions of the derived class.
- Private members of base class are inaccessible to the objects of the derived class.
- If private members of base class are to be inherited to derived class then declare them as protected.
- Protected members of base class will remain protected for derived class.
Look at the below images, it shows the use of different access modifiers for the inheritance of data.

Facts :
- In all the cases, the private members are not inherited and therefore, the private members of a base class will never become the members of its derived class.
- In inheritance, some of the base class data elements and member functions are “inherited” into the derived class.
- Inheritance, when used to modify and extend the capability of the existing classes, becomes a very powerful tool for incremental program development.
You must log in to post a comment.