(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 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 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 con- structor 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 bal- ance 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 Account. SavingsAccount’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 addi- tional data member of type double that represents the fee charged per transaction. Checking- Account’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 per- form 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.
What will be an ideal response?
```
// Definition of Account class.
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account
{
public:
Account( double ); // constructor initializes balance
void credit( double ); // add an amount to the account balance
bool debit( double ); // subtract an amount from the account balance
void setBalance( double ); // sets the account balance
double getBalance(); // return the account balance
private:
double balance; // data member that stores the balance
}; // end class Account
#endif
```
```
// Member-function definitions for class Account.
#include
#include "Account.h" // include definition of class Account
using namespace std;
// Account constructor initializes data member balance
Account::Account( double initialBalance )
{
// if initialBalance is greater than or equal to 0.0, set this value
// as the balance of the Account
if ( initialBalance >= 0.0 )
balance = initialBalance;
else // otherwise, output message and set balance to 0.0
{
cout << "Error: Initial balance cannot be negative." << endl;
balance = 0.0;
} // end if...else
} // end Account constructor
// credit (add) an amount to the account balance
void Account::credit( double amount )
{
balance = balance + amount; // add amount to balance
} // end function credit
// debit (subtract) an amount from the account balance
// return bool indicating whether money was debited
bool Account::debit( double amount )
{
if ( amount > balance ) // debit amount exceeds balance
{
cout << "Debit amount exceeded account balance." << endl;
return false;
} // end if
else // debit amount does not exceed balance
{
balance = balance - amount;
return true;
} // end else
} // end function debit
// set the account balance
void Account::setBalance( double newBalance )
{
balance = newBalance;
} // end function setBalance
// return the account balance
double Account::getBalance()
{
return balance;
} // end function getBalance
```
```
// Definition of SavingsAccount class.
#ifndef SAVINGS_H
#define SAVINGS_H
#include "Account.h" // Account class definition
class SavingsAccount : public Account
{
public:
// constructor initializes balance and interest rate
SavingsAccount( double, double );
double calculateInterest(); // determine interest owed
private:
double interestRate; // interest rate (percentage) earned by account
}; // end class SavingsAccount
#endif
```
```
// Member-function definitions for class SavingsAccount.
#include "SavingsAccount.h" // SavingsAccount class definition
// constructor initializes balance and interest rate
SavingsAccount::SavingsAccount( double initialBalance, double rate )
: Account( initialBalance ) // initialize base class
{
interestRate = ( rate < 0.0 ) ? 0.0 : rate; // set interestRate
} // end SavingsAccount constructor
// return the amount of interest earned
double SavingsAccount::calculateInterest()
{
return getBalance() * interestRate;
} // end function calculateInterest
```
```
// Definition of CheckingAccount class.
#ifndef CHECKING_H
#define CHECKING_H
#include "Account.h" // Account class definition
class CheckingAccount : public Account
{
public:
// constructor initializes balance and transaction fee
CheckingAccount( double, double );
void credit( double ); // redefined credit function
bool debit( double ); // redefined debit function
private:
double transactionFee; // fee charged per transaction
// utility function to charge fee
void chargeFee();
}; // end class CheckingAccount
#endif
```
```
// Member-function definitions for class CheckingAccount.
#include
#include "CheckingAccount.h" // CheckingAccount class definition
using namespace std;
// constructor initializes balance and transaction fee
CheckingAccount::CheckingAccount( double initialBalance, double fee )
: Account( initialBalance ) // initialize base class
{
transactionFee = ( fee < 0.0 ) ? 0.0 : fee; // set transaction fee
} // end CheckingAccount constructor
// credit (add) an amount to the account balance and charge fee
void CheckingAccount::credit( double amount )
{
Account::credit( amount ); // always succeeds
chargeFee();
} // end function credit
// debit (subtract) an amount from the account balance and charge fee
bool CheckingAccount::debit( double amount )
{
bool success = Account::debit( amount ); // attempt to debit
if ( success ) // if money was debited, charge fee and return true
{
chargeFee();
return true;
} // end if
else // otherwise, do not charge fee and return false
return false;
} // end function debit
// subtract transaction fee
void CheckingAccount::chargeFee()
{
Account::setBalance( getBalance() - transactionFee );
cout << "$" << transactionFee << " transaction fee charged." << endl;
} // end function chargeFee
```
```
// Test program for Account hierarchy.
#include
#include
#include "Account.h" // Account class definition
#include "SavingsAccount.h" // SavingsAccount class definition
#include "CheckingAccount.h" // CheckingAccount class definition
using namespace std;
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: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;
cout << "account3 balance: $" << account3.getBalance() << endl;
cout << "\nAttempting to debit $25.00 from account1." << endl;
account1.debit( 25.0 ); // try to debit $25.00 from account1
cout << "\nAttempting to debit $30.00 from account2." << endl;
account2.debit( 30.0 ); // try to debit $30.00 from account2
cout << "\nAttempting to debit $40.00 from account3." << endl;
account3.debit( 40.0 ); // try to debit $40.00 from account3
// display balances
cout << "\naccount1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;
cout << "account3 balance: $" << account3.getBalance() << endl;
cout << "\nCrediting $40.00 to account1." << endl;
account1.credit( 40.0 ); // credit $40.00 to account1
cout << "\nCrediting $65.00 to account2." << endl; account2.credit( 65.0 ); //
credit $65.00 to account2
cout << "\nCrediting $20.00 to account3." << endl;
account3.credit( 20.0 ); // credit $20.00 to account3
// display balances
cout << "\naccount1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;
cout << "account3 balance: $" << account3.getBalance() << endl;
// add interest to SavingsAccount object account2
double interestEarned = account2.calculateInterest();
cout << "\nAdding $" << interestEarned << " interest to account2."
<< endl;
account2.credit( interestEarned );
cout << "\nNew account2 balance: $" << account2.getBalance() << endl;
} // end main
```
account1 balance: $50.00
account2 balance: $25.00
account3 balance: $80.00
Attempting to debit $25.00 from account1.
Attempting to debit $30.00 from account2.
Debit amount exceeded account balance.
Attempting to debit $40.00 from account3.
$1.00 transaction fee charged.
account1 balance: $25.00
account2 balance: $25.00
account3 balance: $39.00
Crediting $40.00 to account1.
Crediting $65.00 to account2.
Crediting $20.00 to account3.
$1.00 transaction fee charged.
account1 balance: $65.00
account2 balance: $90.00
account3 balance: $58.00
Adding $2.70 interest to account2.
New account2 balance: $92.70
You might also like to view...
Microsoft Word has several features that will help you when editing a document. Please describe the following: insertion point, word wrap, and Overtype/Insert mode.
What will be an ideal response?
What is RGB?
What will be an ideal response?