CJCoding With Joseph
15per day

BankAccount Withdraw Exception

Create a class BankAccount with a private double balance.

Requirements:
1. Constructor BankAccount(double initialBalance): throws std::invalid_argument with "Initial balance cannot be negative" if initialBalance < 0. Otherwise sets balance.
2. void deposit(double amount): throws std::invalid_argument with "Deposit amount must be positive" if amount <= 0. Otherwise adds to balance.
3. void withdraw(double amount): throws std::invalid_argument with "Withdraw amount must be positive" if amount <= 0; throws std::runtime_error with "Insufficient funds" if amount > balance. Otherwise subtracts from balance.
4. double getBalance() const: returns the current balance.

Do NOT write a main function.

Expected Output:

150
Topics:
ClassesExceptions
Code Editor
1
Tab to indent ยท Ctrl+Enter to run ยท Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.