Please help with the following, Read Instructions: Extend the inheritance hierar
ID: 3915903 • Letter: P
Question
Please help with the following, Read Instructions:
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 SavingsAccount (“CD”, or certificate of deposit).
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 modifications.
3 CheckingAccount class
Change to a template class. Add accountNumber to the constructor. No other modifications.
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).
4.2 Member Functions
Constructors
Defines a parameterized constructor which calls the SavingsAccount constructor.
Destructor
Defines destructor. If you have allocated memory, clean it up.
Mutators
void debit(amount) Redefines 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 first 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.
Extended Banking
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
My code from the other project is the following:
Driver.cpp
#include "account.h"
Account.h
Account.cpp
#include "account.h"
Savingsaccount.h
#ifndef SAVINGSACCOUNT_H
savingsaccount.cpp
#include "savingsaccount.h"
checkingaccount.h
#ifndef CHECKINGACCOUNT_H
checkingaccount.cpp
#include <iostream>
Explanation / Answer
#include "account.h"
#include "savingsaccount.h"
#include "checkingaccount.h"
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int main() {
vector<Account*> accounts(6);//declared vector of type Account
//vector<Account*>::iterator ite;
for (int i = 0; i < 6; i++) {
//Decides which account to save data
if (i % 2 == 0) {
//Checking account
double balance, fee;
cout << "Please enter balance:";
cin >> balance;
cout << "Enter transaction fees:";
cin >> fee;
accounts[i] = new checkingAccount(balance, fee);
}
else {
//Savings account
double bal, interestRate;
cout << "Please enter the balance:";
cin >> bal;
cout << "Enter the interest rate:";
cin >> interestRate;
accounts[i] = new savingsAccount(bal, interestRate);
}
}
//iterated through Account vector
int account_num = 1;
for (size_t i = 0; i < accounts.size(); i++) {
double w_amount, d_amount;
cout<<" Account "<<account_num << " balance: $"<< accounts[i] ->getBalance()<< endl;
cout<<"Enter amount to withdraw from account"<< account_num <<":$";
cin>>w_amount;
accounts[i]->debit(w_amount);
cout<<"Enter an amount to deposit to account "<< account_num<<":$";
cin>>d_amount;
accounts[i]-> credit(d_amount);
//downcast pointer
savingsAccount *savingAccountPtr = dynamic_cast<savingsAccount *>(accounts[i]);
//if savings account, calculate and add interest rate
if(savingAccountPtr != 0) {
double interestCharged = savingAccountPtr ->calcInterest();
cout<<"Adding $"<<interestCharged<<" interest to account"<<account_num<<"(Savings account)"<<endl;
savingAccountPtr -> credit(interestCharged);
}
//prints updated balance for either checking or saving account
cout<<"Updated balance in account "<<account_num<< " balance is:$ "<< accounts[i]->getBalance() << endl;
//updates account #
account_num++;
}
}
Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
template<class T>
class Account {
private:
double balance;
string accountNumber;
public:
Account()accountNumber("1000101"),balance(70);
virtual string getAccountNumber();
virtual bool debit (double amount); //funct prototype for function debit
virtual void credit (double amount);//funct prototype for function credit
double getBalance();//returns account balance
void setBalance(double newBalance);//sets account balance
virtual void displayAccount();
~Account();//destructor
};
#endif
Account.cpp
#include "account.h"
#include <iostream>
using namespace std;
//initialize data member Balance
Account::Account() {
balance = 0.0;
}
Account::Account(double amount) {
//checks that balance is not less than 0
if (amount < 0){
balance = 0.0;
cout<<"Balance cannot be less than 0" << endl;
}
else balance = amount;
}
//debit(subtract) amount from account balance
bool Account::debit(double amount) {
if (amount > balance){
cout<<"Cannot debit $"<< amount << ". Insufficient balance."<<endl;
return false;
}
else
balance -= amount;
return true;
}
//credit(add) amount to account balance
void Account::credit(double amount) {
balance += amount;
}
//gets account balance
double Account::getBalance() {
return balance;
}
//sets account balance
void Account::setBalance(double newBalance) {
balance = newBalance;
}
//destructor
Account::~Account() {
delete this;
}
Savingsaccount.h
#ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H
#include "account.h"
template<class M>
class savingsAccount: public Account{
private:
double interestRate;
public:
savingsAccount();
savingsAccount(double balance, double interest);
~savingsAccount();
double calcInterest();
};
#endif
savingsaccount.cpp
#include "savingsaccount.h"
#include <iostream>
using namespace std;
protected:
double interest;
savingsAccount::savingsAccount(): Account() {
interestRate = 0.0;//initializes balance in savings account
}
savingsAccount::savingsAccount(double balance, double interest): Account(balance) {
interestRate = (interest < 0.0) ? 0.0: interest;
}
double savingsAccount::calcInterest() {
return interestRate * getBalance();//gets interest rate
}
savingsAccount::~savingsAccount() {
delete this;
}
checkingaccount.h
#ifndef CHECKINGACCOUNT_H
#define CHECKINGACCOUNT_H
#include "account.h"
template<class U>
class checkingAccount: public Account {
public:
checkingAccount();
checkingAccount(double balance, double fee);//initializes balance and fee
~checkingAccount();//destructor
virtual bool debit(double amount);//redifines inherited debit function
virtual void credit(double amount);//redifines inherited credit function
private:
double feeForTransaction;
void chargeFee();//function to charge fee
};
#endif
checkingaccount.cpp
#include <iostream>
#include "checkingaccount.h"
using namespace std;
checkingAccount::checkingAccount(): Account() {
feeForTransaction = 0.0;//initializes balance to 0
}
checkingAccount::checkingAccount(double balance, double fee): Account(balance) {
if(fee >= 0.0){
feeForTransaction = fee;
}
else {
cout<<"Error. Fee cannot be negative."<<endl;
feeForTransaction = 0.0;
}
}
checkingAccount::~checkingAccount() {
delete this;
}
bool checkingAccount::debit(double amount) {
bool success = Account::debit(amount);
if (success) {
chargeFee();
return true;
} else {
cout << "Cannot debit. Insufficient balance." << endl;
return false;
}
}
//charges fee
void checkingAccount::chargeFee() {
Account::setBalance(getBalance() - feeForTransaction);
cout << "$" << feeForTransaction << " charged for fee." << endl;
}
void checkingAccount::credit(double amount) {
//Account::credit(amount);
if((amount+ this ->getBalance())>= feeForTransaction){
Account::credit(amount);
chargeFee();
}
else{
cout<<"Cannot credit. Insufficient balance"<<endl;
}
}
template class<savingsAccount>class CDAccount
{
public:
CDAccount()s(savingAccount);
~CDAccount();
}