What is inheritance? | C++ Advanced | Part -4

What is inheritance

Why do I look so similar to my father? My nose is also exactly like him.

That’s because you have inherited his features.

Inheritance is when a class derives some properties from another class.

Just like human beings inherit features from their parents.

The class which uses/inherits the properties is known as derived class or sub class.

And the class from which the features are taken/inherited is known as the base class or super class.

To inherit properties of a class, colon(:) is used.

class BaseClass{

//body

};

class DerivedClass: access_specifier BaseClass{

//body

};

This is how we inherit a class. Here DerivedClass is an inherited class from BaseClass.

Here, access_specifier can be publicprivate or protected.

derived class takes/inherits the members of the base class depending upon the mode of inheritance.

Let’s learn about the modes of inheritance and see how they differ.

*ClassB:access_specifier ClassA is the correct syntax to inherit properties of ClassA.

Modes of Inheritance

Modes of inheritance
Modes of inheritance

There are three modes of inheritance:

  1. Public inheritance
  2. Protected inheritance
  3. Private inheritance

Public Inheritance

  • When a class is derived/made from a public base class, it is known as Public inheritance.
  • In this mode only the public and protected members will be inherited and not the private members.
  • The public members become public in the derived class and the protected members become protected.

Protected Inheritance

  • When a class is derived/made from a protected base class, it is known as Protected inheritance.
  • In this mode only the public and protected members will be inherited and not the private members.
  • The public members and the protected members of the base class become protected in the derived class.

Private Inheritance

  • When a class is derived from a private base class, it is known as Private inheritance
  • In this mode only the public and protected members will be inherited and not the private members.
  • The public members and the protected members of the base class become private in the derived class.
*In public mode of inheritance, private members are not inherited and public and protected members of the base are inherited.

Inheritance in action

Since we have learnt about the modes of inheritance, let’s understand inheritance using an example.

class BaseClass{

public:

int sum;

void add(int num1, int num2){

sum = num1+num2;

}

};

class DerivedClass:public BaseClass{

public:

void showSum(){

cout<<sum;

}

};

In the above snippet we have a class DerivedClass which inherits the BaseClass class.

We will now create an object of type DerivedClass.

int main() {

DerivedClass d; //object of type DerivedClass

d.add(3,5); //Calling add function inherited from base class

d.showSum(); //Displaying the result

Can you guess the output now?

We are calling the add() function declared in BaseClass and then we are calling the showSum() function.

We are passing 3 and 5 as parameters.

So, if your answer was 8 then you are right.

Types of inheritance

TYPES OF INHERITANCE
TYPES OF INHERITANCE

Classes can inherit from other classes and in different ways.

This brings us to the study of different types of inheritances.

There are four types of inheritance in C++ – Single inheritance, Multiple inheritance, Multilevel inheritance and Hierarchical inheritance.

Single inheritance

It is the simplest type of inheritance.

In this, the derived class inherits the properties of a base class.

class BaseClass{

//body

};

class DerivedClass :access_specifier BaseClass{

//body

};

Just that

Can you guess the type of inheritance we used in the add example we saw earlier?

If your answer was single inheritance then keep rolling.

In Single inheritance one and only one class can inherit another class.

That is a single base class and a single derived class.

Multiple Inheritance

In multiple inheritance, a derived class inherits properties from more than one base class.

From the above shown image we can understand that DerivedClass is a class which is derived from BaseClass1 and BaseClass2.

It can be syntactically represented as

 class BaseClass1{

//body

};

class BaseClass2{

//body

};

class DerivedClass :access_specifier BaseClass1, access_specifier BaseClass2{

//body

};

Here, DerivedClass inherits from both BaseClass1 and BaseClass2.

Multilevel Inheritance

In multilevel inheritance there is a chaining of classes.

From the above shown image we can understand that DerivedClass1 is a sub class of BaseClass and DerivedClass2 is a sub class of DerivedClass1.

It can be syntactically represented as

 class BaseClass{

//body

};

class DerivedClass1:access_specifier BaseClass{

//body

};

class DerivedClass2 :access_specifier DerivedClass1{

//body

};

Here, DerivedClass1 inherits from BaseClass and DerivedClass2 inherits from DerivedClass1.

There is no limit of this chaining. There can be a class DerivedClass3 which inherits class DerivedClass2 and so on

Hierarchical Inheritance

In hierarchical inheritance, more than one derived classes are inherited from a single base class.

That is, a single base class has multiple derived classes.

There could be as many child classes as we want.

But all these classes must have only a single parent class.

 class BaseClass{

//body

};

class DerivedClass1:access_specifier BaseClass{

//body

};

class DerivedClass2:access_specifier BaseClass{

//body

};

Here, DerivedClass1 and DerivedClass2 are both inherited from BaseClass.

Apart from these, one more type of inheritance exists – Hybrid inheritance.

Hybrid inheritance is when more than one type of inheritance are combined together.

Awesome, we have learnt a lot.

Let’s put our knowledge to test.

To summarize

  • In private mode of inheritance private members are not inherited and public and protected members of the base class turn to private in derived class.
  • The snippet aligns with Hierarchical inheritance. 
  • A colon(:) is used in inheritance and not a semicolon.
  • Inheritance is when a class derives some properties from another class.
  • The class which inherits/ takes the properties is known as derived class and the class from which the properties are inherited / taken is known as the base class.
  • There are three modes of inheritance – Public, Protected and Private.
  • There are four types of inheritance in c++ – Single inheritance, Multiple inheritance , Multilevel inheritance and Hierarchical inheritance.
  • Hybrid inheritance is when more than one type of inheritance are combined together.

Leave a Reply

Your email address will not be published. Required fields are marked *