C++

C++’s Inheritance – Detailed instructions from A-Z

One component of the Object Oriented Programming System (OOPs) is inheritance, which enables the child class to take on the attributes (the data members) and functionality (the member methods) of the parent class.

What is a class for kids?
A class that derives from another class is referred to as a child class, derived class, or subclass.
Describe parent class.
The parent class, super class, or base class is the class from which another class derives.

Syntax of Inheritance

class parent_class
{
    //Body of parent class
};
class child_class : access_modifier parent_class
{
   //Body of child class
};

What benefits do C++ programmers get from using inheritance?

The two fundamental benefits of inheritance are readability and code reuse. We do not need to repeat the same code in the child class when the parent class’s properties and functions are inherited by the child class. This results in less code being written, makes it simpler to reuse, and greatly improves the readability of the code.

To further grasp this, let’s use an example from real life: Assume for the moment that Human is a class with attributes like height, weight, color, etc. as well as operations like eating(), sleeping(), dreaming(), working(), etc.
Now that we want to develop Male and Female classes, albeit these classes are distinct from one another, they can inherit certain characteristics and capabilities from Human class because Male and Female are both members of the human species. The remainder can be added to each class independently.
With this method, we write less code because we didn’t have to rewrite any of the properties and functions that both classes inherited from the base class. Additionally, this makes reading the code simpler.

Example of an Inheritance

Let’s look at an example before we cover the different inheritance types:
We have two classes here. Teacher and MathTeacher are parent and child classes respectively since MathTeacher derives from the Teacher class. The parent class’s property collegeName is accessible to the child class.

Another significant feature is that the constructor of the child class is automatically called by the constructor of the base class when an object of the child class is created.

#include <iostream>
using namespace std;
class Teacher {
public:
  Teacher(){
    cout<<"Hey Guys, I am a teacher"<<endl;
  }
  string collegeName = "Beginnersbook";
};
//This class inherits Teacher class
class MathTeacher: public Teacher {
public:
  MathTeacher(){
    cout<<"I am a Math Teacher"<<endl;
  }
  string mainSub = "Math";
  string name = "Negan";
};
int main() {
  MathTeacher obj;
  cout<<"Name: "<<obj.name<<endl;
  cout<<"College Name: "<<obj.collegeName<<endl;
  cout<<"Main Subject: "<<obj.mainSub<<endl;
  return 0;
}

Output:

Hey Guys, I am a teacher
I am a Math Teacher
Name: Negan
College Name: Beginnersbook
Main Subject: Math

Types of Inheritance in C++

1) Single inheritance
2) Multilevel inheritance
3) Multiple inheritance
4) Hierarchical inheritance
5) Hybrid inheritance

Single inheritance

One class exactly inherits one class while using single inheritance.
For example: Lets say we have class A and B

B inherits A

Example of Single inheritance:

#include <iostream>
using namespace std;
class A {
public:
  A(){
     cout<<"Constructor of A class"<<endl;
  }
};
class B: public A {
public:
  B(){
     cout<<"Constructor of B class";
  }
};
int main() {
   //Creating object of class B
   B obj;
   return 0;
}

Output:

Constructor of A class
Constructor of B class

2)Multilevel Inheritance

One class inherits another child class in this sort of inheritance.

C inherits B and B inherits A

Example of Multilevel inheritance:

#include <iostream>
using namespace std;
class A {
public:
  A(){
     cout<<"Constructor of A class"<<endl;
  }
};
class B: public A {
public:
  B(){
     cout<<"Constructor of B class"<<endl;
  }
};
class C: public B {
public:
  C(){
     cout<<"Constructor of C class"<<endl;
  }
};
int main() {
  //Creating object of class C
  C obj;
  return 0;
}

Output:

Constructor of A class
Constructor of B class
Constructor of C class

Several Inheritances

A class may inherit from numerous classes under multiple inheritance. As a result, numerous parent classes may exist for a single child class in this sort of inheritance.
For instance:

C inherits A and B both

Example of Multiple Inheritance:

#include <iostream>
using namespace std;
class A {
public:
  A(){
     cout<<"Constructor of A class"<<endl;
  }
};
class B {
public:
  B(){
     cout<<"Constructor of B class"<<endl;
  }
};
class C: public A, public B {
public:
  C(){
     cout<<"Constructor of C class"<<endl;
  }
};
int main() {
   //Creating object of class C
   C obj;
   return 0;
}
Constructor of A class
Constructor of B class
Constructor of C class

4)Hierarchical Inheritance

One parent class has multiple children classes in this sort of inheritance. For instance:

Class B and C inherits class A

Hierarchical inheritance example

#include <iostream>
using namespace std;
class A {
public:
  A(){
     cout<<"Constructor of A class"<<endl;
  }
};
class B: public A {
public:
  B(){ 
     cout<<"Constructor of B class"<<endl;
  }
};
class C: public A{
public:
  C(){
     cout<<"Constructor of C class"<<endl;
  }
};
int main() {
   //Creating object of class C
   C obj;
   return 0;
}

Output:

Constructor of A class
Constructor of C class

5) Hybrid Inheritance

Multiple inheritance types can be combined to form hybrid inheritance. Hybrid inheritance, for instance, can refer to both a child and parent class connection that follows multiple and hierarchical inheritance.

Related Articles

Leave a Reply

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

Back to top button