In this guide, we’ll walk you through building a simple Banking System using C++ and Object-Oriented Programming (OOP). Our goal? To build a user-friendly program that does what a basic bank account would: lets users create an account, deposit and withdraw funds, and check their balance—simple but effective.
Instead of diving into every theoretical aspect of OOP, we’re focusing on the essentials here. You’ll see how classes and objects work together in C++ to handle these core banking tasks. Let’s get started.
Why Use OOP for a Banking System?
Think about it. In a real bank, every account holds personal information, transactions, and a balance—all encapsulated within that one account. OOP in C++ works much the same way, letting us use classes and objects to mirror real-world entities. This keeps data secure and makes the program easier to manage.
Plus, OOP helps keep code organized. You get reusable, flexible code that’s perfect for expanding or upgrading in the future.
Key Features of Our Banking System
For our Simple Banking System in C++, we’ll include these features:
- Account Creation: Start with a new account setup.
- Deposit Money: A quick way to add funds.
- Withdraw Money: Secure withdrawals that can’t exceed the balance.
- Check Balance: Users can see how much they have anytime.

Code Breakdown of the Simple Banking System
Here’s the code, followed by a breakdown of each part. Each function in the program handles one of the above features, making it easy to understand and expand later if you’d like.
#include <iostream>
#include <string>
using namespace std;
// Bank Account Class
class BankAccount {
private:
int accountNumber;
string accountHolder;
float balance;
public:
// Constructor
BankAccount(int accNum, string accHolder, float initialBalance) {
accountNumber = accNum;
accountHolder = accHolder;
balance = initialBalance;
}
// Deposit Function
void deposit(float amount) {
if (amount > 0) {
balance += amount;
cout << "Deposited: $" << amount << endl;
} else {
cout << "Invalid deposit amount!" << endl;
}
}
// Withdraw Function
void withdraw(float amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
cout << "Withdrawn: $" << amount << endl;
} else if (amount > balance) {
cout << "Insufficient balance!" << endl;
} else {
cout << "Invalid withdrawal amount!" << endl;
}
}
// Check Balance Function
void checkBalance() {
cout << "Current balance: $" << balance << endl;
}
// Display Account Info
void displayAccountInfo() {
cout << "Account Number: " << accountNumber << endl;
cout << "Account Holder: " << accountHolder << endl;
cout << "Balance: $" << balance << endl;
}
};
// Main function with Menu
int main() {
int accNum;
string accHolder;
float initialBalance;
cout << "Enter Account Number: ";
cin >> accNum;
cin.ignore();
cout << "Enter Account Holder Name: ";
getline(cin, accHolder);
cout << "Enter Initial Balance: ";
cin >> initialBalance;
// Creating a BankAccount instance
BankAccount myAccount(accNum, accHolder, initialBalance);
int choice;
do {
cout << "\nBanking System Menu:\n";
cout << "1. Deposit Money\n";
cout << "2. Withdraw Money\n";
cout << "3. Check Balance\n";
cout << "4. Display Account Info\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
float depositAmount;
cout << "Enter deposit amount: ";
cin >> depositAmount;
myAccount.deposit(depositAmount);
break;
}
case 2: {
float withdrawAmount;
cout << "Enter withdrawal amount: ";
cin >> withdrawAmount;
myAccount.withdraw(withdrawAmount);
break;
}
case 3:
myAccount.checkBalance();
break;
case 4:
myAccount.displayAccountInfo();
break;
case 5:
cout << "Exiting the Banking System. Goodbye!\n";
break;
default:
cout << "Invalid choice! Please try again.\n";
}
} while (choice != 5);
return 0;
}
Breaking Down the Code
Now let’s look at each section of this Banking System in C++ and see how it all fits together:
- Class Definition:
- We start with the
BankAccount
class, which holds account details and balances in private data members. Why private? It’s for data encapsulation, which keeps account details safe from unintended access or changes outside the class.
- We start with the
- Constructor:
- The constructor initializes a new bank account with an account number, the holder’s name, and an initial balance. This sets up the account right from the start.
- Deposit Function:
- This function allows users to deposit funds into their account. It checks to make sure the amount is valid (greater than 0) before adding it to the balance.
- Withdraw Function:
- Withdrawals are only allowed if the amount is valid and doesn’t exceed the account balance. This check helps prevent overdrafts, which is critical in real-life banking.
- Check Balance:
- A quick call to
checkBalance()
shows the user’s current balance. This is a read-only function, which means it doesn’t change any data.
- A quick call to
- Display Account Info:
- For users who want a snapshot of their account, this function provides the account number, holder’s name, and balance.
- Main Menu and User Choices:
- In the main function, a menu loop lets users keep depositing, withdrawing, or checking balances until they decide to exit. The menu provides a simple way to access each feature in the system.
Final Words
Congratulations! You’ve just built a Simple Banking System in C++ using Object-Oriented Programming. This project is a great starting point for learning C++ OOP, and it demonstrates how encapsulation helps keep code secure and organized.
For extra practice, try adding more features, like account history or multiple account support. With OOP, the possibilities are endless!