Devam Ediyor

493278 Account Inheritance Hierarchy

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 [url removed, login to view] 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 classes Savings-Account and CheckingAccount that inherit from class 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 balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it is greater than or equal to 0.0. If not, the balance should be set to 0.0 and the constructor should display an error message, indicating that the initial balance was invalid. The class should provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money from the Account and ensure that the debit amount does not exceed the Account's balance.

If it does, the balance should be left unchanged and the function should print the message "Debit amount exceeded account balance." Member function getBalance should return the current balance.

Derived class SavingsAccount should inherit the functionality of an Account, but also include a data member of type double indicating the interest rate (percentage) assigned to the [url removed, login to view]'s constructor should receive the initial balance, as well as an initial value for the SavingsAccount's interest rate. SavingsAccount should provide a public member function calculateInterest that returns a double indicating the amount of interest earned by an account.

Member function calculateInterest should determine this amount by multiplying the interest rate by the account balance. [Note: SavingsAccount should inherit member functions credit and debit as is without redefining them.]

Derived class CheckingAccount should inherit from base class Account and include an additional data member of type double that represents the fee charged per transaction. CheckingAccount's constructor should receive the initial balance, as well as a parameter indicating a fee amount. Class CheckingAccount should redefine member functions credit and debit so that they subtract the fee from the account balance whenever either transaction is performed successfully. CheckingAccount's versions of these functions should invoke the base-class Account version to perform the updates to an account balance. CheckingAccount's debit function should charge a fee only if money is actually withdrawn (i.e., the debit amount does not exceed the account balance). [Hint: Define Account's debit function so that it returns a bool indicating whether money was withdrawn. Then use the return value to determine whether a fee should be charged.]

After defining the classes in this hierarchy, write a program that creates objects of each class and tests their member functions. Add interest to the SavingsAccount object by first invoking its calculateInterest function, then passing the returned interest amount to the object's credit function.

Sample Output:

account1 balance: $[url removed, login to view]

account2 balance: $[url removed, login to view]

account3 balance: $[url removed, login to view]

Attempting to debit $[url removed, login to view] from account1.

Attempting to debit $[url removed, login to view] from account2.

Attempting to debit $[url removed, login to view] from account3.

$[url removed, login to view] transaction fee charged.

account1 balance: $[url removed, login to view]

account2 balance: $[url removed, login to view]

account3 balance: $[url removed, login to view]

Crediting $[url removed, login to view] to account1.

Crediting $[url removed, login to view] to account2.

Crediting $[url removed, login to view] to account3.

$[url removed, login to view] transaction fee charged.

account1 balance: $[url removed, login to view]

account2 balance: $[url removed, login to view]

account3 balance: $[url removed, login to view]

Attempting to debit $[url removed, login to view] from account1.

Debit amount exceeded account balance.

Attempting to debit $[url removed, login to view] from account2.

Debit amount exceeded account balance.

Attempting to debit $[url removed, login to view] from account3.

Debit amount exceeded account balance.

account1 balance: $[url removed, login to view]

account2 balance: $[url removed, login to view]

account3 balance: $[url removed, login to view]

Adding $[url removed, login to view] interest to account2.

New account2 balance: $[url removed, login to view]

Initial balance is invalid.

Initial balance is invalid.

Initial balance is invalid.

account4 balance: $[url removed, login to view]

account5 balance: $[url removed, login to view]

account6 balance: $[url removed, login to view]

Driver Program:

#include <iostream>

#include <iomanip>

using namespace std;

#include "Account.h"

#include "SavingsAccount.h"

#include "CheckingAccount.h"

int main()

{

Account account1( 50.0 ); // create Account object

SavingsAccount account2( 25.0, .03 ); // create SavingsAccount object

CheckingAccount account3( 80.0, 1.0 ); // create CheckingAccount object

cout << fixed << setprecision( 2 );

// display initial balance of each object

cout << "account1 balance: $" << [url removed, login to view]() << endl;

cout << "account2 balance: $" << [url removed, login to view]() << endl;

cout << "account3 balance: $" << [url removed, login to view]() << endl;

cout << endl;

// attempt valid debits

cout << "Attempting to debit $[url removed, login to view] from account1." << endl;

[url removed, login to view]( 25.0 ); // try to debit $[url removed, login to view] from account1

cout << "Attempting to debit $[url removed, login to view] from account2." << endl;

[url removed, login to view]( 13.0 ); // try to debit $[url removed, login to view] from account2

cout << "Attempting to debit $[url removed, login to view] from account3." << endl;

[url removed, login to view]( 40.0 ); // try to debit $[url removed, login to view] from account3

cout << endl;

// display balances

cout << "account1 balance: $" << [url removed, login to view]() << endl;

cout << "account2 balance: $" << [url removed, login to view]() << endl;

cout << "account3 balance: $" << [url removed, login to view]() << endl;

cout << endl;

// attempt valid credits

cout << "Crediting $[url removed, login to view] to account1." << endl;

[url removed, login to view]( 40.0 ); // credit $[url removed, login to view] to account1

cout << "Crediting $[url removed, login to view] to account2." << endl;

[url removed, login to view]( 65.0 ); // credit $[url removed, login to view] to account2

cout << "Crediting $[url removed, login to view] to account3." << endl;

[url removed, login to view]( 20.0 ); // credit $[url removed, login to view] to account3

cout << endl;

// display balances

cout << "account1 balance: $" << [url removed, login to view]() << endl;

cout << "account2 balance: $" << [url removed, login to view]() << endl;

cout << "account3 balance: $" << [url removed, login to view]() << endl;

cout << endl;

// attempt invalid debits

cout << "Attempting to debit $[url removed, login to view] from account1." << endl;

[url removed, login to view]( 125.0 ); // try to debit $[url removed, login to view] from account1

cout << "Attempting to debit $[url removed, login to view] from account2." << endl;

[url removed, login to view]( 113.0 ); // try to debit $[url removed, login to view] from account2

cout << "Attempting to debit $[url removed, login to view] from account3." << endl;

[url removed, login to view]( 140.0 ); // try to debit $[url removed, login to view] from account3

cout << endl;

// display balances

cout << "account1 balance: $" << [url removed, login to view]() << endl;

cout << "account2 balance: $" << [url removed, login to view]() << endl;

cout << "account3 balance: $" << [url removed, login to view]() << endl;

cout << endl;

// add interest to SavingsAccount object account2

double interestEarned = [url removed, login to view]();

cout << "Adding $" << interestEarned << " interest to account2."

<< endl;

[url removed, login to view]( interestEarned );

cout << "New account2 balance: $" << [url removed, login to view]() << endl;

cout << endl;

// create accounts with invalid initial balances

Account account4( -50.0 ); // create Account object

SavingsAccount account5( -25.0, .03 ); // create SavingsAccount object

CheckingAccount account6( -80.0, 1.0 ); // create CheckingAccount object

// display initial balance of each object

cout << "account4 balance: $" << [url removed, login to view]() << endl;

cout << "account5 balance: $" << [url removed, login to view]() << endl;

cout << "account6 balance: $" << [url removed, login to view]() << endl;

cout << endl;

return 0;

} // end main

Beceriler: Her şey Kabul, PHP

Daha fazlasını görün: account inheritance hierarchy, account inheritance hierarchy create, customers bank, credit account, balance define, std, print hold, endl, bool, earn money fee, fee earn money, checking account program, earn base, php print error, member function php, write program bank account, program checking account, function sample, program sample bank account, charge account, checking account, fixed deposit, data customers bank, value double, php write error

İşveren Hakkında:
( 2 değerlendirme )

Proje NO: #2239192