← Back to Classes
Question 152
BankAccount Withdraw Exception
BankAccount Withdraw Exception
Create a classBankAccountwith a privatedouble balance. Requirements: 1. ConstructorBankAccount(double initialBalance): throwsstd::invalid_argumentwith"Initial balance cannot be negative"ifinitialBalance < 0. Otherwise sets balance. 2.void deposit(double amount): throwsstd::invalid_argumentwith"Deposit amount must be positive"ifamount <= 0. Otherwise adds to balance. 3.void withdraw(double amount): throwsstd::invalid_argumentwith"Withdraw amount must be positive"ifamount <= 0; throwsstd::runtime_errorwith"Insufficient funds"ifamount > balance. Otherwise subtracts from balance. 4.double getBalance() const: returns the current balance. Do NOT write a main function.
Expected Output:
150= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
ClassesExceptions
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · 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.