Create an inheritance hierarchy that a bank might use to represent customers’ ba
ID: 3907207 • Letter: C
Question
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 specific 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()
1
Banking
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). This value should be [0, 1.0]
2.2 Member Functions
Constructors
Defines default and parameterized constructor. Confirm interestRate not below zero. If so, set to zero and
output an error message.
Destructor
Defines 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 subtracts the fee if successful
debit (Hint: redefine parent method to return a value you can check).
2
Banking
void credit(amount)
Invokes the base class credit member function and subtracts the fee. (Yes, it
is terrible for this bank to charge you to deposit money which they are profiting from investing, but that’s
just the way life goes sometimes.)
4 The Driver Program
In your driver, prompt the user for input as shown below. Create a vector of six
Account
pointers (alter-
nating Checking then Saving) and point them to objects you initialize with the user’s input. Then, create a
loop that, for each account, allows 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:
Checking Account 0
Please enter the balance:
Please enter the fee:
Savings Account 1
Please enter the balance:
Please enter the interest rate:
(repeat for Accounts 3 through 6)
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)
MAKEFILE:
Explanation / Answer
account.h
/*Account.h */
#include<iostream>
using namespace std;
class Account{
private:
double balance;
public:
Account(){
balance = 0;
}
Account(double d){
balance = d;
if (balance < d)
balance = 0;
}
~Account(){
}
void debit(double a);
void credit(double a);
double getBalance();
};
account.cpp
#include "account4.h"
void Account::debit(double a){
if (a <= balance){
balance = balance - a;
}
else {
cout << "Insufficient balance ";
}
}
void Account::credit(double a){
if (a > 0){
balance = balance + a;
}
}
double Account::getBalance(){
return balance;
}
Savingsaccount.h
/*SavingsAccount.h */
#include<iostream>
#include "account4.h"
using namespace std;
class SavingsAccount : public Account {
private:
double intrestRate;
public:
SavingsAccount() : Account(){
intrestRate = 0;
}
SavingsAccount(double d, double intr) : Account(d){
intrestRate = intr;
}
~SavingsAccount(){
}
double calcIntrest();
};
savingsaccount.cpp
#include "savingsaccount4.h"
double SavingsAccount::calcIntrest(){
return getBalance() * intrestRate;
}
checkingaccount.h
/*SavingsAccount.h */
#include<iostream>
#include "savingsaccount4.h"
using namespace std;
class CheckingAccount : public Account{
private:
double feeForTransaction;
public:
CheckingAccount() : Account(){
feeForTransaction = 0;
}
CheckingAccount(double d, double fee) : Account(d){
feeForTransaction = fee;
}
~CheckingAccount(){
}
void credit(double a);
void debit(double a);
double getFeeForTransaction();
};
checkingaccount.cpp
#include "checkingaccount4.h"
void CheckingAccount::debit(double a){
Account::debit(a);
Account::debit(feeForTransaction);
}
void CheckingAccount::credit(double a){
Account::credit(a);
Account::debit(feeForTransaction);
}
double CheckingAccount::getFeeForTransaction(){
return feeForTransaction;
}
driver.cpp
#include "checkingaccount4.h"
using namespace std;
int main(){
cout << "Please enter balance:";
double bal;
cin >> bal;
cout << "Please enter balance:";
double fee;
cin >> fee;
CheckingAccount c(bal,fee);
cout << "Please enter balance:";
cin >> bal;
double intr;
while(true){
cout << "Please enter intrest rate:";
//double intr;
cin >> intr;
if (intr < 0 || intr > 1){
cout << "Invalid value ";
}
else
break;
}
SavingsAccount s(bal,intr);
cout << "Saving Account balance:" << s.getBalance() << endl;
cout << "Enter an amount to withdraw(Savings Account):";
cin >> bal;
s.debit(bal);
cout << "Saving Account balance:" << s.getBalance() << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.