Question

MAIN OBJECTIVE       Develop a polymorphic banking program using the Account hierarchy created (below). C++ Capture...

MAIN OBJECTIVE

      Develop a polymorphic banking program using the Account hierarchy created (below).

C++ Capture an output.

polymorphism.

Write an application that creates a vector of Account pointers to two SavingsAccount and two CheckingAccountobjects. For each Account in the vector, allow the user to specify an amount of money to withdraw from the Account using member function debit and an amount of money to deposit into the Account using member function credit. As you process each Account, determine its type. If an Account is a SavingsAccount, calculate the amount of interest owed to the Account using member function calculateInterest, and 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.

Account.h

#ifndef ACCOUNT_H

#define ACCOUNT_H

class Account

{

public:

Account( double );

void credit( double );

bool debit( double );

void setBalance( double );

double getBalance();

private:

double balance;

};

#endif

Account.cpp

#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;

}

SavingsAccount.h

#ifndef SAVINGS_H

#define SAVINGS_H

#include "Account.h"

class SavingsAccount : public Account

{

public:

SavingsAccount( double, double );

double calculateInterest();

private:

double interestRate;

};

#endif

SavingsAccount.cpp

#include "SavingsAccount.h"

SavingsAccount::SavingsAccount( double initialBalance, double rate )

: Account( initialBalance )

{

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

}

double SavingsAccount::calculateInterest()

{

return getBalance() * interestRate;

}

CheckingAccount.h

#ifndef CHECKING_H

#define CHECKING_H

#include "Account.h"

class CheckingAccount : public Account

{

public:

CheckingAccount( double, double );

void credit( double );

bool debit( double );

private:

double transactionFee;

void chargeFee();

};

#endif

CheckingAccount.cpp

#include <iostream>

using std::cout;

using std::endl;

#include "CheckingAccount.h"

CheckingAccount::CheckingAccount( double initialBalance, double fee )

: Account( initialBalance )

{

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

}

void CheckingAccount::credit( double amount )

{

Account::credit( amount );

chargeFee();

}

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 << " charging the transaction fee." << endl;

}

Inheritance.cpp

#include <iostream>

using std::cout;

using std::endl;

#include <iomanip>

using std::setprecision;

using std::fixed;

#include "Account.h"

#include "SavingsAccount.h"

#include "CheckingAccount.h"

int main()

{

Account account1( 80.0 );

SavingsAccount account2( 45.0, .04 );

CheckingAccount account3( 100.0, 2.0 );

cout << fixed << setprecision( 2 );

cout << "balance of account1: $" << account1.getBalance() << endl;

cout << "balance of account 2: $" << account2.getBalance() << endl;

cout << "balance of account 3: $" << account3.getBalance() << endl;

cout << "\n debit $35.00 from account1." << endl;

account1.debit( 35.0 );

cout << "\n debit $50.00 from account2." << endl;

account2.debit( 50.0 );

cout << "\ndebit $45.00 from account3." << endl;

account3.debit( 45.0 );

cout << "\naccount1 balance: $" << account1.getBalance() << endl;

cout << "account2 balance: $" << account2.getBalance() << endl;

cout << "account3 balance: $" << account3.getBalance() << endl;

cout << "\nCredit $60.00 to account1." << endl;

account1.credit( 60.0 );

cout << "\nCredit $45.00 to account2." << endl;

account2.credit( 45.0 );

cout << "\nCredit $30.00 to account3." << endl;

account3.credit( 30.0 );

cout << "\naccount1 balance: $" << account1.getBalance() << endl;

cout << "account2 balance: $" << account2.getBalance() << endl;

cout << "account3 balance: $" << account3.getBalance() << endl;

double interestEarned = account2.calculateInterest();

cout << "\nAdding $" << interestEarned << " interest to account2." << endl;

account2.credit( interestEarned );

cout << "\n updated account2 balance: $" << account2.getBalance() << endl;

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

Inheritance.cpp

#include <iostream>

using std::cout;

using std::endl;

#include <iomanip>

#include <vector>

using std::setprecision;

using std::fixed;

#include "Account.h"

#include "SavingsAccount.h"

#include "CheckingAccount.h"

using namespace std;

int main()

{

vector <Account*> accounts(5);

    accounts[ 0 ] = new Account( 80 );

    accounts[ 1 ] = new SavingsAccount( 45.0, .04 );

    accounts[ 2 ] = new CheckingAccount( 100.0, 2.0);

     SavingsAccount account2( 45.0, .04 );

cout << "balance of account1: $" << accounts[0]->getBalance() << endl;

cout << "balance of account 2: $" << accounts[1]->getBalance() << endl;

cout << "balance of account 3: $" << accounts[2]->getBalance() << endl;

cout << "\n debit $35.00 from account1." << endl;

accounts[0]->debit( 35.0 );

cout << "\n debit $50.00 from account2." << endl;

accounts[1]->debit( 50.0 );

cout << "\ndebit $45.00 from account3." << endl;

accounts[2]->debit( 45.0 );

cout << "\naccount1 balance: $" << accounts[0]->getBalance() << endl;

cout << "account2 balance: $" << accounts[1]->getBalance() << endl;

cout << "account3 balance: $" << accounts[2]->getBalance() << endl;

cout << "\nCredit $60.00 to account1." << endl;

accounts[0]->credit( 60.0 );

cout << "\nCredit $45.00 to account2." << endl;

accounts[1]->credit( 45.0 );

cout << "\nCredit $30.00 to account3." << endl;

accounts[2]->credit( 30.0 );

cout << "\naccount1 balance: $" << accounts[0]->getBalance() << endl;

cout << "account2 balance: $" << accounts[1]->getBalance() << endl;

cout << "account3 balance: $" << accounts[2]->getBalance() << endl;

double interestEarned = dynamic_cast<SavingsAccount*>(accounts[1])->calculateInterest();

cout << "\nAdding $" << interestEarned << " interest to account2." << endl;

accounts[1]->credit( interestEarned );

cout << "\n updated account2 balance: $" << accounts[1]->getBalance() << endl;

system("pause");

return 0;

}

SavingsAccount.h

#ifndef SAVINGS_H

#define SAVINGS_H

#include "Account.h"

class SavingsAccount : public Account

{

public:

SavingsAccount( double, double );

~SavingsAccount();

double calculateInterest();

private:

double interestRate;

};

#endif

SavingsAccount.cpp

#include "SavingsAccount.h"

SavingsAccount::SavingsAccount( double initialBalance, double rate )

: Account( initialBalance )

{

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

}

SavingsAccount::~SavingsAccount() {}

double SavingsAccount::calculateInterest()

    

{

return getBalance() * interestRate;

}

Account.h

#ifndef ACCOUNT_H

#define ACCOUNT_H

class Account

{

public:

Account( double );

virtual ~Account();

virtual void credit( double );

virtual bool debit( double );

void setBalance( double );

double getBalance();

private:

double balance;

};

#endif

CheckingAccount.h

#ifndef CHECKING_H

#define CHECKING_H

#include "Account.h"

class CheckingAccount : public Account

{

public:

CheckingAccount( double, double );

~CheckingAccount();

void credit( double );

bool debit( double );

private:

double transactionFee;

void chargeFee();

};

#endif

Account.cpp

#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;

}

}

Account:: ~Account() {}

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;

}

CheckingAccount.cpp

#include <iostream>

using std::cout;

using std::endl;

#include "CheckingAccount.h"

CheckingAccount::CheckingAccount( double initialBalance, double fee )

: Account( initialBalance )

{

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

}

CheckingAccount::~CheckingAccount() {}

void CheckingAccount::credit( double amount )

{

Account::credit( amount );

chargeFee();

}

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 << " charging the transaction fee." << endl;

}

Output:

İT CAUser6302330\Documents\Visual Studio 2010\Projects\1008Debug\1008.exe balance of account1 80 balance of account 2: $45 ba

