Hierarchical Inheritance in C++ (With Easy Program And Example)

If we are learning the programming language, then there is definitely a
question of Inheritance

    Inheritance Type

    • Single inheritance.
    • Multi-level inheritance.
    • Multiple inheritances.
    • Multipath inheritance.
    • Hierarchical Inheritance.
    • Hybrid inheritance.

    Hierarchical Inheritance


    Hierarchical-Inheritance

    But today we will learn about Inheritance a type of Hierarchical
    inheritance, an
    example of Hierarchical inheritance and its
    syntax.

    Also, Hierarchical inheritance is compiled in this article with the diagram. After reading this article, all of the hierarchical inheritance will be cleared. which has many types.


    What is Hierarchical Inheritance?

    Inheritance is one of the most important types of OOP. Inheriting is an AC
    process by which a new class is made from the old class.

    In which old class is called derived class/child class and subclass

    And the new class is called base/super/parent class.

    Hierarchical-Inheritance-example

    Due to this process, we can use the member function and member variable of one
    class in another class.

    Inheritance occurs in two types of classes: first base class and second
    derived class.

    This process of inheritance will be easily understood through further examples
    and diagrams.

    First, let us know what is the derived class and base class.

    What is the Base class?

    The base class also has a superclass and parent class. The base class
    contains member functions and member variables that can also be used in
    derived classes.

    What is a derived class?

    The derived class superclass also contains child classes and subclasses. Derived class access member of the base class.

    Hierarchical inheritance has a base class and has many derived classes.
    This is a very important inheritance in C ++.

    In this diagram has three derived classes and one base class.

    Syntax of Hierarchical Inheritance

    class base_classname

    {

        properties;

        methods;

    };

    class derived_class1 : visibility_mode base_classname

    {

        properties;

        methods;

    };

    class derived_class2 : visibility_mode base_classname

    {

        properties;

        methods;

    };

    class derived_classN : visibility_mode base_classname

    {

        properties;

        methods;

    };

     

    explain

    This syntax of hierarchical inheritance has a base class and is a base class.
    Members of this one class can access two derived classes.

    It may contain more than one derived class.

    Syntax of the Derived Class

    class derived_class name: visibility_mode base_classname

    What is visibility mode?

    Visibility mode is an important control in C ++ programming, it is also called
    accessibility mode and access specifier.

    It is used to specify which level of
    data member and
    member function to be accessed.

    There are three types of visibility mode

    1. Public
    2. Protected
    3. Private

    Examples of Hierarchical Inheritance

    // hierarchial inheritance.cpp

    #include <iostream>

    using namespace std;

    class base_class           //single base class

    {

        public:

        int a,b ;

        void getdata()

      {

            cout << “nEnter value of a” ;

         cin>>a;

         cout << “nEnter value of b” ;

         cin>>b;

      }

    };

    class derived_class1 : public base_class       
     //derived_class1 is derived from base_class

    {

        public:

        void add()

      {

          cout<< “Addition of a and b : ” <<a+b;

     }

    };

    class derived_class2 : public base_class       
     //derived_class2 is derived from base_class

    {

        public:

        void sum()

      {

            cout<< “subtraction of a and b : ”
    <<a-b;

      }

    };

    int main()

    {

       derived_class1 obj1;          //object
    of derived class1

       derived_class2 obj2;          //object
    of derived class2

       obj1.getdata();

       obj1.add();

       obj2.getdata();

       obj2.sum();

    }  //end of program

    In this program, declaring A and B once and using them in different derived
    classes.

    Output

    Enter the value of a:        

    Enter the value of b:

    4                   
                         
                         
                  

    5

    Addition of a and b: 9

    Enter a value of a:               
                         
                       
      

    Enter a value of b:

    6

    5

    subtraction of a and b: 1 

    Simple Hierarchical Inheritance Progam of C++


    #include<iostream>

    using namespace std;

    class base{

      public:

      void display_book(){

     cout<<“nThis is book”;

      }

    };

    class derived_class1 : public base{

        public:

        void display_ds(){

          cout<<“nThis is data structure book”; 
      

        }

    };

    class derived_class2 : public base{

        public:

        void display_cpp(){

            cout<<“nThis is cpp book”;

        }

    };

    int main(){

       base b;

       b.display_book();

       derived_class1 d1;

       d1.display_book();

       d1.display_ds();

       derived_class2 d2;

       d2.display_book();

       d2.display_cpp();

      return 0;

    }

     

    Output

    This is book 

    This is the data structure book         
      

    This is book                 
                

    This is cpp book 

    0 thoughts on “Hierarchical Inheritance in C++ (With Easy Program And Example)”

    Leave a comment