Introduction Create an inheritance hierarchy that a bank might use to represent
ID: 3904120 • Letter: I
Question
Introduction
Create an inheritance hierarchy that a bank might use to represent customers' bank accounts. All customers
can deposit (credit") into their accounts and withdraw (debit") from their accounts. More speci c types of
accounts also exist. Savings accounts, for instance, earn interest on the money they hold. Checking accounts,
on the other hand, can charge a fee per transaction.
Create an inheritance hierarchy containing base class Account and derived classes SavingsAccount and
CheckingAccount that inherit from Account.
Deliverables
A driver program (driver.cpp)
An implementation of Account class (account.h, account.cpp)
An implementation of SavingsAccount (savingsaccount.h, savingsaccount.cpp)
An implementation of CheckingAccount (checkingaccount.h, checkingaccount.cpp)
1 Account class
Implement the base class.
1.1 Data Members
balance (double)
1.2 Member Functions
Constructors
Defines default and paramaterized constructors. Confirm balance not below zero. If so, set to zero and
output an error message.
Destructor
Defines destructor. Clean up any allocated memory.
Accessors
double getBalance()
Mutators
void debit(amount) Withdraws from the account if the amount does not exceed the balance. If
so, nothing is debited and an error message is printed. This method may change when you implement the
derived class.
void credit(amount) Deposits into the account.
2 SavingsAccount class
Implement the derived class.
2.1 Data Members
interestRate (double)
2.2 Member Functions
Constructors
De nes default and parameterized constructor. Con rm interestRate not below zero. If so, set to zero and
output an error message.
Destructor
De nes destructor. Clean up any allocated memory.
Accessors
double calcInterest() Returns the current interest of the account (balance multiplied by interest
rate).
3 CheckingAccount class
Implement the derived class.
3.1 Data Members
feeForTransaction (double)
3.2 Member Functions
Constructors
Defines default and parameterized constructor. Confirm feeForTransaction not below zero. If so, set to zero
and output an error message.
Destructor
Defines destructor. Clean up any allocated memory.
Mutators
void debit(amount) Invokes the base class debit member function and substracts the fee if successful
debit (Hint: rede ne parent method to return a value you can check).
void credit(amount) Invokes the base class credit member function and subtracts the fee.
4 The Driver Program
In your driver, create a vector of six Account pointers (alternating Checking then Saving, etc.) For each
account, allow the user to deposit and withdraw from the account using the appropriate member functions.
As you process each account, determine its type. If it is a SavingsAccount, calculate and add interest after
the deposit. After processing each account, print the updated balance by invoking the base-class member
function getBalance.
4.1 Example output:
Account 1 balance: $70.00
Enter an amount to withdraw from Account 1: 80.00
Debit amount exceeded account balance.
Enter an amount to deposit into Account 1: 35.00
$1.00 transaction fee charged.
Updated Account 1 balance: $104.00
Account 2 balance: $80.00
Enter an amount to withdraw from Account 2: 10.00
Enter an amount to deposit into Account 2: 35.00
Adding $5.00 interest to Account 2 (a SavingsAccount)
Updated Account 2 balance: $110.00
(repeat for Accounts 3 through 6)
Explanation / Answer
Below is your code.Please do rate this answer positive, If i was able to help you. Let me know if you have any issues in comments
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 <iostream>
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
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 <iostream>
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
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
driver.cpp
#include <iostream>
#include <iomanip>
#include <vector>
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( 4 );
// initialize vector with Accounts
accounts[ 0 ] = new SavingsAccount( 80.0, .03 );
accounts[ 1 ] = new CheckingAccount( 70.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 << " 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
} // end main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.