0% found this document useful (0 votes)
21 views3 pages

Assignment 8 642303007

This document contains C++ code for a BankAccount class that models basic deposit and withdrawal functionality for a bank account. The BankAccount class has methods to deposit and withdraw amounts, and throws exceptions for invalid or insufficient fund transactions. The main function demonstrates using the class by depositing an amount, withdrawing an amount, and attempting another withdrawal to trigger an exception.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

Assignment 8 642303007

This document contains C++ code for a BankAccount class that models basic deposit and withdrawal functionality for a bank account. The BankAccount class has methods to deposit and withdraw amounts, and throws exceptions for invalid or insufficient fund transactions. The main function demonstrates using the class by depositing an amount, withdrawing an amount, and attempting another withdrawal to trigger an exception.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Name: Srushti M.

Deshmukh
Mis: 642303007

Assignment No 8

#include <iostream> #include


<stdexcept>

using namespace std;

class BankAccount { private:


double balance;

public:
BankAccount() : balance(0.0) {}

void deposit(double amount) { if


(amount <= 0) {
throw invalid_argument("Deposit amount must be positive.");
}
balance += amount;
cout << "Deposit successful. New balance: " << balance << endl;
}

void withdraw(double amount) { if


(amount <= 0) {
throw invalid_argument("Withdrawal amount must be positive.");
}
if (amount > balance) {
throw runtime_error("Insufficient funds. Cannot withdraw.");
}
balance -= amount;
cout << "Withdrawal successful. New balance: " << balance << endl;
}
double getBalance() const { return
balance;
}
};

int main() { try


{
BankAccount account;

double depositAmount, withdrawalAmount; cout <<

"Enter deposit amount: ";


cin >> depositAmount;
account.deposit(depositAmount);

cout << "Enter withdrawal amount: "; cin


>> withdrawalAmount;
account.withdraw(withdrawalAmount);

cout << "Enter withdrawal amount: "; cin


>> withdrawalAmount;
account.withdraw(withdrawalAmount);
} catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
}

return 0;
}

You might also like