Inheritance in C++ Programming

- March 03, 2018
Inheritance is a mechanism in which one object acquires all the properties and behaviours of the parent object. 

Introduction to C++ Inheritance

The word "inherit" means to receive. In software inheritance, a new class is written such that it can access or use members of existing class. The new class that can access existing class is known as subclass or derived class (also called child class). On the other hand, the existing class that is reused is called the super class or base class (also known as parent class).
Inheritance is the most important feature of object oriented programming that allows one data type to acquire properties of other data types. It is a form of software reuse ability in which code of existing classes is used for making new classes.
Inheritance from a base class may be declared as public, protected, or private.
Chart of programming inheritance

In the above diagram, arrow is drawn from derived class to base class. The direction of the arrow indicates that derived class can access members of the base class but the base class can't access the members of its derived class. The derived class inherits all the capabilities of the base class (it can use member functions and data members of base class). It can also have its own data members and member functions. The derived class can be larger than its base class.

Categories of Inheritance

A new class can be derived from one or more existing classes based upon the number of base classes from which a class can be derived. The inheritance is divided into two categories:
  1. Single Inheritance
  2. Multiple Inheritance

Inheritance Accessibility 

The accessibility of members of the base class can be controlled by using specifiers. The access specifier determines whether unrelated and derived classes can access the inherited. These specifiers are used based upon accessing members of the base class, the inheritance accessibility is divided into three types:
  1. Public Inheritance
  2. Private Inheritance
  3. Protected Inheritance

Declaration of Derived Class Using Single Inheritance

When a derived class is defined, the base class is also connected with new derived class. There is slightly difference between declaring a derived class and base class.
The general syntax to declare derived class is:
class  d_class : spr  b_class
      body of the derived class
d_class It represents the name of derived class.
: (colon) The colon indicates that the d_class is derived from the b_class. It indicates the relationship between derived class and the base class.
spr It represents the access specifier that can be public, private or protected.
b_class It represents the name of base class that will be inherited.
Suppose, class "CodeHim" declared is as follows:
class CodeHim
      body of class CodeHim
To derive a new class "X" from existing class CodeHim (base class). The syntax are:
class X  :  public CodeHim
      body of class X
In the above syntax, class X is derived from the existing class CodeHim. So the class X is called derived class and calss CodeHim called base class.
In header of derived class declaration, the colon (:) indicates inheritance. Before the base class name, the keyword "public" indicates type of inheritance.

Inheritance Example Program

The following C++ program prepares simple result card of a student by using single inheritance.
using namespace std;
  class  student_data
          int serial;
          char name[30], phone[11];
          void input_dt (void);
          void print_dt (void);
  class  student_card :  public  student_data
          float math, chem, phys, total, avg;
          void input_marks (void);
          void result_card (void);
main ()
   student_card res;
   res . input_dt();
   res . input_marks();
   res . print_dt();
   res . result_card();
void student_data : : input_dt ()
   cout<<"Enter Student Serial Number:"<<endl;
   cout<<"Enter Student Name:"<<endl;
   cout<<"Enter Student Phone Number:"<<endl;
void student_data : : print_dt()
   cout<<"Serial: "<<serial<<endl;
   cout<<"Name: "<<name<<endl;
   cout<<Phone: "<<phone<<endl;
void student_card : : input_marks()
   cout<<"Enter Marks of Mathematics:";
   cout<<"Enter Marks of Chemistry:";
   cout<<"Enter Marks of Physics:";
   total = math+chem+phys;
   avg = total /3;
void student_card : : result_card()
   cout<<"Marks of Mathematics: "<<math<<endl;
   cout<<"Marks of Chemistry: "<<chem<<endl;
   cout<<"Marks of Physics: "<<phys<<endl;
   cout<<"Total Marks: "<<total<<endl;
   cout<<"Average Marks: "<<avg<<endl;