C++ Inheritance

C++ object oriented programming provides the feature of C++ inheritance using classes. Inheritance is the phenomenon of inheriting some or all members of a class by another class. Members can be variables and functions. Inheritance works between classes. The classes in inheritance are:

  • Base class or Parent class
  • Derived class or Child class

The relationship between base and derived classes is many to many.

Base Class or Parent class

The class from which another class inherits variables or functions is base class or parent class. Base class cannot contain features of its derived classes. A base class can have multiple derived classes.

Derived Class or Child class

The class that inherits variables or functions from other class is derived or child class. The derived class contains all features of its base class and might also contain some new features of its own. A child class can have multiple parent classes.

For instance all vehicles can be represented by a base class vehicle. If programmer needs to specify the type of vehicle, then the base class can have derived classes like car, truck, bike etc. Similarly, to define books, there can be a parent class as Book. The parent class Book can have child classes like fiction, non fiction, self help, educational, etc.

The syntax of defining a parent or base class is same as that of a simple class. While the syntax of defining child class or base class contains the colon sign (:) between child class and parent class. Following is the syntax:

For example, the below code shows a parent class Book that has child class Fiction. The child class Fiction has an additional variable audiobook. Only the object of child class Fiction will have the attribute audiobook and programmer can access it. Any object of class Book will not have the variable audiobook. In case of accessing audiobook for object of class Book, error will arise. However the object of child class Fiction will inherit all the attributes like title, author, pages of its parent class Book.

#include <iostream>
#include <string>
using namespace std;
class Book {                  // Base Class
  public:
    string title;
    string author;
    int pages;
    
};
class Fiction: public Book {  // Derived Class
  public:
    string audiobook;
};
int main() {
  Fiction book;
  book.title = "Jack and the bean stalk"; 
  book.author = "Robert James";
  book.pages = 25;
  book.audiobook = "Available on Amazaon";
  cout << book.title << endl;
  cout << book.author << endl;
  cout << book.pages << endl;
  cout << book.audiobook << endl;
  return 0;
}

The output of the above code after compilation is:

Relation between Parent and Child classes

The child to parent relationship is a is-a relationship. For instance, according to the above example:

  • Fiction is a Book
  • Car is a Vehicle
  • Circle is a shape
  • Part-time employee is an Employee

and the list goes on.

Access Specifiers of Class

A child class can inherit all public and protected members of its parent class. Child class cannot inherit the private members of its parent class. If a programmer wants to keep some member only accessible within the base class and for base class objects only, then those members should be declared under private. The table below shows the three access specifiers. And how the access specifiers have effect on inheritance between parent and child classes.

 Access Specifiers Same Class Child Class Outside Class
 PublicAccessible Accessible Accessible
 Protected Accessible Accessible Not Accessible
 Private Accessible Not Accessible  Not Accessible
Access Specifiers in Inheritance

Types of Inheritance

There are following types of inheritances:

  • Multiple Inheritance
  • Multilevel Inheritance

Multiple Inheritance

A child class can inherit from one or more than one parent classes. When child class has two or more than two parent classes, then it is called multiple inheritance. The syntax for multiple inheritance is:

Each parent class will have its own access specifier that can be public, protected, private. Parent classes are separated by comma. The following diagram shows multiple inheritance where a child class C inherits from two parent classes A and B.

Multiple Inheritance

For example, in the example of university system, a teaching assistant TA can be a part of Faculty and at the same time can be a Student too. In the code below, the child class TA inherits from two parents classes: Student and Faculty, through multiple inheritance. Note that in inheritance the order of constructors is such as that the compiler first executes the constructors of parent class and then executes the constructors of child class. If there are more than one parent classes then the order of execution is same as the order of parent classes list.

#include <iostream>
#include <string>
using namespace std;
class Student {                 
  public:
    Student()
    {
        cout << "Registered as Student" << endl;
    }
    
};
class Faculty {
    public:
    Faculty()
    {
        cout << "Registered as Faculty" << endl;
    }
};
class TA: public Student, public Faculty { 
  public:
    TA()
    {
        cout << "Registered as TA" << endl;
    }
};
int main() {
  TA John;
  return 0;
}

In the above code, compiler will first execute the constructors of class Student as it comes first in the list. It will then execute the constructor of class Faculty. At the end it will execute the constructor of child class TA. The output of the above code after compilation is:

Multilevel Inheritance

Multilevel inheritance is the inheritance where a class is derived from a class that is itself derived from a base class. The following diagram shows how multilevel inheritance works. Here class C is derived from class B. And class B is derived from class A. It makes the class A the grand parent, class B the parent and class C the child.

The syntax for defining multilevel inheritance is as follows:

For example, in the code below the base class is Student. The child class Degree inherits from Student class. The class Discipline inherits from the class Degree. Note that here the class Degree acts as both base and derived class. It is base class for Discipline class and derived class for Student class.

#include <iostream>
#include <string>
using namespace std;
class Student {                 
  public:
    Student()
    {
        cout << "Registered as Student ";
    }
    
};
class Degree: public Student {
    public:
    Degree()
    {
        cout << "in bachelors ";
    }
};
class Discipline: public Degree { 
  public:
    Discipline()
    {
        cout << "of Computer Science" << endl;
    }
};
int main() {
  Discipline John;
  return 0;
}

The compiler first executes the constructors of main parent class, the class that is not derived from any class. The output of the above code after compilation is:

Comments are closed.