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

C++ Tasks: Develop a polymorphism banking program using the Account hierarchy de

ID: 3574214 • Letter: C

Question

C++

Tasks: Develop a polymorphism banking program using the Account hierarchy defined in Programming Assignment 4. This assignment uses the Account, SavingsAccount and CheckingAccount classes defined in Assignment 4 to make polymorphism works.

1. Create a vector of Account pointers that can point to Account, SavingsAccount and CheckingAccountobjects.

Use the C++ STL vector template for this task. Chapter 7 of textbook contain information about using vector template. Fig. 12.19 on p. 568 of textbook also contain sample codes.

2. Create different types of accounts (objects) using the new operator and assign their addresses to the pointers in the vector. Refer to Fig. 12.19 for sample codes.

3. For each account in the vector, allow the user to specify an amount of money to deposit or withdraw from the account. The polymorphism will dynamically decide on which credit or debit member functions to use for each class.

You may have to declare some member functions (e.g. credit, debit, printAccount, etc.) as “virtual functions” in the base class to make the polymorphism working.

Do not try to use “pure virtual function” in base class Account because this would make it impossible to create objects of this class.

4. After processing each transaction (credit, debit, checkBalance, …), print appropriate message about the transaction including account number or owner, the type of account (e.g. SavingsAccount), amount of money involved, and the result of transaction.

To find out the type of account, you can use the Runtime Type Information (RTTI) capability (e.g. typeidand name functions). See Fig.12.19 in textbook for sample codes. An alternative way is to define a member variable (a string) in class definition to store the name of account’s type and initialize its value with class name when an account object is created.

5. An optional exercise is to define virtual destructor for each class to release the memory space of each object (because the object was created using the new operator). This part is optional and won’t be counted for grading this assignment.

6.In your main program:

-Create five or more different bank account objects dynamically (using new operator) and store their addresses in the vector you created.

-Perform various transactions on these accounts to test the member functions and see how polymorphism works.  

7. In item (4): Using RTTI to find out the type of account objects is optional. However, if you implement the following, you earn two bonus points.

Implement a member function getClass ID which uses the Runtime Type Information (RTTI) capability (e.g.typeid and name functions) to find the name string of the class that the current object belong to. For example, if the pointer points to a SavingsAccount object, then this member function should return a “SavingsAccount” string.

In the main function, use a for-loop to traverse through the vector that contains pointers to the accounting objects, and print out the class name of each object in the vector.

8. In item (5): Define virtual destructor for each class to release the memory space of each object (because the object was created using the new operator). Originally, this part is optional and won’t be counted for grading this assignment. But if you implement this part, two bonus points will be award.

/*

This is the previous assignment!

*/

#include<iostream>

using namespace std;

//Base class - Account

class Account

{

protected:

double Account_Balance;

public:

Account(double);

bool credit(double);

bool debit(double);

double getBalance();

void Initial_Balance(double);

};

//Account balance Constructor with parameters

Account::Account(double Balance1)

{

if(Balance1<=0.0)

{

cout<<" Invalid - Plz enter valid amount!! ";

Account_Balance=0.0;

}

else

Account_Balance=Balance1;

}

//Function to initialise the balance (Initial balance)

void Account::Initial_Balance(double Balance2)

{

Account_Balance=Balance2;

}

//Crediting the amount to the account

bool Account::credit(double amt)

{

if(amt<=0.0)

{

cout<<" Error! unsuccessful transaction. ";

cout<<" Enter valid amount";

return false;

}

Account_Balance+=amt;

cout<<" Transaction Successful... ";

return true;

}

//Function to withdraw the amount

bool Account::debit(double amt)

{

if(amt<=0.0)

{

cout<<" Transaction unsucessful!!Please enter a valid amount. ";

return false;

}

if(amt>Account_Balance)

{

cout<<" Transaction unsucessful ";

cout<<" Low Balance - Amount cannot been withdrawn ";

return false;

}

Account_Balance-=amt;

cout<<" Transaction Successful... ";

return true;

}

//Returns the balance amount

double Account::getBalance()

{

return Account_Balance;

}

//SavingsAccount class derived from base class Account

class SavingsAccount:public Account

{

protected:

double interest_rate;

public:

SavingsAccount(double,double);

double InterestRate();

};

//Constructor for SavingsAccount with parameters

SavingsAccount::SavingsAccount(double Bal,double percentage):Account(Bal)

{

Account_Balance=Bal;

interest_rate=percentage;

}

//Returns interest rate

double SavingsAccount::InterestRate()

{

return Account_Balance*(interest_rate/100);

}

//Checking Account class - derived from base class

class CheckingAccount:public Account

{

protected:

double fee;

public:

CheckingAccount(double,double);

double credit(double);

double debit(double);

};

//Constructor for checking account with parameters

CheckingAccount::CheckingAccount(double Bal,double fee_amount):Account(Bal)

{

Account_Balance=Bal;

fee=fee_amount;

}

//Fee charged for the transaction debit

double CheckingAccount::credit(double amt)

{

if((amt-fee)>=0.0)

{ amt-=fee;

cout<<" Transaction processed - Fee Charged ";

}

else

amt=-99;

return amt;

}

//This function should charge a fee only if money is actually withdrawn

double CheckingAccount::debit(double amt)

{

if((amt-fee)>=0.0)

{

amt-=fee;

cout<<" Transaction processed - Fee Charged ";

}

else

amt=-99;

return amt;

}

int main()

