Categories: C++Learn C++

Hierarchical inheritance in C++ language

Hierarchical inheritance in C++

In this tutorial, we will discuss the OOP concept of Hierarchical inheritance in C++ language.
Heirarchical inheritance is multiple classes inherited from a single class. There is one parent (super) class and many children(sub) classes. Hierarchical inheritance is supported in C++. In the example below, class B, class C and class D inherits the properties of class A. Class A acts as the parant class of class B, class C and class D.
inheritance
Inheritance

Inheritance
Example
class A  //base class
{
…………………………
};
class B :access_specifier A  //class B inherits from class A
{
………………………………….
};
class C :access_specifier A  //class C inherits from class A
{
………………………………….
};
class D :access_specifier A  //class C inherits from class A
{
………………………………….
};
As seen in the diagram above, a class can have more than one child class (sub classes) and two or more children classes can have one only one (same) parent class. Here, Class A acts as the parent for sub classes: class B, class C and class D. This is known as hierarchical inheritance.
You can try to understand using the following syntex

Program 1

Example

out put

Output

In the above program, there is only one base class named A. derived classes B, C and D inherit all properties from A.

Derived classes(B, C and D) have their own properties as well as base class properties

 


Explanation above program

#include <iostream> //header file
using namespace std;
class A //paranr class A
{    // Start of class A
public:
    int k,l;// assign two variables for int
    void values() // create function for class A
    {
        k=6;
        l=5;
    }
};  // end of classA
class B:public A
{  // Start of class B
public:
    void add() // create function for class B
    {
     cout << “The sum of k,l is:”<<k+l<< endl;
    }
};// end of class B

class C:public A
// Start of class C
public:
    void sub()// create function for class C
    {
     cout << “The sub of k,l is:”<<k-l<< endl;
    }
};// end of class C
class D:public A 
{// Start of class
public:
    void mul() // create function for class D
    {
     cout << “The mul of k,l is:”<<k*l<< endl;
    }
};// end of class
int main()
{
    B obj1; // create object for class B
    C obj2;// create object for class C
    D obj3;// create object for class D

    obj1.values(); //call values function through obj1
    obj1.add();  //call add function through obj1

    obj2.values(); //call values function through obj2
    obj2.sub();//call add function through obj2

    obj3.values(); //call values function through obj3
    obj3.mul();//call add function through obj3

    return 0;

}

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

9 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

10 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

10 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

10 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

10 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

10 months ago

This website uses cookies.