Add a comment
Know the answer?
Add Answer to:
MAIN OBJECTIVE       Develop a polymorphic banking program using the Account hierarchy created (below). C++ Capture...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Introduction Extend the inheritance hierarchy from the previous project by changing the classes to template classes....

    Introduction 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 SavingsAc-count ("CD", or certicate of deposit). Deliverables A driver program (driver.cpp) An implementation of Account class (account.h) An implementation of SavingsAccount (savingsaccount.h) An implementation of CheckingAccount (checkingaccount.h) An implementation of...

  • (C++) How do I develop a polymorphic banking program using an already existing Bank-Account hierarchy? For each account...

    (C++) How do I develop a polymorphic banking program using an already existing Bank-Account hierarchy? 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,...

  • C++ Help. PLEASE include detailed comments and explanations. PLEASE follow the drections exactly. Thank you. Define...

    C++ Help. PLEASE include detailed comments and explanations. PLEASE follow the drections exactly. Thank you. Define a class with the name BankAccount and the following members: Data Members: accountBalance: balance held in the account interestRate: annual interest rate. accountID: unique 3 digit account number assigned to each BankAccount object. Use a static data member to generate this unique account number for each BankAccount count: A static data member to track the count of the number of BankAccount objects created. Member...

  • Im having trouble with this C++ program. Lab 10/object/test files provided LAB 10 1 //Savings.cpp - displays the account balance at 2 //the end of 1 through 3 years 3 //Created/revised by <your nam...

    Im having trouble with this C++ program. Lab 10/object/test files provided LAB 10 1 //Savings.cpp - displays the account balance at 2 //the end of 1 through 3 years 3 //Created/revised by <your name> on <current date> 4 5 #include <iostream> 6 #include <iomanip> 7 #include <cmath> 8 using namespace std; 9 10 //function prototype 11 double getBalance(int amount, double rate, int y); 12 13 int main() 14 { 15 int deposit = 0; 16 double interestRate = 0.0; 17...

  • C++ Banks offer various types of accounts, such as savings, checking, certificate of deposits, and money...

    C++ Banks offer various types of accounts, such as savings, checking, certificate of deposits, and money market, to attract customers as well as meet their specific needs. Two of the most commonly used accounts are savings and checking. Each of these accounts has various options. For example, you may have a savings account that requires no minimum balance but has a lower interest rate. Similarly, you may have a checking account that limits the number of checks you may write....

  • In C++ Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit (i.e., credit) money into their accounts and withdraw (i.e.,...

    In C++ Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit (i.e., credit) money into their accounts and withdraw (i.e., debit) money 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, charge a fee per transaction (i.e., credit or debit). Create an inheritance hierarchy containing base class Account and derived...

  • Please show it in C++. Thank you! Problem Definition Create an inheritance hierarchy containing base class...

    Please show it in C++. Thank you! Problem Definition Create an inheritance hierarchy containing base class Account and derived class Savings-Account. Base class Account should include one data member of type double to represent the account balance. The class should provide a constructor that receives an initial baiance and uses it to initialize the data member. The class should provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money...

  • Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string;...

    Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string; class Account { public: Account(); Account(string, double); void deposit(double); bool withdraw(double); string getName() const; double getBalance() const; private: string name; double balance; }; #endif ////////////////////////////////////////////// //AccountManager.hpp #ifndef _ACCOUNT_MANAGER_HPP_ #define _ACCOUNT_MANAGER_HPP_ #include "Account.hpp" #include <string> using std::string; class AccountManager { public: AccountManager(); AccountManager(const AccountManager&); //copy constructor void open(string); void close(string); void depositByName(string,double); bool withdrawByName(string,double); double getBalanceByName(string); Account getAccountByName(string); void openSuperVipAccount(Account&); void closeSuperVipAccount(); bool getBalanceOfSuperVipAccount(double&) const;...

  • The code is attached below has the Account class that was designed to model a bank account.

    IN JAVAThe code is attached below has the Account class that was designed to model a bank account. An account has the properties account number, balance, annual interest rate, and date created, and methods to deposit and withdraw funds. I need creat program using the 2 java programs.Create two subclasses for checking and savings accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn.I need to write a test program that creates objects of Account,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT