Menu
Coddy logo textTech

Recap - Bank Account Manager

Part of the Object Oriented Programming section of Coddy's C++ journey — lesson 33 of 104.

challenge icon

Challenge

Easy

Let's build a complete Bank Account Manager that brings together all the class property concepts you've learned in this chapter: private members with getters and setters, const member functions, mutable members, static variables and methods, and friend functions.

You'll create two files to organize your code:

  • BankAccount.h: Define a BankAccount class that manages account data with proper encapsulation. Your class should have:
    • Private members: accountNumber (string), ownerName (string), balance (double), and a mutable int queryCount to track how many times the balance has been checked
    • A private static member totalAccounts to track how many accounts exist
    • A constructor that takes the account number, owner name, and initial balance. It should initialize queryCount to 0 and increment totalAccounts
    • Const getters: getAccountNumber(), getOwnerName(), and getBalance() — the balance getter should increment queryCount each time it's called
    • A const getter getQueryCount() that returns how many times the balance was checked
    • A deposit(double amount) method that adds to the balance only if the amount is positive
    • A withdraw(double amount) method that subtracts from the balance only if the amount is positive and doesn't exceed the current balance
    • A static method getTotalAccounts() that returns the total number of accounts created
    • Declare a friend function transfer(BankAccount& from, BankAccount& to, double amount) that moves money between accounts — it should only transfer if the amount is positive and the source has sufficient funds

    Define the static member and the friend function after the class definition.

  • main.cpp: Demonstrate all these features working together. Read two owner names and two initial balances from input (four lines total: name1, balance1, name2, balance2), then:
    • Create two BankAccount objects with account numbers "ACC001" and "ACC002"
    • Print "Total accounts: <count>"
    • Print "<name>: "<name>: $<balance>"lt;balance>" for the first account
    • Print "<name>: "<name>: $<balance>"lt;balance>" for the second account
    • Deposit 500.0 into the first account
    • Print "After deposit - <name>: "After deposit - <name>: $<balance>"lt;balance>" for the first account
    • Transfer 300.0 from the first account to the second using the friend function
    • Print "After transfer:"
    • Print " <name>: " <name>: $<balance>"lt;balance>" for the first account
    • Print " <name>: " <name>: $<balance>"lt;balance>" for the second account
    • Print "Query count for <name>: <count>" for the first account

Format all balance values with two decimal places using std::fixed and std::setprecision(2) from <iomanip>. Convert balance inputs from strings to doubles using std::stod().

Don't forget header guards in your header file and to include <string> and <iostream> where needed.

Try it yourself

#include <iostream>
#include <string>
#include <iomanip>
#include "BankAccount.h"

int main() {
    // Read input
    std::string name1, name2;
    std::string balance1Str, balance2Str;
    
    std::getline(std::cin, name1);
    std::getline(std::cin, balance1Str);
    std::getline(std::cin, name2);
    std::getline(std::cin, balance2Str);
    
    double balance1 = std::stod(balance1Str);
    double balance2 = std::stod(balance2Str);
    
    // Set output formatting for 2 decimal places
    std::cout << std::fixed << std::setprecision(2);
    
    // TODO: Create two BankAccount objects with account numbers "ACC001" and "ACC002"
    
    // TODO: Print "Total accounts: <count>"
    
    // TODO: Print "<name>: $<balance>" for the first account
    
    // TODO: Print "<name>: $<balance>" for the second account
    
    // TODO: Deposit 500.0 into the first account
    
    // TODO: Print "After deposit - <name>: $<balance>" for the first account
    
    // TODO: Transfer 300.0 from the first account to the second using the friend function
    
    // TODO: Print "After transfer:"
    
    // TODO: Print "  <name>: $<balance>" for the first account (note the 2-space indent)
    
    // TODO: Print "  <name>: $<balance>" for the second account (note the 2-space indent)
    
    // TODO: Print "Query count for <name>: <count>" for the first account
    
    return 0;
}

All lessons in Object Oriented Programming