Question: The main concept of this assignment is exception classes and how they are used handling certain runtime errors.Objectives-1. Demonstrate how to define one or more

The main concept of this assignment is exception classes and how they are used handling certain runtime errors.Objectives-1. Demonstrate how to define one or more Exception classes. 2. Demonstrate where to test for an error condition and how to throw (raise) an exception. 3. Demonstrate how to catch and handle an exception.I have provided a group of related account classes and a test program to help you focus on just exceptions and exception handling. You will write one small exception class that will be used tomodify the account classes so that certain functions might throw an exception of this type.Thesefunctions will perform some test, which if true will throw an exceptionthese functions willnot handle the exception, only test for and possibly throw an exception.In the main program (driver function), you will modify what you see to try and catch theexceptions.The statements that should cause an exception error are commented, so you can testyour exceptions by removing the comments one-by-one.Remember that once an exception isthrown the remaining try block is skipped, so you should test each statement individually.All thatis required in an exception handler block for this assignment is to print a message that an exception was caught.The exception class you will write, with at least a 0-arg constructor, is as follows:class AccountOverdrawnException The account class functions that you will modify are as follows:BankAccount:withdraw function throws AccountOverdrawnExceptionwhen amount exceeds balance transfer function throws AccountOverdrawnException when amount exceeds balance CheckingAccount: modify inherited functions as necessary SavingsAccount: modify inherited functions as necessary TimeDepositAccount:modify inherited functions as necessary.Requirements:1.Write the exception class as described; the exception class must be in its own separate.h and .cpp files. 2.Modify the account classes as described. (You will need to use an#includedirectivein the BankAccount base class in order to throw excpetions.) 3.Modify the test program to try-catch this type of exception with some errorconditions. (You may need to use an#includedirective also in the AccountTest.cpp program file in order to catch and handle exceptions.) 4. In addition to the required 0-argument constructor, include and demonstrate a 1-argument constructor in your exception class to report the overdrawn amount with anAccountOverdrawnException.If an account member function detects an attempt towithdraw ortransfer an amount that would leave a negative balance then do not allowthat to happen; instead throw the exception passing it the difference between theargument amount and balance.The exception handler should retrieve that valuefrom the exception object and include the amount in the message. (Note: the exceptionclass itself should not print messages; it will require a private data member and apublic observer function.)5. Demonstrate that in some cases, only the base class functions need to testfor andthrow an exception.6. Add another exception class called InvalidAmountException.This exceptionshould be used with any constructor or function that takes an amount value as anargument and which should be thrown when the amount argument isnegative.Theexception handler should include that amount in its error message.(Note: theexception class itself should not print messages; it will require a private data memberand a public observer function.)

/* AccountTest.cpp

This program tests the BankAccount class and

their subclasses. */

#include

#include using namespace std;

#include "CheckingAccount.h" #include "SavingsAccount.h" #include "TimeDepositAccount.h"

int main() { cout << fixed << showpoint << setprecision(2) << endl;

CheckingAccount harrysChecking(100); SavingsAccount momsSavings(0.5);

// begin try...

momsSavings.deposit(10000);

// Mom: 10000 // Harry: 100

momsSavings.transfer(harrysChecking, 2000);

harrysChecking.withdraw(1500); harrysChecking.withdraw(80);

// Mom: 8000 // Harry: 520

momsSavings.transfer(harrysChecking, 1000); harrysChecking.withdraw(400);

// Mom: 7000 // Harry: 1120

// simulate end of month momsSavings.addInterest(); harrysChecking.deductFees();

// 7035 cout << " Mom's savings balance = " << momsSavings.getBalance() << endl;

// 1116 cout << "Harry's checking balance = " << harrysChecking.getBalance() << endl << endl;

//harrysChecking.withdraw(5000); // AccountOverdrawnException //harrysChecking.deposit(-5000); // InvalidAmountException

// -3884 cout << "Harry's checking balance = " << harrysChecking.getBalance() << endl << endl;

// ...end try and catch exceptions

TimeDepositAccount dadsAccount(5.25, 10); dadsAccount.deposit(10000);

for( int i = 1; i <= 9; i++ ) dadsAccount.addInterest();

// 15848.89 after 9 years cout << " Dad's account balance = " << dadsAccount.getBalance() << endl;

// begin try...

dadsAccount.withdraw(100); // bal: 15728.89 ($20 penalty) dadsAccount.addInterest(); // bal: 16554.66 (interest: 825.77) dadsAccount.withdraw(100); // bal: 16454.66 (no penalty)

//dadsAccount.transfer(momsSavings, 20000); // AccountOverdrawnException

// ... end try and catch excpetions.

cout << endl; cout << " Dad's account balance = " << dadsAccount.getBalance() << endl;

cout << " Mom's savings balance = " << momsSavings.getBalance() << endl;

cout << " End Program - ";

return 0; }

* BankAccount.cpp

A bank account has a balance that can be changed by

deposits and withdrawals.

*/

#include "BankAccount.h"

/*

Constructing an account with 0 balance.

*/

BankAccount::BankAccount()

{

balance = 0.0;

}

/*

Constructs a bank account with a given balance

param initialBalance the initial balance

*/

BankAccount::BankAccount(double initialBalance)

{

balance = initialBalance;

}

/*

Deposits money into the bank account.

param amount the amount to deposit

*/

void BankAccount::deposit(double amount)

{

balance = balance + amount;

}

/*

Withdraws money from the bank account.

param amount the amount to withdraw

*/

void BankAccount::withdraw(double amount)

{

balance = balance - amount;

}

/*

Gets the current balance of the bank account.

return the current balance

*/

double BankAccount::getBalance()

{

return balance;

}

/*

Transfers money from the bank account to another account

param other the other account

param amount the amount to transfer

*/

void BankAccount::transfer(BankAccount& other, double amount)

{

withdraw(amount);

other.deposit(amount); // polymorphic reference, late binding

}

/* BankAccount.h

A bank account has a balance that can be changed by deposits and withdrawals. */

#pragma once;

class BankAccount { private: double balance;

public: /* Constructing an account with 0 balance. */ BankAccount();

/* Constructs a bank account with a given balance param initialBalance the initial balance */ BankAccount(double initialBalance);

/* Deposits money into the bank account. param amount the amount to deposit

Must be virtual for BankAccount& param in trasfer() */ virtual void deposit(double amount);

/* Withdraws money from the bank account. param amount the amount to withdraw */ void withdraw(double amount);

/* Gets the current balance of the bank account. return the current balance */ double getBalance();

/* Transfers money from the bank account to another account param other the other account param amount the amount to transfer */ void transfer(BankAccount& other, double amount);

};

/* CheckingAccount.cpp

A checking account that charges transaction fees.

*/

#include "CheckingAccount.h"

/*

Constructs a checking account with a given balance

param initialBalance the initial balance

*/

CheckingAccount::CheckingAccount(int initialBalance)

: BankAccount(initialBalance)

{

// initialize transaction count

transactionCount = 0;

}

void CheckingAccount::deposit(double amount)

{

// attempt to add amount to balance

BankAccount::deposit(amount);

// now update transaction count

transactionCount++;

}

void CheckingAccount::withdraw(double amount)

{

// attempt to subtract amount from balance

BankAccount::withdraw(amount);

// now update transaction count

transactionCount++;

}

/*

Attempts to deduct the accumulated fees, if any,

and resets the transaction count if successful.

*/

void CheckingAccount::deductFees()

{

if (transactionCount > FREE_TRANSACTIONS)

{

double fees = TRANSACTION_FEE *

(transactionCount - FREE_TRANSACTIONS);

BankAccount::withdraw(fees);

}

transactionCount = 0;

}

/* CheckingAccount.h

A checking account that charges transaction fees.

*/

#pragma once;

#include "BankAccount.h"

const int FREE_TRANSACTIONS = 3;

const double TRANSACTION_FEE = 2.0;

class CheckingAccount : public BankAccount

{

private:

int transactionCount; // successful transactions

public:

/*

Constructs a checking account with a given balance

param initialBalance the initial balance

*/

CheckingAccount(int initialBalance);

virtual void deposit(double amount);

void withdraw(double amount);

/**

Attempts to deduct the accumulated fees, if any,

and resets the transaction count if successful.

*/

void deductFees();

};

/* SavingsAccount.cpp

An account that earns interest at a fixed rate. */

#include "SavingsAccount.h"

/* Constructs a bank account with a given interest rate param rate the interest rate */ SavingsAccount::SavingsAccount(double rate) { interestRate = rate; }

/* Adds the earned interest to the account balance. */ void SavingsAccount::addInterest() { double interest = getBalance() * interestRate / 100;

deposit(interest); }

/* SavingsAccount.h

An account that earns interest at a fixed rate.

*/

#pragma once;

#include "BankAccount.h"

class SavingsAccount : public BankAccount

{

private:

double interestRate;

/*

Constructs a bank account with a given interest rate

param rate the interest rate

*/

public:

SavingsAccount(double rate);

/ Adds the earned interest to the account balance.

*/

void addInterest();

};

/* TimeDepositAccount.cpp

*/

#include "TimeDepositAccount.h"

TimeDepositAccount::TimeDepositAccount(double rate, int maturity)

: SavingsAccount(rate)

{

periodsToMaturity = maturity;

}

void TimeDepositAccount::addInterest()

{

periodsToMaturity--;

SavingsAccount::addInterest();

}

void TimeDepositAccount::withdraw(double amount)

{

if(periodsToMaturity > 0)

SavingsAccount::withdraw(EARLY_WITHDRAWAL_PENALTY + amount);

else

SavingsAccount::withdraw(amount);

}

/* TimeDepositAccount.h

*/

#pragma once;

#include "SavingsAccount.h"

const double EARLY_WITHDRAWAL_PENALTY = 20;

class TimeDepositAccount : public SavingsAccount

{

private:

int periodsToMaturity;

public:

TimeDepositAccount(double rate, int maturity);

void addInterest();

void withdraw(double amount);

};

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!