Inheritance

Inheritance is the passing of traits from parents to their offsprings.
In computer science, we have parent class and child class. Child class inherit the attributes of parent class.


class ParentClassName{
	
};
class ChildClass : modifier ParentClass{
	
	
};
int main(){
	Child C1;
	/*with C1 you can access data members 
  of Parent class except private, does 
  not matter if they are not members of 
  Child class*/
}

Parent class is also called Base class Super class.
Child class is also called Derived class or Sub class.
Note: It is a custom to start the name of class with capital letter. But if you are fitting well with small letter, there is no problem.


inheritance.cpp
#include<iostream>
using namespace std;
//parent class
class Vehicles{
  public:
    void canAccelerate(){
    cout<<"Accelerate\n";
      }
};
//child class
class Volvo : public Vehicles{
  //class members if any    
};
int main(){
  Volvo v;
  /*canAccelerate() is the function of 
  parent class but it is inherited by 
  the child class Volvo*/
  v.canAccelerate();
}

Protected Data Members:

Before we go into details, we should study about protected data members. When we inherit class using public access modifier, child class can access only public members of parent class. But if we want to access private members? Actually we can not access private members even in child class. For this purpose, we make data members protected. The protected data members can not be accessed in main function, they can only be accessed in child class.

We have studied access specifiers of class members. Access specifiers are also used in inheritance. Based on access specifiers, there are three modes of inheritance:
  • Public mode
  • Private mode
  • Protected mode
Lets explore some details.

Access Specifiers in inheritance:

public access specifier in inheritance:
When a class is inherited using a public access specifier, all public members of the parent class become public members of the child class.


class Parent {
	private:
		//data members if any
	public:
    	int publicMember;
};

class Child : public Parent {
  /* publicMember from Parent class 
  is accessible here or in the main 
  function as a public member of Child 
  class*/
};

private access specifier in inheritance:
When a class is inherited with a private access specifier, all members of the parent class (whether they are public, protected, or private) become private members of the child class.


class Parent {
	private:
    	int privateMember;
    public:
    	//member functions if any
};

class Child : private Parent {
    /* privateMember from BaseClass is 
    accessible here within the member 
    functions of DerivedClass.*/
};

protected access specifier in inheritance:
When a class is inherited with a protected access specifier, all public members of the base class become protected members of the derived class. Protected members of the base class can be accessed directly through objects of the derived class or within member functions of the derived class.


class Parent {
  protected:
      int protectedMember;
  };
  
  class Child : protected Parent {
      /* protectedMember from BaseClass 
      is accessible here as a protected
      member of DerivedClass.*/
  };

Levels of Inheritance:

  • Single Inheritance
  • Multiple Inheritance
  • Multi-level Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

Single Inheritance:

One parent class has one child class.

inheritance.cpp
class BaseClass {
  // Base class members
};

class DerivedClass : public BaseClass {
  // Derived class members
};

Multiple Inheritance:

Multiple inheritance allows a class to inherit from more than one parent classes. One child and many parents.

inheritance.cpp
class BaseClass1 {
  // Base class 1 members
};

class BaseClass2 {
  // Base class 2 members
};

class Derived : public Base1, public Base2 {
  // Derived class members
};

Multi-level Inheritance:

In multilevel inheritance, a child class is created from another child class. If we have class1, class2 and class3, class1 is the parent of class2 and class2 is the parent of class3 and so on. It is like grand-parent(child1), parent(class2), child/grandson(class3).

inheritance.cpp
class Parent1 {
  // Parent1 class members
};

class Parent2 : public Parent1 {
  // Parent2 class members
};

class Child : public Parent2 {
  // Child class members
};

Hierarchical Inheritance:

In hierarchical inheritance, multiple base classes inherit from the same parent class. Many child, one parent.

inheritance.cpp
class Father {
  // Parent class members
};

class Child1 : public Father {
  // Child1 class members
};

class Child2 : public Father {
  // Child2 class members
};

Hybrid Inheritance:

Hybrid inheritance is a combination of single inheritance and multiple inheritance. It involves the use of multiple parent classes and is generally seen in complex class hierarchies.

inheritance.cpp
class Animal {
  // Parent class members
};

class Mammal : public Animal {
  // Mammal class members
};

class Bird : public Animal {
  // Bird class members
};

class Bat : public Mammal, public Bird {
  // Bat class members
};

Inheritance and Constructor:

When we have a base class and a derived class, we need to pass arguments from derived class to base class.


class Shape { 
  protected:
    int length;
    int width;
  
  public:
  
    // Parametrized constructor
    Shape(int length, int width) {
      this->length = length;
      this->width = width;
    }
  
};
  
class Triangle : public Shape {
  public:
    // Parametrized constructor
    Triangle(int base, int height)
        : Shape(base, height)
    {
    }
};