×

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

Desired Capabilities in Selenium Web Driver in C++

A class called Desired Capabilities is used to specify a set of fundamental requirements, such as browser, operating system, and version combinations.to carry out automated testing of a web application...

7 minutes read.

C++ Keywords

In this article, we will discuss keywords in C++ with their several features and functions. What are Keywords in C++? In C++, a keyword is a reserved word that has a predefined...

4 minutes read.

C++ Encapsulation

The following two essential components are present in all C++ programmes: Functions are the parts of a programme that perform actions, and they are termed programme statements (code).Program data is the...

4 minutes read.

Vector in C++

Vector in C++ In today’s article, we will be learning all the things about vector in C++ and how vector is different form an array in C++. So basically, vector is very...

3 minutes read.

Tensorflow in C++

With cppflow, you can execute TensorFlow models in C++ without Bazel, TensorFlow installation, or TensorFlow compilation. Tensor manipulation, eager execution, and running stored models straight from C++ are all possible. Knowing...

7 minutes read.

C++ Call by Reference

Call by Reference is a C++ method for passing arguments to a function that enables us to pass the actual memory address of the parameter rather than a copy of...

4 minutes read.

C++ Program to find largest subarray with 0 sum

Write a program to find the largest subarray that has a sum zero. The array contains positive and negative numbers. Print the length of the max subarray whose sum turns...

4 minutes read.

Single Handling in C++

Introduction: Single handling in C++ refers to a technique for processing multiple events or requests with a single function or handler rather than creating separate functions for each task. This allows...

5 minutes read.

ATM machine program in C++ using functions

Automated Teller Machines (ATMs) carry out daily financial transactions. They are straightforward and simple, allowing customers to complete self-service transactions quickly. ATMs can then be used to withdraw cash, deposit...

3 minutes read.

Pointers in C++

Pointers are a powerful feature in the C++ programming language, allowing developers to directly manipulate memory addresses and create more efficient and dynamic programs. However, pointers can also source various...

3 minutes read.

Splitting a string in C++

Any programming language must have the ability to work with string data. For programming needs, we sometimes need to separate string data. Many computer languages provide a split() method that...

4 minutes read.

LCM Program in C++

What is LCM? LCM is an acronym for "least common multiple". It is used to discover the lowest positive integer divisible by all integers (whose LCM is calculated). For instance, the...

4 minutes read.

Roadmap to C++ Programming

Introduction There are so many programming languages available in the market, but among them, C++ is something that never lost its charm. It has a powerful impact on the programming world....

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.

Palindrome using Do-while loop in C++

What is Palindrome? A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++...

5 minutes read.

Passing by Reference Vs. Passing by the pointer in C++

 Passing by Reference Vs. Passing by the pointer in C++ Throughout C++, it can transfer parameter values except by pointers or through referring to a function. For both cases, we have...

3 minutes read.

Assertions in C/C++

Assertions are the statements used to check presumptions which is made by the programmers. Example: Assertion is used to verify whether the malloc returned by the pointer is NULL or not. For...

4 minutes read.

Auto keyword in C++

The primary function of auto keyword is to assign and detect the value of the data type automatically in C++. The compiler knows the data type of the variable by...

2 minutes read.

How is multiset implemented in C++

Similar to sets, multisets are an associative container type where several items may share the same values. Associative containers implement instantly searchable sorted data structures with O(log n) complexity. In a multiset,...

5 minutes read.

C++ Signal Handling

Signals are interruptions sent by the operating system to a process to cause it to cease doing its current job and focus on the task for which the interrupt was...

4 minutes read.