๐ DAY 50: HALF CENTURY ๐
From "Hello World" to "Bank System Architect".
50 Days of Code. No Breaks. Pure Grind.
๐ฆ Project: The Ultimate Bank System
Today, we are not learning a new topic. We are combining Everything we have learned so far into one powerful software.
We will build a banking system where:
- Encapsulation: Customer data (Balance) is secure.
- Inheritance: We have different account types (Savings, Current) inheriting from a base Account.
- Polymorphism: The
withdraw()function works differently for Savings (Strict) vs Current (Overdraft allowed).
๐ The Architecture (Logic)
Before coding, a good developer designs the system.
1. Class: BankAccount (Parent)
- Properties:
AccountNumber,Name,Balance(Protected). - Functions:
deposit(),display().
2. Class: SavingsAccount (Child)
- Rule: Cannot withdraw if balance falls below ₹500.
3. Class: CurrentAccount (Child)
- Rule: Can withdraw extra money (Overdraft) up to ₹1000.
๐ป The Source Code
Copy this code into your IDE to run the system.
#include <iostream>
#include <string>
using namespace std;
// ------------------------------------------
// 1. ABSTRACT BASE CLASS (The Blueprint)
// ------------------------------------------
class BankAccount {
protected:
int accNum;
string name;
double balance;
public:
// Constructor
BankAccount(int a, string n, double b) {
accNum = a;
name = n;
balance = b;
}
// Common Function: Deposit
void deposit(double amount) {
balance += amount;
cout << "✅ " << amount << " Deposited. New Balance: " << balance << endl;
}
// Virtual Function (Polymorphism)
// We let the children decide how to withdraw
virtual void withdraw(double amount) = 0;
// Display Info
void display() {
cout << "\n-----------------------" << endl;
cout << "๐ค Account Holder: " << name << endl;
cout << "๐ข Account No: " << accNum << endl;
cout << "๐ฐ Balance: ₹" << balance << endl;
cout << "-----------------------" << endl;
}
};
// ------------------------------------------
// 2. SAVINGS ACCOUNT (Strict Rules)
// ------------------------------------------
class SavingsAccount : public BankAccount {
public:
// Constructor passing data to Parent
SavingsAccount(int a, string n, double b) : BankAccount(a, n, b) {}
// Overriding Withdraw Logic
void withdraw(double amount) {
if (balance - amount < 500) {
cout << "❌ Transaction Failed: Minimum Balance (₹500) rule!" << endl;
} else {
balance -= amount;
cout << "✅ Withdraw Success: ₹" << amount << endl;
}
}
};
// ------------------------------------------
// 3. CURRENT ACCOUNT (Flexible Rules)
// ------------------------------------------
class CurrentAccount : public BankAccount {
public:
CurrentAccount(int a, string n, double b) : BankAccount(a, n, b) {}
void withdraw(double amount) {
// Allow overdraft of 1000
if (balance - amount < -1000) {
cout << "❌ Transaction Failed: Overdraft Limit Exceeded!" << endl;
} else {
balance -= amount;
cout << "✅ Withdraw Success: ₹" << amount << endl;
}
}
};
// ------------------------------------------
// MAIN FUNCTION (The Driver)
// ------------------------------------------
int main() {
cout << "=== ๐ฆ WELCOME TO SAHIL BANK SYSTEM ๐ฆ ===\n" << endl;
// 1. Creating a Savings Account for Sahil
cout << "--- Creating Savings Account ---" << endl;
SavingsAccount s1(101, "Sahil Tiwari", 2000);
s1.display();
s1.deposit(500); // Add Money
s1.withdraw(2100); // Try to empty account (Should Fail due to min balance)
s1.withdraw(1000); // Valid Withdraw
// 2. Creating a Current Account for Business
cout << "\n--- Creating Current Account ---" << endl;
CurrentAccount c1(102, "Sahil Tech Solutions", 5000);
c1.display();
c1.withdraw(5500); // Withdraw more than balance (Should Pass due to Overdraft)
c1.display();
return 0;
}
๐ The Final Output
When you compile and run this code, here is what the console looks like:
=== ๐ฆ WELCOME TO SAHIL BANK SYSTEM ๐ฆ ===
--- Creating Savings Account ---
-----------------------
๐ค Account Holder: Sahil Tiwari
๐ข Account No: 101
๐ฐ Balance: ₹2000
-----------------------
✅ 500 Deposited. New Balance: 2500
❌ Transaction Failed: Minimum Balance (₹500) rule!
✅ Withdraw Success: ₹1000
--- Creating Current Account ---
-----------------------
๐ค Account Holder: Sahil Tech Solutions
๐ข Account No: 102
๐ฐ Balance: ₹5000
-----------------------
✅ Withdraw Success: ₹5500
-----------------------
๐ค Account Holder: Sahil Tech Solutions
๐ข Account No: 102
๐ฐ Balance: ₹-500
-----------------------
๐ 50 Days Complete!
We started with int a = 10; and today we built a Banking System using Polymorphism.
This is the power of consistency.
Next Mission (Day 51-100): We dive into Advanced Data Structures (Linked Lists, Stacks, Queues) and Competitive Programming. The real game begins now.
No comments:
Post a Comment