Here’s a Bank Management System in C++ using Classes and Inheritance. This program demonstrates how inheritance can be used to manage different types of bank accounts.
Features of the Program
✅ Base Class (BankAccount
) - Holds common account details
✅ Derived Classes (SavingsAccount
, CurrentAccount
) - Inherit and extend functionalities
✅ Deposits & Withdrawals with balance checks
✅ Interest Calculation for Savings Account
✅ Overdraft Protection for Current Account
1️⃣ C++ Program for Bank Management System
#include <iostream>
using namespace std;
// Base Class: Bank Account
class BankAccount {
protected:
string accountHolder;
long accountNumber;
double balance;
public:
// Constructor
BankAccount(string name, long accNum, double initialBalance) {
accountHolder = name;
accountNumber = accNum;
balance = initialBalance;
}
// Display Account Details
void displayAccountDetails() {
cout << "\nAccount Holder: " << accountHolder << endl;
cout << "Account Number: " << accountNumber << endl;
cout << "Current Balance: $" << balance << endl;
}
// Deposit Money
void deposit(double amount) {
balance += amount;
cout << "$" << amount << " deposited successfully. New Balance: $" << balance << endl;
}
// Virtual Withdraw Function (Overridden in Derived Classes)
virtual void withdraw(double amount) {
if (amount > balance) {
cout << "Insufficient balance!\n";
} else {
balance -= amount;
cout << "$" << amount << " withdrawn successfully. Remaining Balance: $" << balance << endl;
}
}
};
// Derived Class: Savings Account
class SavingsAccount : public BankAccount {
private:
double interestRate;
public:
SavingsAccount(string name, long accNum, double initialBalance, double rate)
: BankAccount(name, accNum, initialBalance), interestRate(rate) {}
// Apply Interest
void applyInterest() {
double interest = (balance * interestRate) / 100;
balance += interest;
cout << "Interest of $" << interest << " added. New Balance: $" << balance << endl;
}
};
// Derived Class: Current Account
class CurrentAccount : public BankAccount {
private:
double overdraftLimit;
public:
CurrentAccount(string name, long accNum, double initialBalance, double limit)
: BankAccount(name, accNum, initialBalance), overdraftLimit(limit) {}
// Override Withdraw to Allow Overdraft
void withdraw(double amount) override {
if (amount > (balance + overdraftLimit)) {
cout << "Overdraft limit exceeded! Cannot withdraw $" << amount << endl;
} else {
balance -= amount;
cout << "$" << amount << " withdrawn successfully. Remaining Balance: $" << balance << endl;
}
}
};
// Main Function
int main() {
// Creating Savings Account
SavingsAccount savings("Alice Johnson", 1001, 5000.0, 5.0);
savings.displayAccountDetails();
savings.deposit(2000);
savings.applyInterest();
savings.withdraw(3000);
cout << "\n-----------------------------\n";
// Creating Current Account
CurrentAccount current("Bob Smith", 2002, 3000.0, 1000.0);
current.displayAccountDetails();
current.deposit(1500);
current.withdraw(4000);
current.withdraw(2000); // Exceeds overdraft limit
return 0;
}
2️⃣ Output of the Program
Account Holder: Alice Johnson
Account Number: 1001
Current Balance: $5000
$2000 deposited successfully. New Balance: $7000
Interest of $350 added. New Balance: $7350
$3000 withdrawn successfully. Remaining Balance: $4350
-----------------------------
Account Holder: Bob Smith
Account Number: 2002
Current Balance: $3000
$1500 deposited successfully. New Balance: $4500
$4000 withdrawn successfully. Remaining Balance: $500
Overdraft limit exceeded! Cannot withdraw $2000
3️⃣ Explanation of the Code
✅ Class BankAccount
(Base Class)
- Stores common details: accountHolder, accountNumber, balance
- Implements deposit() and withdraw() functions
withdraw()
is virtual to allow overriding
✅ Class SavingsAccount
(Derived from BankAccount
)
- Has an interest rate
- Implements applyInterest() to add interest
✅ Class CurrentAccount
(Derived from BankAccount
)
- Allows overdraft up to a limit
- Overrides
withdraw()
to enable overdraft
4️⃣ Advantages of Using Inheritance
✅ Code Reusability - Common features in BankAccount
are inherited by SavingsAccount
and CurrentAccount
.
✅ Encapsulation - Account details are protected with protected
access specifier.
✅ Polymorphism - withdraw()
is overridden in CurrentAccount
.
5️⃣ Possible Enhancements
🔹 Add ATM PIN authentication.
🔹 Implement File Handling to save and retrieve account details.
🔹 Add More Account Types like Fixed Deposit or Loan Accounts.
Would you like me to add File Handling or an Interactive Menu System? 🚀
0 Comments