Recap - Bank Account Manager
Part of the Object Oriented Programming section of Coddy's C++ journey — lesson 33 of 104.
Challenge
EasyLet'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 aBankAccountclass that manages account data with proper encapsulation. Your class should have:- Private members:
accountNumber(string),ownerName(string),balance(double), and amutable int queryCountto track how many times the balance has been checked - A private static member
totalAccountsto track how many accounts exist - A constructor that takes the account number, owner name, and initial balance. It should initialize
queryCountto0and incrementtotalAccounts - Const getters:
getAccountNumber(),getOwnerName(), andgetBalance()— the balance getter should incrementqueryCounteach 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.
- Private members:
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
BankAccountobjects 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.0into the first account - Print
"After deposit - <name>: "After deposit - <name>: $<balance>"lt;balance>"for the first account - Transfer
300.0from 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
- Create two
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
1Fundamentals of OOP
External FilesC++ Build & CompilationHeader Files & Source FilesNamespaces & ScopeIntroduction to OOP in C++Classes vs ObjectsThe 'this' PointerMethods (Member Functions)Attributes (Data Members)Ctors & Dtors BasicsRecap - Simple Calculator4Class Properties
Instance vs Static MembersGetters and SettersConst Member FunctionsMutable KeywordStatic Methods and VariablesFriend Functions & ClassesRecap - Bank Account Manager7Inheritance
Basic InheritanceInheritance Access LevelsCtor & Dtor Call OrderMethod OverridingVirtual Functions & VTableMultiple InheritanceVirtual InheritanceRecap - Employee Hierarchy2Memory Management
Stack vs Heap MemoryPointers and ReferencesDynamic Memory (new/delete)Smart Pointers in C++RAII in C++Recap - Dynamic Array Manager5Encapsulation
Access Specifiers in C++Access Specifiers In DepthInformation HidingStruct vs ClassNested & Inner ClassesRecap - Student Records System8Polymorphism
Compile vs Runtime PolymorphFunction OverloadingVirtual Functions RevisitedPure Virtual FunctionsAbstract ClassesInterface Design in C++Dynamic Casting & RTTIRecap - Shape Calculator3Constructors & Destructors
Default ConstructorParameterized ConstructorCopy ConstructorMove ConstructorConstructor Init ListsDelegating ConstructorsDestructor Deep DiveRule of Three / Five / ZeroRecap - String Class6Operator Overloading
Intro to Operator OverloadArithmetic Operator OverloadComparison Operator OverloadStream OperatorsAssignment Operator Overload[] and () Operator OverloadType Conversion OperatorsRecap - Matrix Class9Templates
Function TemplatesClass TemplatesTemplate SpecializationVariadic TemplatesSFINAE & Type Traits BasicsRecap - Generic Container