Skip to main content

Inheritance in C++

Inheritance in C++

In this tutorial you will learn:
1. What is inheritance?
2. What are different types of inheritance?
3. Why to use inheritance?
4. C++ syntax

What is inheritance?
A form of software reuse in which you create a class that absorbs an existing class’s data and behavior and enhances them with new capabilities. Just like we are the children of our parents and we inherit certain characteristics of our parents, similarly in a program if we have more than one classes in a program , one class may be the base(parent) class and the other class may be the derived(child) class. Likewise two unrelated classes can be present in a single program.
Derived class is inherited from the parent class and it contains some specific features(data members or member functions) of the base class. It is usually not necessary that the derived class contain all the data members and the member function of the base class. It depends of the type of inheritance between the base and derived class which may be public, private or protected. Several classes can be inherited from the base class by changing the specifier. In some programs the inherited class can also act as a base class and that inherited class can also act as the base for the other classes. Inheritance saves time which we could waste by writing the code that has already been written and it is helpful in reusing the already made code which is the key feature of object oriented programming.

Note that private aspects of the base class are part of the child, but are not (directly) accessible within the derived class. C++ also provide a feature of deriving a class from two classes at the same time. Hence C++ supports multiple inheritance whereas java doesn’t.
Basic syntax for inheritance:
class nameOfDerivedClass : kind nameOfBaseClass
kind is one of public, private or protected.

Types of Inheritance:
There are three types of inheritance
• Public Inheritance
• Protected Inheritance
• Private inheritance

Public Inheritance:
It is the most commonly used type of inheritance. In public inheritance public and protected members of the base class remain, respectively, public and protected members of the derived class.
class nameOfDerivedClass : public nameOfBaseClass

Protected Inheritance:
Public and protected member of the base class are inherited as protected members of the derived class. A base class’s protected members can be accessed within that base class and by members of any classes derived from that base class. Offers an intermediate level of protection between public and private access

class nameOfDerivedClass : protected nameOfBaseClass

Private Inheritance:
Public and protected members of the base class are inherited as private members of the derived class.
class nameOfDerivedClass : private nameOfBaseClass

Why we should use Inheritance?
Inheritance allow us to use the already present code in the program, hence it saves our time and resources. Due to inheritance the reliability of the programs increases remarkably because when we need not to write the new code and use the preexisting code which has been tested already by the programmer there is lesser chance of error. Due to inheritance the data there is consistency of interface and rapid prototyping is achieved.


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.