Simple Multi Level Inheritance Example Program


Definition

Inheritance is when an object or class is based on another object or class, using the same implementation specifying implementation to maintain the same behaviour. It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces. The relationships of objects or classes through inheritance give rise to a hierarchy. In multilevel inheritance a subclass is inherited from another subclass. It is not uncommon that a class is derived from another derived class as shown in the figure "Multilevel Inheritance". Multilevel Inheritance The class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. The class B is known as intermediate base class since it provides a link for the inheritance between A and C.

Syntax

class A{
	//Do something
}
class B : public A{
	//Do something
}
class C : public B{
	//Do something
}

Multilevel Inheritance Example Program

// Header Files
#include<iostream>

#include<stdio.h>
#include<conio.h>

using namespace std;

class Employee {
    int eno;
    char name[20], des[20];	
    // Private members cannot call from outside class.
public:
    void getEmpDetails() {
        cout << "\nEnter the Employee number:";
        cin>>eno;
        cout << "Enter the Employee name:";
        cin>>name;
        cout << "Enter the Employee designation:";
        cin>>des;
    }
    
    void employee_display() {
    	cout <<"\nEmployee number:"<<eno;
		cout <<"\nEmployee name:"<<name;
		cout <<"\nEmployee designation:"<<des;
    }
};

class Salary : private Employee {
	//Private Base Class, We cannot access outside the dervied Class
    float bp, hra, da, pf, np;
public:

    void getPayDetails() {
    	getEmpDetails();
        cout << "Enter the Basic pay:";
        cin>>bp;
        cout << "Enter the Humen Resource Allowance:";
        cin>>hra;
        cout << "Enter the Dearness Allowance :";
        cin>>da;
        cout << "Enter the Profitablity Fund:";
        cin>>pf;
        calculate();
    }

    void calculate() {
        np = bp + hra + da - pf;
    }

    void salary_display() {
    	employee_display();
    	cout <<"\nEmployee Basic pay:"<<bp;
		cout <<"\nEmployee Humen Resource Allowance:"<<hra;
		cout <<"\nEmployee Dearness Allowance:"<<da;
		cout <<"\nEmployee Profitablity Fund:"<<pf;
		cout <<"\nEmployee Net Pay:"<<np;
    }
};

class BankCredit : private Salary {
	char bank[20], ifsc_code[20];	
	int account_number;
	public:
	void getBankDetails() {
    	getPayDetails();
        cout << "Enter the Bank Name:";
        cin>>bank;
        cout << "Enter the IFSC:";
        cin>>ifsc_code;
        cout << "Enter the Account Number :";
        cin>>account_number;
    }
    
    void display() {
    	salary_display();
    	cout <<"\nEmployee Bank Name:"<<bank;
		cout <<"\nEmployee IFSC:"<<ifsc_code;
		cout <<"\nEmployee Account Number:"<<account_number<<endl;
    }
};

int main() {
    int i, n;
    char ch;
    BankCredit s[10];
    cout << "Simple Multi Level Inheritance Example Program : Payroll System \n";
    cout << "Enter the number of employee:";
    cin>>n;
    for (i = 0; i < n; i++) {
    	cout << "\nEmployee Details # "<<(i+1)<<" : ";
        s[i].getBankDetails();
    }

    for (i = 0; i < n; i++) {
        s[i].display();
    }
    getch();
    
    return 0;
}

Sample Output

Simple Multi Level Inheritance Example Program : Payroll System
Enter the number of employee:2

Employee Details # 1 :
Enter the Employee number:101
Enter the Employee name:MASTE
Enter the Employee designation:Er
Enter the Basic pay:26000
Enter the Humen Resource Allowance:1300
Enter the Dearness Allowance :1200
Enter the Profitablity Fund:500
Enter the Bank Name:SBI
Enter the IFSC:ISFC001
Enter the Account Number :10001

Employee Details # 2 :
Enter the Employee number:102
Enter the Employee name:FORGE
Enter the Employee designation:Dr
Enter the Basic pay:32000
Enter the Humen Resource Allowance:2000
Enter the Dearness Allowance :300
Enter the Profitablity Fund:500
Enter the Bank Name:CITI
Enter the IFSC:ISFC0004
Enter the Account Number :20001

Employee number:101
Employee name:MASTE
Employee designation:Er
Employee Basic pay:26000
Employee Humen Resource Allowance:1300
Employee Dearness Allowance:1200
Employee Profitablity Fund:500
Employee Net Pay:28000
Employee Bank Name:SBI
Employee IFSC:ISFC001
Employee Account Number:10001

Employee number:102
Employee name:FORGE
Employee designation:Dr
Employee Basic pay:32000
Employee Humen Resource Allowance:2000
Employee Dearness Allowance:300
Employee Profitablity Fund:500
Employee Net Pay:33800
Employee Bank Name:CITI
Employee IFSC:ISFC0004
Employee Account Number:20001

--------------------------------
Process exited after 82.2 seconds with return value 0
Press any key to continue . . .