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 balance public. 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 writebalance = balance - amount before the test, a refused withdraw already damaged the value.
  • Treating a failed withdraw as success because you forgot to return false on theamount > balance path.
  • Printing money without fixed and setprecision(2). 114.5 looks like a truncated figure; 114.50 reads as currency.

Practice

  1. Add a method getBalance if you removed it, and print the balance after every successful deposit.
  2. Refuse a deposit above 10,000 and print that the bank cap was hit.
  3. Hold two Account objects, 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.

FAQ: C++ Project: Bank System

Common questions about this page.

What is the StudyGrid C++ tutorial?

The StudyGrid C++ tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, classes, the STL, templates, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run c++ bank project examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c++ bank project in this C++ C++ lesson (C++ Project: Bank System).

Is the C++ editor the same as Try Python or Try HTML?

No. Try C++ compiles with g++ at /cpp/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. C++ lessons never open those editors.

Do I need to install a compiler to learn C++?

No. Open a chapter, click Try it in C++, and compile in the browser. You can also download a .cpp file and compile locally with g++.

Where should I start the C++ tutorial?

Start at C++ Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open C++ Examples, then templates, map, and lambdas. Use Next at the bottom of each chapter.

Is the C++ tutorial free?

Yes. The C++ workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.