Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this lab you will define two derived classes of the BankAccount class provide

ID: 3909792 • Letter: I

Question

In this lab you will define two derived classes of the BankAccount class provided: a checking account and a savings account class. The basic BankAccount class provides the basic functionality of a bank account, such as withdrawal and deposit. The child (derived) classes implement the specific features of each type of account.

The Savings account:

This class must add interest rate as a feature. The interest will be set in the class constructor and the amount earned will be calculated by the monthlyProc function when the user asks for a statement. The amount earned should be added to the balance.

Redefine the withdraw function here to check if there is enough money before the withdrawal is performed by the base class function. It must make sure the balance in the account doesn't fall below zero.

The printStatement function calls monthlyProc to calculate the interest earned, and prints a statement that must include the balance in the account, the interest earned and the number of withdrawals and deposits made that month. Also, reset the number of withdrawals and deposits after printing the statement.

The Checking Account:

The Checking account class will manage the fees. After the first 5 withdrawals (which are free), there is a pre-defined fee that needs to be subtracted from the balance for each subsequent withdraw. The fee is set by the constructor and used to calculate the total fee amount.

Redefine the withdraw function here to check if there is enough money before the withdrawal is performed by the base class function. It must make sure the balance in the account doesn't fall below zero.

The printStatement function calls the monthlyProc to calculate the total fee amount, then print a statement including the balance in the account, the fee and the number of withdrawals and deposits made that month. Also, reset the number of withdrawals and deposits after printing the statement.

The following is provided:

BankAccount.h: The specification of the base bank account class.

main.cpp that you can use to test your classes.

#ifndef BankAccount_h

#define BankAccount_h

class BankAccount

{

private:

virtual void monthlyProc() = 0;

  

protected:

double amount;

int withdrawals, deposits;

  

public:

BankAccount(double amt)

{

amount = amt;

withdrawals = deposits = 0;

}

  

virtual void withdraw(double amt)

{

amount -= amt;

withdrawals++;

}

virtual void deposit(double amt)

{

amount += amt;

deposits++;

}

virtual void printStatement() = 0;   

};

#include <iostream>

#include "Savings.h"

#include "Checking.h"

int main(int argc, const char * argv[]) {

  

Checking checking(1000, .5);

Savings savings(5000, .1);

char acct, action;

double amt;

  

do {

do {

std::cout << "(C)hecking OR (S)avings => ";

std::cin >> acct;

std::cout << "(d)deposit (w)ithdraw (p)rint statement => ";

std::cin >> action;

std::cout << "Enter amount => ";

std::cin >> amt;

  

switch (acct)

{

case 'c':

case 'C':

switch(action)

{

case 'd':

case 'D':

checking.deposit(amt);

break;

  

case 'w':

case 'W':

checking.withdraw(amt);

break;

  

case 'p':

case 'P':

checking.printStatement();

break;

  

default:

std::cout << "Invalid action ";

}

break;

case 's':

case 'S':

switch(action)

{

case 'd':

case 'D':

savings.deposit(amt);

break;

  

case 'w':

case 'W':

savings.withdraw(amt);

break;

  

case 'p':

case 'P':

savings.printStatement();

break;

  

default:

std::cout << "Invalid action ";

}

  

}

std::cout << "Continue? (y/n) : ";

std::cin >> action;

} while (action == 'y' || action == 'Y');

  

checking.printStatement();

savings.printStatement();

std::cout << "Quit program? (y/n) : ";

std::cin >> action;

} while (action == 'n'|| action == 'N');

  

return 0;

}

Explanation / Answer

#ifndef BankAccount_h
#define BankAccount_h

// Class BankAccount definition
class BankAccount
{
private:
// Prototype of pure virtual private member function to calculate monthly processing
virtual void monthlyProc() = 0;
protected:
// Protected data member to store balance, number of withdraw, deposit
double amount;
int withdrawals, deposits;
public:

// Parameterized constructor definition
BankAccount(double amt)
{
amount = amt;
withdrawals = deposits = 0;
}// End of parameterized constructor

// Function to withdraw amount and count number of withdrawals
virtual void withdraw(double amt)
{
// Subtracts parameter amt value from the amount
amount -= amt;
// Increases the withdrawals counter by one
withdrawals++;
}// End of function

// Function to deposit amount and count number of deposits
virtual void deposit(double amt)
{
amount += amt;
deposits++;
}// End of function

// Prototype of pure virtual member function to print statement
virtual void printStatement() = 0;
};// End of class

#endif

----------------------------------------------------------------------------------------------------

#ifndef Savings_h
#define Savings_h
#include "BankAccount.h"
using namespace std;

