×

Hierarchical Inheritance

C++ Hierarchical Inheritance

Hierarchical inheritance inherits the property of one base class in more than one derived class.  

C++ Hierarchical Inheritance Example

#include <iostream>  
using namespace std;  
class Person {  
    char gender[10];  
    int age;  
public:  
    void getPerson()  
    {  
        cout << "Age: "; cin >> age;  
        cout << "Gender: "; cin >> gender;  
    }  
    void dispPerson()  
    {  
        cout << "Age: " << age << endl;  
        cout << "Gender: " << gender << endl;  
    }  
};  
class Employee : public Person {  
    float salary;  
public:  
    void getEmployee()  
    {  
        Person::getPerson();  
        cout << "Salary: Rs."; cin >> salary;  
    }  
    void dispEmployee()  
    {  
        Person::dispPerson();  
        cout << "Salary: Rs." << salary << endl;  
    }  
};  
class Student : public Person {  
    char level[20];  
public:  
    void getStudent()  
    {  
        Person::getPerson();  
        cout << "Class: "; cin >> level;  
    }  
    void dispStudent()  
    {  
        Person::dispPerson();  
        cout << "Level: " << level << endl;  
    }  
};  
int main()  
{  
    Person per;  
    Employee emp;  
    Student stu;  
    cout << "Student data" << endl;  
    cout << "Enter data" << endl;  
    stu.getStudent();  
    cout << endl << "Displaying data" << endl;  
    stu.dispPerson();  
    cout << endl << "Staff Data" << endl;  
    cout << "Enter data" << endl;  
    emp.getEmployee();  
    cout << endl << "Displaying data" << endl;  
    emp.dispPerson();  
}
Output:
Student data
Enter data
Age: 10
Gender: f
Class: 5

Displaying data
Age: 10
Gender: f

Employee data
Enter data
Age: 30
Gender: m
Salary: 25000

Display data
Age: 30
Gender: m

Related Topics

How to concatenate two strings in C++

In the C++ programming language, the concatenation of two or even more strings is covered in this section. The term "string concatenation" refers to a collection of characters that join two...

4 minutes read.

Print Table Using While Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

C++ Program For FCFS (First Come First Serve)

The most basic scheduling technique is FCFS, often known as "FIFO (First In, First Out)". In this procedure, the first one is utilized and executed first, while the second one...

4 minutes read.

Boost split in C++ library

Boost::split in C++ library Boost offers strong tools for adding mature, well-tested libraries to the C++ standard library. The boost: split function, which is a component of the Boost string algorithm...

2 minutes read.

Star pattern in C++ using For Loops

Star patterns are one of the most extensively utilized patterns in any programming language since they help to increase logical thinking and flow control understanding.In the C++ programming language, you...

3 minutes read.

Armstrong Number Program in C++

Let's first define Armstrong number before writing the C++ program to check whether the number is Armstrong or not. The sum of the cubes of its digits is equal to the...

2 minutes read.

Reverse String Word-Wise in C++

What is a reversed String? Reversing the words of a sentence is called reversed string by words. The difference between the reverse a string and the reverse a string word-wise is...

4 minutes read.

Program that produces different results in C and C++

Introduction: There are many such programs that compile run both in C and C++ but give different outcomes when compiled by the C and C++ compilers. There are a variety of such...

6 minutes read.

Divide by Zero Exception in C++

We use exception handling method to handle the divide by zero exception. Dividing a number with zero is generally mathematical error. We have to exception handling method to overcome this...

2 minutes read.

Program to find the GCD of two numbers in C++

Before understanding the program of GCD or HCF, one must know what GCD or HCF is. What is GCD? The GCD is referred to as Greatest Common Divisor. HCF is the other...

8 minutes read.

C++ array to function

Arrays in C++ : Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. An array can be declared by specifying the variable...

4 minutes read.

C++ Nested if

C++'s nested if statements enable more complex decision-making when a section of code needs to execute only after a set of conditions is met. The nested if control statement refers...

4 minutes read.

Binary Search in C++

The binary search in the C++ programming language will be discussed. By continually halves the array and then seeking specified items from a half array; binary search is a technique...

8 minutes read.

Features of OOPS in C++

What is OOPs? The main reason programmers prefer C ++ language over C is because of the support of object-oriented programing in C++. As the name suggests, object-oriented programming or OOPs...

3 minutes read.

How to call a void function in C++

Generally, any function has two types: 1. Void function: It doesn't return any value. 2. Non-void function: It returns some value. Program to call a void function in C++ #include <iostream> using namespace std;  void check() {  ...

2 minutes read.

C++ OOPs Concept

The main goal of C ++ programming is to add the idea of ​​object orientation to the C programming language. Inheritance, data binding, polymorphism and other concepts are part of...

4 minutes read.

Features and Use of Pointers in C/C++

What is a pointer? A pointer is mainly used to store the address of another variable. The * operator creates a pointer variable, which points to a data type (like an...

7 minutes read.

Pthreads or POSIX Threads in C++

The thread API for C/C++ is implemented by pthreads or POSIX threads. It enables the multithreading system, which enables parallel and distributed processing, and the creation of new concurrent process...

3 minutes read.

Continue in C++ While loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the...

4 minutes read.

C++ Installation

Let's install C++ setup to start programming in C++. C++ setup contains C++ compiler which is required in your system. There are lots of C++ compilers available, you must choose...

1 minute read.