Multilevel Inheritance in C++

Multilevel inheritance in C++

Multilevel Inheritance is a type of inheritance in which derived class is created from another derived class.

Let’s clear our understanding by taking the following example :-

Design three classes STUDENT, EXAM and RESULT. The STUDENT class has data members such as rollno, name. Create a class EXAM by inheriting the STUDENT class. The EXAM class adds data members representing the marks scored in six subjects. Derive the RESULT from the EXAM class and has its own data members such as totalmarks. Write a program to model this relationship.

In the following example, Result is derived from class Exam and Exam class is also derived from Student.

Student -> Exam -> Result (Here Exam is the intermediator class and base class for result)

Multilevel inheritance in C++

//Multilevel inheritance 
using namespace std;

// Parent/Base class
class Student{
        int rollno;
        string name;
        void read(){
            cout << "Enter Roll number: ";
            cin >> rollno;
            cout << "Enter Name: ";
            cin >> name;
        void display(){
            cout << "Name: " << name << endl;
            cout << "Roll No.: " << rollno << endl;

//Derived class (from student)	
class Exam : public Student{
        int marks1;
        int marks2;
        int marks3;
        int marks4;
        int marks5;
        int marks6;
        void readMarks(){
            cout << "Enter the Marks of Student: " << endl;
            cout << "Marks1: ";
            cin >> marks1;
            cout << "Marks2: ";
            cin >> marks2;
            cout << "Marks3: ";
            cin >> marks3;
            cout << "Marks4: ";
            cin >> marks4;
            cout << "Marks5: ";
            cin >> marks5;
            cout << "Marks6: ";
            cin >> marks6;

// It becomes base class for result
class Result : public Exam{
        int totalMarks;
        float percentage;
    void readStudent(){
        cout << "\n\n";
    void process(){
        totalMarks = marks1+marks2+marks3+marks4+marks5+marks6;
        percentage = (float(totalMarks)/600)*100;
    void displayResult(){
        cout << "Total Marks student obtained: ";
        cout << totalMarks << endl;
        cout << "Total Percentage: ";
        cout << percentage << endl;

int main(){
    Result n;
    n.readStudent(); //Object must be created of final class i.e Result in this case
    return 0;


Enter Roll number: 37
Enter Name: Naman
Enter the Marks of Student:
Marks1: 78
Marks2: 98
Marks3: 88
Marks4: 67
Marks5: 72
Marks6: 73

Name: Naman
Roll No.: 37
Total Marks student obtained: 476
Total Percentage: 79.3333

Leave a Reply