// Class Savings definition derived from BankAccount class
class Savings : public BankAccount
{
private:
// Data member to store interest rate and interest earned
double interestRate;
double interestEarned;
public:

// Parameterized constructor to initialize data members to parameter value
// Call the base class parameterized constructor by passing amt
Savings(double amt, double inte) : BankAccount(amt)
{
interestRate = inte;
interestEarned = 0;
}// End of parameterized constructor

// Overrides withdraw function
virtual void withdraw(double amt)
{
// Checks if parameter amt is less than the current balance amount
if(amt < amount)
// Calls the base class withdraw function
BankAccount::withdraw(amt);
// Otherwise displays error message with deficit amount
else
cout<<" Insufficient balance. Balance deficit by = "<<amt - amount;
}// End of function

// Overrides deposit function
virtual void deposit(double amt)
{
// Checks if parameter amt is greater than zero
if(amt > 0)
// Calls the base class deposit function
BankAccount::deposit(amt);
// Otherwise display error message
else
cout<<" Invalid deposit amount. "<<amt;
}// End of function

// Overrides monthlyProc() pure virtual function
void monthlyProc()
{
// Calculates the interest
interestEarned = amount * interestRate;
// Adds the interest to current balance
amount += interestEarned;
}// End of function

// Overrides printStatement() pure virtual function
virtual void printStatement()
{
// Calls the function to calculate monthly process
monthlyProc();
// Displays account information
cout<<" Current Balance: "<<amount;
cout<<" Interest Rate: "<<interestRate;
cout<<" Interest Earned: "<<interestEarned;
cout<<" Number of Deposits: "<<deposits;
cout<<" Number of Withdrawals: "<<withdrawals;
// Resets the counter to zero
deposits = withdrawals = 0;
}// End of function
};// End of class

#endif

------------------------------------------------------------------------

#ifndef Checking_h
#define Checking_h

// Class Savings definition derived from BankAccount class
class Checking : public BankAccount
{
private:
// Data member to store fine rate and total fine amount
double fee;
double feeAmount;
public:

// Parameterized constructor to initialize data members to parameter value
// Call the base class parameterized constructor by passing amt
Checking(double amt, double f) : BankAccount(amt)
{
fee = f;
feeAmount = 0;
}// End of parameterized constructor

// Overrides withdraw function
virtual void withdraw(double amt)
{
// Checks if parameter amt is less than the current balance amount
if(amt < amount)
// Calls the base class withdraw function
BankAccount::withdraw(amt);
// Otherwise displays error message with deficit amount
else
cout<<" Insufficient balance. Balance deficit by = "<<amt - amount;
}// End of function

// Overrides deposit function
virtual void deposit(double amt)
{
// Checks if parameter amt is greater than zero
if(amt > 0)
// Calls the base class deposit function
BankAccount::deposit(amt);
// Otherwise display error message
else
cout<<" Invalid deposit amount. "<<amt;
}// End of function

// Overrides monthlyProc() pure virtual function
void monthlyProc()
{
// Checks if number of withdraws is greater than 5 free withdraws
if(withdrawals > 5)
{
// Calculates the fine amount
feeAmount += (withdrawals - 5) * fee;
// Subtracts fine amount
amount -= feeAmount;
}// End of if condition
}// End of function

// Overrides printStatement() pure virtual function
virtual void printStatement()
{
// Calls the function to calculate monthly process
monthlyProc();
// Displays account information
cout<<" Current Balance: "<<amount;
cout<<" Fee: "<<fee;
cout<<" Fee Amount: "<<feeAmount;
cout<<" Number of Deposits: "<<deposits;
cout<<" Number of Withdrawals: "<<withdrawals;
// Resets the counter to zero
deposits = withdrawals = 0;
}// End of function
};// End of class

#endif

-----------------------------------------------------------------------------------------------------

#include <iostream>
#include "Savings.h"
#include "Checking.h"

// main function definition
int main(int argc, const char * argv[])
{
// Creates Checking class object using parameterized constructor
Checking checking(1000, .5);
// Creates Savings class object using parameterized constructor
Savings savings(5000, .1);
// To store user choice for account type and type of transaction respectively
char acct, action;
// To store the amount entered by the user
double amt;

// Loops till user choice is not 'n' or 'N'
do
{
// Loops till user transaction choice is not 'n' or 'N'
do
{
// Accepts account type
std::cout << "(C)hecking OR (S)avings => ";
std::cin >> acct;
// Accepts transaction type
std::cout << "(d)deposit (w)ithdraw (p)rint statement => ";
std::cin >> action;
// Checks if transaction type is not 'p' and 'P'
if(action != 'p' && action != 'P')
{
// Accepts the amount
std::cout << "Enter amount => ";
std::cin >> amt;
}// End of if condition

// Checks account type
switch (acct)
{
case 'c':
case 'C':
// Checks transaction type
switch(action)
{
case 'd':
case 'D':
checking.deposit(amt);
break;
case 'w':
case 'W':
checking.withdraw(amt);
break;
case 'p':
case 'P':
checking.printStatement();
break;
default:
std::cout << "Invalid action ";
}// End of inner switch - case
break;
case 's':
case 'S':
// Checks transaction type
switch(action)
{
case 'd':
case 'D':
savings.deposit(amt);
break;
case 'w':
case 'W':
savings.withdraw(amt);
break;
case 'p':
case 'P':
savings.printStatement();
break;
default:
std::cout << "Invalid action ";
}// End of inner switch - case
}// End of outer switch - case

// Accepts user choice for transaction
std::cout << " Continue? (y/n) : ";
std::cin >> action;
} while (action == 'y' || action == 'Y'); // End of inner do - while loop
// Displays both the account statements
checking.printStatement();
savings.printStatement();
// Accepts user choice to continue
std::cout << " Quit program? (y/n) : ";
std::cin >> action;
} while (action == 'n'|| action == 'N'); // End of outer do - while loop
return 0;
}// End of main function

