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

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.