Introduction
In this tutorial, we will create a simple Bank Account Simulator using C++. The goal is to simulate the basic functionalities of a bank account, such as deposit, withdrawal, and checking the account balance. This program is an excellent beginner project for learning object-oriented programming concepts in C++.
Objective
The objective of this project is to design a simple bank account management system where a user can:
- Deposit money into their account
- Withdraw money from their account
- Check their account balance
By implementing this, you’ll get hands-on experience with classes, methods, and basic data handling in C++.
Code Implementation
#include using namespace std; class BankAccount { private: double balance; public: // Constructor to initialize balance BankAccount(double initial_balance) { if (initial_balance >= 0) { balance = initial_balance; } else { balance = 0; cout << "Invalid initial balance. Setting balance to 0." << endl; } } // Method to deposit money into the account void deposit(double amount) { if (amount > 0) { balance += amount; cout << "Deposited: $" << amount << endl; } else { cout << "Deposit amount must be positive." << endl; } } // Method to withdraw money from the account void withdraw(double amount) { if (amount > 0 && amount <= balance) { balance -= amount; cout << "Withdrawn: $" << amount << endl; } else { cout << "Insufficient funds or invalid withdrawal amount." << endl; } } // Method to check the current balance void check_balance() const { cout << "Current balance: $" << balance << endl; } }; int main() { BankAccount account(1000); // Create an account with an initial balance of $1000 int choice; double amount; do { cout << "\nBank Account Simulator Menu" << endl; cout << "1. Deposit Money" << endl; cout << "2. Withdraw Money" << endl; cout << "3. Check Balance" << endl; cout << "4. Exit" << endl; cout << "Enter your choice: "; cin >> choice; switch (choice) { case 1: cout << "Enter amount to deposit: $"; cin >> amount; account.deposit(amount); break; case 2: cout << "Enter amount to withdraw: $"; cin >> amount; account.withdraw(amount); break; case 3: account.check_balance(); break; case 4: cout << "Exiting... Thank you!" << endl; break; default: cout << "Invalid choice. Please try again." << endl; } } while (choice != 4); return 0; }
Explanation of Program Structure
The program begins by defining a BankAccount
class that contains the following:
- Private Data Member:
balance
– stores the current balance of the account. - Constructor: Initializes the balance to a given amount if valid, or sets it to 0 if invalid.
- Methods:
deposit(double amount)
: Adds a specified amount to the balance.withdraw(double amount)
: Subtracts the specified amount from the balance if sufficient funds are available.check_balance()
: Displays the current account balance.
The main()
function handles user interaction. It provides a menu for the user to choose actions like deposit, withdrawal, or balance check. A loop keeps the program running until the user chooses to exit.
How to Run the Program
To run this program, follow these steps:
- Ensure you have a C++ compiler installed (e.g., GCC or any IDE that supports C++ like Code::Blocks or Visual Studio).
- Copy and paste the code into a file with a
.cpp
extension, for example,BankAccountSimulator.cpp
. - Compile the code using your C++ compiler:
g++ BankAccountSimulator.cpp -o BankAccountSimulator
- Run the program:
./BankAccountSimulator
- Follow the menu prompts to simulate a bank account.