C++ Tutorial
C++ Project: Bank System
An Account class with deposit, withdraw, and a printed statement. Balance never goes negative.
What you will build
A tiny bank is one object: an Account. The balance is private. Callers deposit, withdraw, and print a statement. They never assign the balance from outside the class. A withdrawal that would drop below zero is refused, and the old balance stays.
Demo amounts are literals in main, so the program does not wait on cin. Compile it with Try it in C++ at /cpp/try, not at the Python or HTML editors.
Private balance
Put balance under private:. Methods of Account can still read and write it. Code in main cannot. That is how the class enforces the no-negative rule in one place instead of hoping every caller remembers it.
Example
#include <iostream>
using namespace std;
class Account {
private:
double balance;
public:
Account(double start) {
if (start < 0.0) {
balance = 0.0;
} else {
balance = start;
}
}
double getBalance() const {
return balance;
}
};
int main() {
Account a(50.0);
cout << a.getBalance() << endl;
return 0;
}Output is 50. A starting value below zero is stored as 0. There is still no deposit or withdraw. Those come next.
Deposit
Deposit adds a positive amount. Zero or a negative number is ignored so a caller cannot use deposit to shrink the balance. The method returns nothing; the next print shows the new total.
Example
#include <iostream>
using namespace std;
class Account {
private:
double balance;
public:
Account(double start) {
balance = (start < 0.0) ? 0.0 : start;
}
void deposit(double amount) {
if (amount > 0.0) {
balance = balance + amount;
}
}
double getBalance() const {
return balance;
}
};
int main() {
Account a(50.0);
a.deposit(25.0);
a.deposit(-5.0);
cout << a.getBalance() << endl;
return 0;
}Output is 75. The negative deposit did not apply. Only 25 was added to 50.
Open this at /cpp/try with Try it in C++. Change the starting balance to 0 and deposit 10 twice. You should see 20.
Withdraw without going negative
Withdraw succeeds only when the amount is positive and not larger than the current balance. Returntrue on success so main can print whether the cash left the account. On failure, leave balance unchanged.
Example
#include <iostream>
using namespace std;
class Account {
private:
double balance;
public:
Account(double start) {
balance = (start < 0.0) ? 0.0 : start;
}
bool withdraw(double amount) {
if (amount <= 0.0) {
return false;
}
if (amount > balance) {
return false;
}
balance = balance - amount;
return true;
}
double getBalance() const {
return balance;
}
};
int main() {
Account a(50.0);
if (a.withdraw(20.0)) {
cout << "Withdrew 20" << endl;
} else {
cout << "Withdraw 20 failed" << endl;
}
if (a.withdraw(40.0)) {
cout << "Withdrew 40" << endl;
} else {
cout << "Withdraw 40 failed" << endl;
}
cout << "Balance " << a.getBalance() << endl;
return 0;
}20 leaves the account. 40 would require 70 and the balance is then 30, so that call fails. The last line prints Balance 30.
Complete program with a statement
print is a method. It reads the private balance and writes a short statement. Include<iomanip> so money shows two digits after the decimal. main runs a fixed sequence: open, deposit, two withdrawals, print.
Example
#include <iostream>
#include <iomanip>
using namespace std;
class Account {
private:
double balance;
public:
Account(double start) {
balance = (start < 0.0) ? 0.0 : start;
}
void deposit(double amount) {
if (amount > 0.0) {
balance = balance + amount;
}
}
bool withdraw(double amount) {
if (amount <= 0.0 || amount > balance) {
return false;
}
balance = balance - amount;
return true;
}
void print() const {
cout << fixed << setprecision(2);
cout << "Statement" << endl;
cout << "Balance: $" << balance << endl;
}
};
int main() {
Account checking(100.00);
checking.deposit(40.00);
if (!checking.withdraw(25.50)) {
cout << "First withdraw refused" << endl;
}
if (!checking.withdraw(200.00)) {
cout << "Overdraft refused" << endl;
}
checking.print();
return 0;
}Start at 100.00, add 40.00, take 25.50. The 200.00 withdrawal is refused. The statement showsBalance: $114.50. You cannot write checking.balance = -1; frommain; the field is private.
Common mistakes
- Leaving
balancepublic. Then any line can set it to a negative number and the class rules become decoration. - Subtracting first and checking the result afterward. If you write
balance = balance - amountbefore the test, a refused withdraw already damaged the value. - Treating a failed withdraw as success because you forgot to return
falseon theamount > balancepath. - Printing money without
fixedandsetprecision(2).114.5looks like a truncated figure;114.50reads as currency.
Practice
- Add a method
getBalanceif you removed it, and print the balance after every successful deposit. - Refuse a deposit above 10,000 and print that the bank cap was hit.
- Hold two
Accountobjects, transfer 15.00 from one to the other only when withdraw succeeds, then print both statements.
Next project: a scored quiz stored as a vector of questions.