Summary: In this tutorial, we will learn what multilevel inheritance is and how can we implement it in C++.

Introduction to Multilevel Inheritance

The inheriting of the derived class in another class is multilevel inheritance.

In multilevel inheritance, we inherit the class that has already inherited another class.

Example of Multilevel Inheritance using the Block Diagram:

multilevel inheritance

The level of multilevel inheritance increases as more class joins the chain.

The main advantage of multilevel inheritance is that it allows the flow of resources to the depths of the hierarchy of classes.

Syntax for Multilevel Inheritance:

class Base_Class{
    //Member Methods
};
 
class Derived_Class_I: public Base_Class{
    //Member Methods
};
 
class Derived_Class II: public Derived_Class_I{
    //Member Methods
};

In this syntax, we have implemented three-level inheritance. However, we can increase the level by including more classes.

C++ Example: Multilevel Inheritance

For Example, the Monthly Average Saving Account is one type of Saving Account, but the Saving Account itself is a type of Account in any bank.

Thus, some properties of the parent bank account are used in the savings account and the savings account properties are used in the monthly average savings account.

Therefore, it is not wrong to say that ‘Monthly Average Saving Account’ is an ‘Account’.

In such cases multilevel inheritance comes handy because in multilevel inheritance, a class can have more than one parent class.

Here is an example in C++ demonstrating the same:

#include <iostream>
using namespace std;
 
//Base Class
class Account{
public:
    double balance;
 
    Account(){
        balance = 0;
    }
 
    Account(int bal){
        balance = bal;
    }
 
    void withdraw(double amt){
        balance -= amt;
        cout << amt<<" Withdraw Successful \n";
    }
 
    void deposit(double amt){
        balance += amt;
        cout << amt<<" Deposit Successful \n";
    }
 
    void display_balance(){
        cout << "Current Balance: "<<balance<<endl;
    }
};
 
//Derived class I
class Saving_Account : public Account{
public:
    double interest_rate;
 
    Saving_Account(){
        interest_rate = 4.5;
    }
 
    Saving_Account(double bal){
        balance = bal;
        interest_rate = 4.5;
    }
 
    Saving_Account(double bal,double rate){
        balance = bal;
        interest_rate = rate;
    }
 
    void calculate_interest(int years){
        balance += (balance * interest_rate/100 *years);
    }
};
 
 
//Derived Class II
class Monthly_Average_Saving_Account: public Saving_Account{
public:
    double average_balance, fine;
 
    Monthly_Average_Saving_Account(){
        average_balance = 99;
        fine = 10;
    }
 
    Monthly_Average_Saving_Account(double bal){
        balance = bal;
        fine = 10;
    }
 
    Monthly_Average_Saving_Account(double bal,double avg){
        balance = bal;
        average_balance = avg;
        fine = 10;
    }
 
 
    void check_average(){
        if(balance < average_balance){
            cout << "Balance is Low \n";
            balance -= fine;
        } else{
            cout <<"Balance Above Average \n";
        }
    }
};
 
int main()
{
    Monthly_Average_Saving_Account account(900);
    account.display_balance();
    account.deposit(100);
    account.display_balance();
    account.withdraw(700);
    account.display_balance();
    account.check_average();
 
    return 0;
}

Output:

Example of Multilevel Inheritance

In this tutorial, we learned with examples the significance of multilevel inheritance in C++.

Leave a Reply