Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please read the instructions Introduction Extend the inheritance hierarchy from the previous project by changing the classes to template classes. Do not worry about rounding

Please read the instructions

Introduction Extend the inheritance hierarchy from the previous project by changing the classes to template classes. Do not worry about rounding in classes that are instantiated as integer classes, you may just use the default rounding. You will add an additional data member, method, and bank account type that inherits SavingsAc-count ("CD", or certicate of deposit).

Deliverables A driver program (driver.cpp) An implementation of Account class (account.h) An implementation of SavingsAccount (savingsaccount.h) An implementation of CheckingAccount (checkingaccount.h) An implementation of CDAccount (cdaccount.h)

1 Account class Change to a template class and add/modify the following items. 1.1 Data Members accountNumber (string) 1.2 Member Functions Constructors Add accountNumber to the constructor. Accessors string getAccountNumber() Returns the account number. void displayAccount() Prints the account type, fee or interest rate, and balance. Determine if this should be virtual, pure virtual, or regular. 2 SavingsAccount class Change to a template class. Add accountNumber to the constructor. You may make interestRate `protected' so you can directly access it in CDAccount. No other modi cations.

3 CheckingAccount class Change to a template class. Add accountNumber to the constructor. No other modi cations.

4 CDAccount class Implement a template class that is derived from SavingsAccount class.

4.1 Data Members No additional data members (inherits its data members).

Constructors De nes a parameterized constructor which calls the SavingsAccount constructor. Destructor De nes destructor. If you have allocated memory, clean it up. Mutators void debit(amount) Rede nes the member function to debit nothing from the balance and return false, since you are not allowed to debit from a CD during its term. void credit(amount) Invokes the base class credit member function to add to the account.

5 The Driver Program In your driver, prompt the user for input as shown below. Create two vectors (savingsAccounts and checkingAccounts). The rst has Account pointers with type double that point to a CD and a Savings account, the second has Account pointers with integer type and point to two Checking accounts. Initialize the objects with the user's input and appropriate constructors. Next, create a loop that, for each savingsAccount, allows the user to deposit and withdraw from the account using the base class member functions. After withdrawing and depositing, downcast to a SavingsAccount pointer and add interest. Print the updated information by invoking the base class member function dis- playAccount(). Create another loop for each checkingAccount that allows the user to deposit and withdraw from the account using the base class member functions. After withdrawing and depositing, print the updated information by invoking the base class member function displayAccount(). Verify that, even if doubles are passed to the constructor, their values are coerced to integers. 5.1 Example input: CD Account 1 Please enter the account number: 98765432 Please enter the balance: 70 Please enter the interest rate: 0.1 (repeat for the savings account) Checking Account 1 Please enter the account number: 01010101 Please enter the balance: 70.55 Please enter the fee: 1.99 (repeat for next checking account)

5.2 Example output: CD Account 1 balance: $70.00 Enter an amount to withdraw from Account 1: 10.00 Cannot debit from a CD account. Enter an amount to deposit into Account 1: 35.00 Adding $10.50 interest to Account 1 CD account 01234567 has an interest rate of 0.10 and a balance of $115.50 Checking Account 1 balance: $70 Enter an amount to withdraw from Account 1: 9.99 0 $1 transaction fee charged Enter an amount to deposit into Account 1: .99 $1 transaction fee charged. Checking account 01010101 has a $1 transaction fee and a balance of $59

Files from previous project

driver.cpp

#include

#include

#include

using namespace std;

#include "Account.h" // Account class definition

#include "SavingsAccount.h" // SavingsAccount class definition

#include "CheckingAccount.h" // CheckingAccount class definition

int main() {

// create vector accounts

vector < Account * > accounts(6);

double balance1, balance2, balance3, balance4, balance5, balance6;

double rate1, rate2, rate3;

double fee1, fee2, fee3;

cout << "Enter balance and interest rate for account1:" << endl;

cin >> balance1;

cin >> rate1;

cout << "Enter balance and transaction fee for account2:" << endl;

cin >> balance2;

cin >> fee1;

cout << "Enter balance and interest rate for account3:" << endl;

cin >> balance3;

cin >> rate2;

cout << "Enter balance and transaction fee for account4:" << endl;

cin >> balance4;

cin >> fee2;

cout << "Enter balance and interest rate for account5:" << endl;

cin >> balance5;

cin >> rate3;

cout << "Enter balance and transaction fee for account6:" << endl;

cin >> balance6;

cin >> fee3;

// initialize vector with Accounts

accounts[0] = new SavingsAccount(balance1, rate1);

accounts[1] = new CheckingAccount(balance2, fee1);

accounts[2] = new SavingsAccount(balance3, rate2);

accounts[3] = new CheckingAccount(balance4, fee2);

accounts[4] = new SavingsAccount(balance5, rate3);

accounts[5] = new CheckingAccount(balance6, fee3);

cout << fixed << setprecision(2);

// loop through vector, prompting user for debit and credit amounts

for (size_t i = 0; i < accounts.size(); i++) {

cout << "Account " << i + 1 << " balance: $"

<< accounts[i]->getBalance();

double withdrawalAmount = 0.0;

cout << " Enter an amount to withdraw from Account " << i + 1

<< ": ";

cin >> withdrawalAmount;

accounts[i]->debit(withdrawalAmount); // debit account

double depositAmount = 0.0;

cout << "Enter an amount to deposit into Account " << i + 1

<< ": ";

cin >> depositAmount;

accounts[i]->credit(depositAmount); // credit account

// downcast pointer

SavingsAccount *savingsAccountPtr = dynamic_cast < SavingsAccount * > (accounts[i]);

// if Account is a SavingsAccount, calculate and add interest

if (savingsAccountPtr != 0) {

double interestEarned = savingsAccountPtr->calculateInterest();

cout << "Adding $" << interestEarned << " interest to Account "

<< i + 1 << " (a SavingsAccount)" << endl;

savingsAccountPtr->credit(interestEarned);

} // end if

cout << "Updated Account " << i + 1 << " balance: $"

<< accounts[i]->getBalance() << " ";

} // end for

system("pause");

} // end main

savingsaccount.h

#ifndef SAVINGS_H

#define SAVINGS_H

#include "Account.h" // Account class definition

class SavingsAccount : public Account {

public:

// constructor initializes balance and interest rate

SavingsAccount(double, double);

double calculateInterest(); // determine interest owed

private:

double interestRate; // interest rate (percentage) earned by account

}; // end class SavingsAccount

#endif

savingsaccount.cpp

#include "SavingsAccount.h" // SavingsAccount class definition

// constructor initializes balance and interest rate

SavingsAccount::SavingsAccount(double initialBalance, double rate)

: Account(initialBalance) {// initialize base class

interestRate = (rate < 0.0) ? 0.0 : rate; // set interestRate

} // end SavingsAccount constructor

// return the amount of interest earned

double SavingsAccount::calculateInterest() {

return getBalance() * interestRate;

} // end function calculateInterest

checkingaccount.h

#ifndef CHECKING_H

#define CHECKING_H

#include "Account.h" // Account class definition

class CheckingAccount : public Account {

public:

// constructor initializes balance and transaction fee

CheckingAccount(double, double);

/*function prototype for virtual function credit,

which will redefine the inherited credit function */

virtual void credit(double);

/*function prototype for virtual function debit,

which will redefine the inherited debit function */

virtual bool debit(double);

private:

double transactionFee; // fee charged per transaction

// utility function to charge fee

void chargeFee();

}; // end class CheckingAccount

#endif

checkingaccount.cpp

#include

using namespace std;

#include "CheckingAccount.h" // CheckingAccount class definition

// constructor initializes balance and transaction fee

CheckingAccount::CheckingAccount(double initialBalance, double fee)

: Account(initialBalance) { // initialize base class

transactionFee = (fee < 0.0) ? 0.0 : fee; // set transaction fee

} // end CheckingAccount constructor

// credit (add) an amount to the account balance and charge fee

void CheckingAccount::credit(double amount) {

Account::credit(amount); // always succeeds

chargeFee();

} // end function credit

// debit (subtract) an amount from the account balance and charge fee

bool CheckingAccount::debit(double amount) {

bool success = Account::debit(amount); // attempt to debit

if (success) { // if money was debited, charge fee and return true

chargeFee();

return true;

} // end if

else // otherwise, do not charge fee and return false

return false;

} // end function debit

// subtract transaction fee

void CheckingAccount::chargeFee() {

Account::setBalance(getBalance() - transactionFee);

cout << "$" << transactionFee << " transaction fee charged." << endl;

} // end function chargeFee

account.h

#ifndef ACCOUNT_H

#define ACCOUNT_H

class Account {

public:

Account(double); // constructor initializes balance

virtual void credit(double); // function prototype for virtual function credit

virtual bool debit(double); // function prototype for virtual function debit

void setBalance(double); // sets the account balance

double getBalance(); // return the account balance

private:

double balance; // data member that stores the balance

}; // end class Account

#endif

account.cpp

#include

using namespace std;

#include "Account.h" // include definition of class Account

// Account constructor initializes data member balance

Account::Account(double initialBalance) {

// if initialBalance is greater than or equal to 0.0, set this value

// as the balance of the Account

if (initialBalance >= 0.0)

balance = initialBalance;

else { // otherwise, output message and set balance to 0.0

cout << "Error: Initial balance cannot be negative." << endl;

balance = 0.0;

} // end if...else

} // end Account constructor

// credit (add) an amount to the account balance

void Account::credit(double amount) {

balance = balance + amount; // add amount to balance

} // end function credit

// debit (subtract) an amount from the account balance

// return bool indicating whether money was debited

bool Account::debit(double amount) {

if (amount > balance) { // debit amount exceeds balance

cout << "Debit amount exceeded account balance." << endl;

return false;

} // end if

else { // debit amount does not exceed balance

balance = balance - amount;

return true;

} // end else

} // end function debit

// set the account balance

void Account::setBalance(double newBalance) {

balance = newBalance;

} // end function setBalance

// return the account balance

double Account::getBalance() {

return balance;

} // end function getBalance

please explain with comments

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

10th Edition

0137916787, 978-0137916788

More Books

Students also viewed these Databases questions