Skip to main content
Engineering LibreTexts

16.1: Inheritance

  • Page ID
    29126
  • Inheritance in C++

    The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important feature of Object Oriented Programming.
    Sub Class: The class that inherits properties from another class is called Sub class or Derived Class - also often referred to as the child class.
    Super Class:The class whose properties are inherited by sub class is called Base Class or Super class - also often referred to as the parent class.

    The article is divided into following subtopics:

    1. Why and when to use inheritance?
    2. Modes of Inheritance
    3. Types of Inheritance

    Why and when to use inheritance?

    Consider a group of vehicles. You need to create classes for Bus, Car and Truck. The methods fuelAmount(), capacity(), applyBrakes() will be same for all of the three classes. If we create these classes avoiding inheritance then we have to write all of these functions in each of the three classes as shown in below figure:

    inheritance

    You can clearly see that above process results in duplication of same code 3 times. This increases the chances of error and data redundancy. To avoid this type of situation, inheritance is used. If we create a class Vehicle and write these three functions in it and inherit the rest of the classes from the vehicle class, then we can simply avoid the duplication of data and increase re-usability. Look at the below diagram in which the three classes are inherited from vehicle class:

    inheritance2
    Using inheritance, we have to write the functions only one time instead of three times as we have inherited rest of the three classes from base class(Vehicle).

    Implementing inheritance in C++: For creating a sub-class which is inherited from the base class we have to follow the below syntax.

    class child_class_name : access_mode parent_class_name
    {
      //body of child_class
    };
    

    Here, child_class_name is the name of the sub class, access_mode is the mode in which you want to inherit this sub class for example: public, private etc. and parent_class_name is the name of the base class from which you want to inherit the sub class.

    Note: A derived class doesn’t inherit access to private data members. However, it does inherit a full parent object, which contains any private members which that class declares.

    // C++ program to demonstrate implementation of Inheritance
    #include <bits/stdc++.h>
    using namespace std;
    
    //Base class
    class School
    {
        public:
            string name_p;
    };
    
    // Sub class inheriting from Base Class(School)
    class Department : public School
    {
        public:
            string name_c;
    };
    
    //main function
    int main()
    {   
            Department deptObj;
            
            // An object of class child has all data members
            // and member functions of class parent
            deptObj.name_c = "Computer Science";
            deptObj.name_p = "Delta College";
            
            cout << "Child name is " << deptObj.name_c << endl;
            cout << "Parent name is " << deptObj.name_p << endl;
            
            return 0;
    }
    
    

    In the code there is a parent class called School, and the child class which is Department. We could have a lot more variable and methods in the parent class, but we are trying to keep this simple. The child class inherits the methods and variables of the parent class - EXCEPT - the child class can not access a parents private variables or methods.

     

    Output:

    Child name is Computer Science
    Parent name is Delta College
    

    In the above program the ‘Child’ class is publicly inherited from the ‘Parent’ class so the public data members of the class ‘Parent’ will also be inherited by the class ‘Child’.

    Adapted from:
    "Inheritance in C++" by Harsh AgarwalGeeks for Geeks is licensed under CC BY-SA 4.0