Question

In: Computer Science

Using C++ Create a polymorphic banking program using the Bank-Account hierarchy created in Exercise 2 of...

Using C++

Create a polymorphic banking program using the Bank-Account hierarchy created in Exercise 2 of Written Assignment 4. For each account in the vector, allow the user to specify an amount of money to withdraw from the Bank-Account using member function debit and an amount of money to deposit into the Bank-Account using member function credit. As you process each Bank-Account, determine its type. If a Bank-Account is a Savings, calculate the amount of interest owed to the Bank-Account using member function calculateInterest, then add the interest to the account balance using member function credit. After processing an account, print the updated account balance obtained by invoking base-class member function getBalance. [MO 5.1, MO 5.2]

Solutions

Expert Solution

ANSWER :

Code :-

#include <iostream>

#include <vector>

using namespace std;

class Account{

public:

Account(double);

virtual void credit(double);

virtual bool debit(double);

void setBalance(double);

double getBalance();

private:

double balance;

};

Account::Account(double initialBalance){

if(initialBalance>=0.0)

balance=initialBalance;

else

cout<<"Not valid number."<<endl;

}

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 newb)

{

balance=newb;

}

double Account::getBalance()

{

return balance;

}

class SavingAccount:public Account

{

public:

SavingAccount(double,double);

double calculateInterest();

private:

double myrate;

};

SavingAccount::SavingAccount(double initialBalance,double rate):Account(initialBalance)

{

myrate=rate;

}

double SavingAccount::calculateInterest()

{

return getBalance()*myrate;

}

class CheckingAccount:public Account

{

public:

CheckingAccount(double,double);

void credit(double);

bool debit(double);

void chargeFee();

private:

double myfee;

};

CheckingAccount::CheckingAccount(double initialBalance,double fee):Account(initialBalance)

{

myfee=fee;

}

void CheckingAccount::credit(double amount)

{

Account::credit(amount);

chargeFee();

}

bool CheckingAccount::debit(double amount)

{

if (true==Account::debit(amount))

{

chargeFee();

return true;

}

else{

return false;

}

}

void CheckingAccount::chargeFee() {

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

}

int main(){

const int SIZE = 3;

// creating three account

vector<Account *>account(SIZE);

double interest;

double withdraw;

double deposit;

account[0]=new SavingAccount(1000,0.5);

account[1]=new CheckingAccount(1000,0.7);

account[2]=new SavingAccount(2000,0.4);

for(int i=0; i<SIZE; i++) {

cout<<"Processing Account number "<<(i+1)<<" balance: $"<<account[i]->getBalance()<<endl;

// debit

cout<<"Enter amount for debit: ";

cin>>withdraw;

account[i]->debit(withdraw);

// credit

cout<<"Enter amount to credit: ";

cin>>deposit;

account[i]->credit(deposit);

// type of acount

SavingAccount *sav = dynamic_cast<SavingAccount*>(account[i]);

if(sav != NULL) {

cout<<"Account is of Saving type"<<endl;

interest = sav->calculateInterest();

cout<<"Interest owned: "<<interest<<endl;

sav->credit(interest);

}else {

cout<<"Account "<<(i+1)<<" is of Checking type"<<endl;

}

cout<<"Final balance: "<<account[i]->getBalance()<<endl;

cout<<endl;

}

// deleting the objects

for(int i=0; i<SIZE; i++)

delete account[i];

return 0;

}

Output :-

Hope it helps... please give an upvote. it's very important to me... thank you:)


Related Solutions

Please do this in C++ Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create...
Please do this in C++ Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create a Rectangle 2. Class Derive a Square Class from the Rectangle Class 3. Derive a Right Triangle Class from the Rectangle Class 4. Create a Circle Class 5. Create a Sphere Class 6. Create a Prism Class 7. Define any other classes necessary to implement a solution 8. Define a Program Driver Class for Demonstration (Create a Container to hold objects of the types...
Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create a Rectangle 2. Class Derive...
Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create a Rectangle 2. Class Derive a Square Class from the Rectangle Class 3. Derive a Right Triangle Class from the Rectangle Class 4. Create a Circle Class 5. Create a Sphere Class 6. Create a Prism Class 7. Define any other classes necessary to implement a solution 8. Define a Program Driver Class for Demonstration (Create a Container to hold objects of the types described above Create and Add...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main()...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main() function to coordinate the execution of the program. We will need methods: Method for Depositing values into the account. What type of method will it be? Method for Withdrawing values from the account. What type of method will it be? Method to output the balance of the account. What type of method will it be? Method that will output all deposits made to the...
Using C#, modify the codes below to do the following: Develop a polymorphic banking application using...
Using C#, modify the codes below to do the following: Develop a polymorphic banking application using the Account hierarchy created in the codes below. Create an array of Account references to SavingsAccount and CheckingAccount objects. For each Account in the array, allow the user to specify an amount of money to withdraw from the Account using method Debit and an amount of money to deposit into the Account using method Credit. As you process each Account, determine its type. If...
c++ The above Account class was created to model a bank account. An account has the...
c++ The above Account class was created to model a bank account. An account has the properties (keep the properties private) account number, balance, and annual interest rate, date created, and functions to deposit and withdraw. Create two derived classes for checking and saving accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn. Define a constant virtual toString() function in the Account class and override it in the derived classes to return the account...
c++ using polymorphyism , create an inheritance hierarchy for the following types of insurance : Home,...
c++ using polymorphyism , create an inheritance hierarchy for the following types of insurance : Home, vehicle , life 2 member functions should be included: - calcpremium    - Home- 0.5% of the value of the home    - Life- 1% of the value of the policy    - Vehicle - 0.5% of the value of the policy calcCommission Home- 0.2% of the value of the home Life- 1% of the value of the policy Vehicle - 0.5% of the...
Create an object call Accounts using python for a banking system. Initialize the account with three...
Create an object call Accounts using python for a banking system. Initialize the account with three data as inputs : Firstname, Lastname and initial deposit. Create 4 additional member functions: Deposit, Withdraw, Fee Calculations, interest The fee calculation is going to be $14 per month if the total amount in the account is less than $1000. Interest is set to be at 5%.
c++ Define polymorphism. What is the benefit of using pointers with polymorphic functions?
c++ Define polymorphism. What is the benefit of using pointers with polymorphic functions?
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create 2 threads that will act as counters. One thread should count down from 5 to 0. Once that thread reaches 0, then a second thread should be used to count up to 20.
Write a program to create a bank account and to process transactions. Call this class bankAccount...
Write a program to create a bank account and to process transactions. Call this class bankAccount A bank account can only be given an initial balance when it is instantiated. By default, a new bank account should have a balance of 0. A bank account should have a public get method, but no public set method. A bank account should have a process method with a double parameter to perform deposits and withdrawals. A negative parameter represents a withdrawal. It...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT