Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Account
- {
- public:
- Account(double initialDeposit = 0) :balance(initialDeposit) {}
- double getBalance()const { return balance; }
- double deposit(double amount);
- double withdraw(double amount);
- private:
- double balance;
- };
- class NegativeDeposit
- {
- };
- class InsufficientFunds
- {
- };
- double Account::deposit(double amount)
- {
- if (amount > 0)
- balance += amount;
- else
- throw NegativeDeposit();
- return balance;
- }
- double Account::withdraw(double amount)
- {
- if ((amount > balance) || (amount < 0))
- throw InsufficientFunds();
- else
- balance -= amount;
- return balance;
- }
- int main()
- {
- Account a(100);
- try
- {
- cout << "Depositing 50" << endl;
- cout << "New balance: " << a.deposit(50) << endl;
- //cout << "Depositing -25" << endl;
- //cout << "New balance: " << a.deposit(-25) << endl;
- cout << "Withdraw 25" << endl;
- cout << "New balance: " << a.withdraw(25) << endl;
- cout << "Withdraw 250" << endl;
- cout << "New balance: " << a.withdraw(250) << endl;
- }
- catch (InsufficientFunds) // InsufficientFunds: a class name
- {
- cout << "Not enough money to withdraw that amount." << endl;
- }
- catch (NegativeDeposit) // NegativeDeposit: a class name
- {
- cout << "You may only deposit a positive amount." << endl;
- }
- cout << "Enter a character to exit" << endl;
- char wait;
- cin >> wait;
- system("pause");
- return 0;
- }
- // note that
- // class NegativeDeposit {…};
- // class InsufficientFunds {…};
Advertisement
Add Comment
Please, Sign In to add comment