Multilevel inheritance is a process of deriving a class from another derived class.
When one class inherits another class which is further inherited by another class, it is known as multi level inheritance in C++. Inheritance is transitive so the last derived class acquires all the members of all its base classes.
Bank Account System
Base class (Bank Account ) - basic functionalities like deposit and withdrawal.
Derived class (SavingsAccount) - Inherits from Bank Account and adds an interest rate feature.
Further Derived Class (FixedDepositAccount): Inherits from SavingsAccount and introduces a fixed deposit feature with a maturity period.
Example
#include <iostream>
using namespace std;
// Base Class: BankAccount
class BankAccount {
protected:
string accountNumber;
string accountHolder;
double balance;
public:
// Constructor
BankAccount(string accNo, string accHolder, double bal) {
accountNumber = accNo;
accountHolder = accHolder;
balance = bal;
}
// Method to deposit money
void deposit(double amount) {
balance += amount;
cout << "Deposited: " << amount << ". New Balance: " << balance << endl;
}
// Method to withdraw money
void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
cout << "Withdrawn: " << amount << ". Remaining Balance: " << balance << endl;
} else {
cout << "Insufficient balance!" << endl;
}
}
// Display account details
void display() {
cout << "Account Number: " << accountNumber << endl;
cout << "Account Holder: " << accountHolder << endl;
cout << "Balance: " << balance << endl;
}
};
// Derived Class: SavingsAccount (inherits from BankAccount)
class SavingsAccount : public BankAccount {
protected:
double interestRate;
public:
// Constructor
SavingsAccount(string accNo, string accHolder, double bal, double rate)
: BankAccount(accNo, accHolder, bal) {
interestRate = rate;
}
// Method to apply interest
void applyInterest() {
double interest = balance * interestRate / 100;
balance += interest;
cout << "Interest Applied: " << interest << ". New Balance: " << balance << endl;
}
};
// Further Derived Class: FixedDepositAccount (inherits from SavingsAccount)
class FixedDepositAccount : public SavingsAccount {
private:
int maturityPeriod; // in months
public:
// Constructor
FixedDepositAccount(string accNo, string accHolder, double bal, double rate, int period)
: SavingsAccount(accNo, accHolder, bal, rate) {
maturityPeriod = period;
}
// Method to display fixed deposit details
void displayFixedDepositDetails() {
cout << "Fixed Deposit Details:" << endl;
display();
cout << "Interest Rate: " << interestRate << "%" << endl;
cout << "Maturity Period: " << maturityPeriod << " months" << endl;
}
};
// Main Function
int main() {
string accNo, accHolder;
double balance, interestRate, depositAmount, withdrawAmount;
int maturityPeriod;
// Taking user input for account details
cout << "Enter Account Number: ";
cin >> accNo;
cout << "Enter Account Holder Name: ";
cin>>accHolder;
cout << "Enter Initial Balance: ";
cin >> balance;
cout << "Enter Interest Rate (%): ";
cin >> interestRate;
cout << "Enter Maturity Period (in months): ";
cin >> maturityPeriod;
// Creating an object of FixedDepositAccount with user input
FixedDepositAccount fdAccount(accNo, accHolder, balance, interestRate, maturityPeriod);
// Display initial details
fdAccount.displayFixedDepositDetails();
// Performing deposit operation
cout << "\nEnter Amount to Deposit: ";
cin >> depositAmount;
fdAccount.deposit(depositAmount);
// Applying interest
cout << "\nApplying Interest..." << endl;
fdAccount.applyInterest();
// Performing withdrawal operation
cout << "\nEnter Amount to Withdraw: ";
cin >> withdrawAmount;
fdAccount.withdraw(withdrawAmount);
// Display final account details
cout << "\nFinal Account Details:";
fdAccount.displayFixedDepositDetails();
return 0;
}
Output:
Enter Account Number: 112401
Enter Account Holder Name: Prasanjeet
Enter Initial Balance: 4000
Enter Interest Rate (%): 6
Enter Maturity Period (in months): 12
Fixed Deposit Details:
Account Number: 112401
Account Holder: Prasanjeet
Balance: 4000
Interest Rate: 6%
Maturity Period: 12 months
Enter Amount to Deposit: 2000
Deposited: 2000. New Balance: 6000
Applying Interest...
Interest Applied: 360. New Balance: 6360
Enter Amount to Withdraw: 6000
Withdrawn: 6000. Remaining Balance: 360
Final Account Details: Fixed Deposit Details:
Account Number: 112401
Account Holder: Prasanjeet
Balance: 360
Interest Rate: 6%
Maturity Period: 12 months
Process returned 0 (0x0) execution time : 129.085 s
Press any key to continue.
No comments:
Post a Comment