Sample Output:

(C)hecking OR (S)avings => s
(d)deposit (w)ithdraw (p)rint statement => d
Enter amount => 0

Invalid deposit amount. 0
Continue? (y/n) : y
(C)hecking OR (S)avings => s
(d)deposit (w)ithdraw (p)rint statement => d
Enter amount => 100

Continue? (y/n) : y
(C)hecking OR (S)avings => s
(d)deposit (w)ithdraw (p)rint statement => p

Current Balance: 5610
Interest Rate: 0.1
Interest Earned: 510
Number of Deposits: 1
Number of Withdrawals: 0
Continue? (y/n) : y
(C)hecking OR (S)avings => s
(d)deposit (w)ithdraw (p)rint statement => w
Enter amount => 6000

Insufficient balance. Balance deficit by = 390
Continue? (y/n) : y
(C)hecking OR (S)avings => s
(d)deposit (w)ithdraw (p)rint statement => w
Enter amount => 100

Continue? (y/n) : y
(C)hecking OR (S)avings => s
(d)deposit (w)ithdraw (p)rint statement => p

Current Balance: 6061
Interest Rate: 0.1
Interest Earned: 551
Number of Deposits: 0
Number of Withdrawals: 1
Continue? (y/n) : y
(C)hecking OR (S)avings => c
(d)deposit (w)ithdraw (p)rint statement => d
Enter amount => 0

Invalid deposit amount. 0
Continue? (y/n) : y
(C)hecking OR (S)avings => c
(d)deposit (w)ithdraw (p)rint statement => d
Enter amount => 100

Continue? (y/n) : y
(C)hecking OR (S)avings => c
(d)deposit (w)ithdraw (p)rint statement => p

Current Balance: 1100
Fee: 0.5
Fee Amount: 0
Number of Deposits: 1
Number of Withdrawals: 0
Continue? (y/n) : y
(C)hecking OR (S)avings => c
(d)deposit (w)ithdraw (p)rint statement => d
Enter amount => 50

Continue? (y/n) : y
(C)hecking OR (S)avings => c
(d)deposit (w)ithdraw (p)rint statement => w
Enter amount => 10

Continue? (y/n) : y
(C)hecking OR (S)avings => c
(d)deposit (w)ithdraw (p)rint statement => w
Enter amount => 20

Continue? (y/n) : y
(C)hecking OR (S)avings => c
(d)deposit (w)ithdraw (p)rint statement => w
Enter amount => 20

Continue? (y/n) : y
(C)hecking OR (S)avings => c
(d)deposit (w)ithdraw (p)rint statement => w
Enter amount => 30

Continue? (y/n) : y
(C)hecking OR (S)avings => c
(d)deposit (w)ithdraw (p)rint statement => w
Enter amount => 40

Continue? (y/n) : y
(C)hecking OR (S)avings => c
(d)deposit (w)ithdraw (p)rint statement => w
Enter amount => 50

Continue? (y/n) : y
(C)hecking OR (S)avings => c
(d)deposit (w)ithdraw (p)rint statement => w
Enter amount => 10

Continue? (y/n) : y
(C)hecking OR (S)avings => c
(d)deposit (w)ithdraw (p)rint statement => w
Enter amount => 20

Continue? (y/n) : y
(C)hecking OR (S)avings => c
(d)deposit (w)ithdraw (p)rint statement => w
Enter amount => 30

Continue? (y/n) : y
(C)hecking OR (S)avings => c
(d)deposit (w)ithdraw (p)rint statement => p

Current Balance: 918
Fee: 0.5
Fee Amount: 2
Number of Deposits: 1
Number of Withdrawals: 9
Continue? (y/n) : n

Current Balance: 918
Fee: 0.5
Fee Amount: 2
Number of Deposits: 0
Number of Withdrawals: 0
Current Balance: 6667.1
Interest Rate: 0.1
Interest Earned: 606.1
Number of Deposits: 0
Number of Withdrawals: 0
Quit program? (y/n) : n

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote