(Polymorphic Banking Program Using Account Hierarchy) Develop a polymorphic bank- ing program using the Account hierarchy created in Exercise 12.10. Create a vector of Account pointers to SavingsAccount and CheckingAccount objects. For each Account in the vector, allow the user to specify an amount of money to withdraw from the Account using member function debit and an amount of money to

deposit into the Account using member function credit. As you process each Account, determine its type. If an Account is a SavingsAccount, calculate the amount of interest owed to the Account using member function calculateInterest, then add the interest to the account balance using member function credit. After processing an Account, print the up- dated account balance obtained by invoking base-class member function getBalance.

What will be an ideal response?

```
// Definition of Account class.
#ifndef ACCOUNT_H
#define ACCOUNT_H

class Account
{
public:
Account( double ); // constructor initializes balance
virtual void credit( double ); // add an amount to the account balance
virtual bool debit( double ); // subtract an amount from the balance
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
```
```
// Member-function definitions for class Account.
#include
#include "Account.h" // include definition of class Account
using namespace std;

// 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
```
```
// Definition of SavingsAccount class.
#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
```
```
// Member-function definitions for class SavingsAccount.

#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
```
```
// Definition of CheckingAccount class.
#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 );

virtual void credit( double ); // redefined credit function
virtual bool debit( double ); // redefined debit function
private:
double transactionFee; // fee charged per transaction

// utility function to charge fee
void chargeFee();
}; // end class CheckingAccount

#endif
```
```
// Member-function definitions for class CheckingAccount.
#include
#include "CheckingAccount.h" // CheckingAccount class definition
using namespace std;

// 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
```
```
// Processing Accounts polymorphically.
#include
#include
#include
#include "Account.h" // Account class definition
#include "SavingsAccount.h" // SavingsAccount class definition
#include "CheckingAccount.h" // CheckingAccount class definition
using namespace std;

int main()
{
// create vector accounts
vector < Account * > accounts( 4 );

// initialize vector with Accounts
accounts[ 0 ] = new SavingsAccount( 25.0, .03 );
accounts[ 1 ] = new CheckingAccount( 80.0, 1.0 );
accounts[ 2 ] = new SavingsAccount( 200.0, .015 );
accounts[ 3 ] = new CheckingAccount( 400.0, .5 );

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 << "\nEnter an amount to withdraw from Account " << i + 1
<< ": ";
cin >> withdrawalAmount;
accounts[ i ]->debit( withdrawalAmount ); // attempt to debit

double depositAmount = 0.0;
cout << "Enter an amount to deposit into Account " << i + 1
<< ": ";
cin >> depositAmount;
accounts[ i ]->credit( depositAmount ); // credit amount to 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() << "\n\n";
} // end for
} // end main
```
Account 1 balance: $25.00
Enter an amount to withdraw from Account 1: 15.00
Enter an amount to deposit into Account 1: 10.50
Adding $0.61 interest to Account 1 (a SavingsAccount)
Updated Account 1 balance: $21.11
Account 2 balance: $80.00
Enter an amount to withdraw from Account 2: 90.00
Debit amount exceeded account balance.
Enter an amount to deposit into Account 2: 45.00
$1.00 transaction fee charged.
Updated Account 2 balance: $124.00
Account 3 balance: $200.00
Enter an amount to withdraw from Account 3: 75.50
Enter an amount to deposit into Account 3: 300.00
Adding $6.37 interest to Account 3 (a SavingsAccount)
Updated Account 3 balance: $430.87
Account 4 balance: $400.00
Enter an amount to withdraw from Account 4: 56.81
$0.50 transaction fee charged.
Enter an amount to deposit into Account 4: 37.83
$0.50 transaction fee charged.
Updated Account 4 balance: $380.02

Computer Science & Information Technology

You might also like to view...

Which of the following contains the correct code to create and instantiate an object named MyStudent from a class named Student?

A. Dim objStudent As Student B. Dim objStudent As New Student C. Dim objStudent = Student D. Dim objStudent = New Student

Computer Science & Information Technology

What are the steps for creating a sequence diagram?

What will be an ideal response?

Computer Science & Information Technology