{

double bal;

cout<<"Enter initial account balance:";

cin>>bal;

Account acct(bal);

double intRate=12.0,Fee_Charged=10.00;

cout<<" ";

int choice;

while(true)

{

cout<<" BANK "" 1.Bank Official. 2.Account User. 3.Exit. ";

cout<<"Enter your choice:";

cin>>choice;

switch(choice)

{

case 1:

cout<<" 1.Update interest rate";

cout<<" 2.Update fee charged per transaction";

cout<<" 3.Update interest to the account ";

cout<<"Enter your choice:";

cin>>choice;

switch(choice)

{

case 1:

cout<<"Update with the new Interest rate:";

cin>>intRate;

break;

case 2:

while(true)

{

cout<<"Update amount of fee charged per transaction:";

cin>>Fee_Charged;

if(Fee_Charged>=0.0)

break;

cout<<" Invalid amount of fee entered!! ";

}

break;

case 3:

{

SavingsAccount sav(acct.getBalance(),intRate);

double interest=sav.InterestRate();

if((acct.credit(interest)==true))

cout<<" Interest added successfully. ";

}

break;

default:

cout<<"Invalid choice entered!! ";

}

break;

case 2:

cout<<" 1.Deposit Amt";

cout<<" 2.Withdraw Amt. ";

cout<<"Enter your choice:";

cin>>choice;

switch(choice)

{

case 1:

{

CheckingAccount acc(acct.getBalance(),Fee_Charged);

cout<<"Enter amount to deposit:";

cin>>bal;

if((acct.credit(bal)==true))

{

bal=acc.credit(acct.getBalance());

if(bal<0)

{

cout<<" Transaction Failed ";

return 0;

}

acct.Initial_Balance(bal);

}

}

break;

case 2:

{

CheckingAccount acc(acct.getBalance(),Fee_Charged);

cout<<"Enter amount to withdraw:";

cin>>bal;

if((acct.debit(bal)==true))

{

bal=acc.debit(acct.getBalance());

if(bal<0)

{

cout<<" Transaction Failed ";

return 0;

}

acct.Initial_Balance(bal);

}

}

break;

default:

cout<<"Invalid choice entered!! ";

}

break;

case 3:

return 0;

default:

cout<<"Invalid choice entered!! ";

}

}

}

Explanation / Answer

#include <iostream>

using std::cout;

using std::endl;

#include "Account.h"

Account::Account(Double initialBalance)

{
   if ( initialBalance >= 0.0 )

   balance = initialBalance;

   else

   {

   cout<< "Error : Initial balance cannot be negative " << endl;

balance = 0.0;

}

}

void Account: :credit( double amount )


{

   balance = balance + amount;

}

bool Account: :debit( double amount )

{

if ( amount > balance)

{

   cout << "Debit amount exceeded account balance" << endl;

   return false;

}

else {

   balance = balance -amount;

   return true;

   }

   }

void Account: :setBalance(double newBalance )

   {

   balance = newBalance;

  }

double Account: :getBalance()

   {   

   return balance;

   }

#define SAVINGS_H

#include savingsAccount : : public Account

{

public :

   SavingAccount( double, double );

double calculateInterest();

private:

   double intrestRate;

};

#endif

here Saving account class is created

#include "SAvingAccount.h"

SavingsAccount: :SavingsAccount( double initialBalance, double rate)

   Account( initialBalance )

{   

   interestRate = ( rate < 0.0 ) ? 0.0 : rate ;

}

  double savingAccount: :calculateIntrst()

{

   return getBalance() * interestRate;

}

#include<iostream>

using std: :cout;

using std: :endl;

#include "CheckingAccount.h" // CHecking Account class

CheckingAccount: :CheckingAccount( double initialBalance, double fee)

: : Account ( initialBalance )

   {

   transactionFee = (fee < 0.0 ) ? 0.0 : fee;

   }

void CheckingAccount: : credit( double amount )

{

   Account: : credit(amount );

chargeFee();

}

// debit an amount from the account balance and charge fee

bool CheckingAccount: :debit( double amount )

{

   bool success = Account: : debit( amount );

   if ( success )

   {

   chargeFee();

   return true;

   } else

   return false;

}

void CheckingAccount: :chargeFee()

   {

   Account: :setBalance( getBalance() - transactionFee );

  cout << "$" << transactionFee << " transaction fee charged" << endl;

   }

Processing Account :

#include<iostream>

using std: :cout;

using std: :cin;

using std: :endl;

#include <iomanip>

using std: : setprecision;

using std: :fixed;

3include<vector>

using std: :vector;

#include "Account.h" // account class definition

#include "SavingAccount.h" // SAVINGACCOUNT class definition

#include "checkingAccount.h" // Checking Account

int main()

{
accounts[ 0 ] = new SavingsAccounts( );

accounts[ 1 ] = new CheckingAccount( );

accounts[ 2 ] = new SavingsAccount( );

accounts[ 3 ] = new CheckingAccount( );

cout << fixed << setprecision( 2 );

for( size_t i = 0; i < accounts.size(); i++ )

{

   cout << "Account" << i + 1 << " balance: $" << ;

   double withdrawalAmount = 0.0;

cout << " Enter an amount to withdraw from account " << i+1 << " : " ;

cin>> withdrawalAmount;

double depositAmount = 0.0;

cout << "enter an amount to deposit into Account " << i+1 << " :" ;

cin>>depositAmount;

SavingAccount *savingsAccountPtr =

if ( )

{

double interestEarned = /call member function */ ;

cout<< "Adding $" << interestEarned << " interest to Account " << i + 1 << " ( a savingAccount)" << endl ;

}

cout << "Updated Account " << i+1 << "balance: $" << /* call getbalance function */ << " " ;

}

return 0;

} // end main


.

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