Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Hierarchical inheritance in C++ language

Posted on February 18, 2017January 21, 2020

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.
Hierarchical inheritance in C++ language
inheritance
Hierarchical inheritance in C++ language
Inheritance

Hierarchical inheritance in C++ language
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

Hierarchical inheritance in C++ language
Example

out put

Hierarchical inheritance in C++ language
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;

}

Single level inheritance in java         Single level inheritance in C++
 
Multi levelinheritance in Java          Multilevel inheritance in C++
 
 Inheritance in Java                         Multiple Inheritance in C++
 
Hierarchical Inheritance in Java
Hybrid inheritance in C++
 
Interface in Java           Inheritance in C++          

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes