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

Create an inheritance hierarchy that a bank might use to represent customer’s ba

ID: 3730861 • Letter: C

Question

Create an inheritance hierarchy that a bank might use to represent

customer’s 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 SavingsAccount and CheckingAccount that inherit from class Account. Base class Account

should include one data member 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’s 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

Account. SavingsAccount’s constructor should receive the initial balance, as well as an initial value for the SavingsAccount’s interest rate. SavingsAcount 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. 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 function 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 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 SavingAccount object by first invoking its calculateInterest function, then passing the returned interest amount to the object’s credit function.

Explanation / Answer

Hi friend, you have not mentioned about Programming language.

Please try to mention all details.

I have implemented in C++.

ANS:
1 // Solution 12.10 Solution: Account.h
2 // Definition of Account class.
3 #ifndef ACCOUNT_H
4 #define ACCOUNT_H
56
class Account
7 {
8 public:
9 Account( double ); // constructor initializes balance
10 void credit( double ); // add an amount to the account balance
11 bool debit( double ); // subtract an amount from the account balance
12 void setBalance( double ); // sets the account balance
13 double getBalance(); // return the account balance
14 private:
15 double balance; // data member that stores the balance
16 }; // end class Account
17
18 #endif
1 // Exercise 12.10 Solution: Account.cpp
2 // Member-function definitions for class Account.
3 #include <iostream>
4 #include "Account.h" // include definition of class Account
5 using namespace std;
67
// Account constructor initializes data member balance
8 Account::Account( double initialBalance )
9 {
10 // if initialBalance is greater than or equal to 0.0, set this value
11 // as the balance of the Account
12 if ( initialBalance >= 0.0 )
13 balance = initialBalance;
14 else // otherwise, output message and set balance to 0.0
15 {
16 cout << "Error: Initial balance cannot be negative." << endl;
17 balance = 0.0;
18 } // end if...else
19 } // end Account constructor
20
21 // credit (add) an amount to the account balance
22 void Account::credit( double amount )
23 {
24 balance = balance + amount; // add amount to balance
25 } // end function credit
26
27 // debit (subtract) an amount from the account balance
28 // return bool indicating whether money was debited
29 bool Account::debit( double amount )
30 {
31 if ( amount > balance ) // debit amount exceeds balance
32 {
33 cout << "Debit amount exceeded account balance." << endl;
34 return false;
35 } // end if
36 else // debit amount does not exceed balance
37 {
38 balance = balance - amount;
39 return true;
40 } // end else
41 } // end function debit
42
43 // set the account balance
44 void Account::setBalance( double newBalance )
45 {
46 balance = newBalance;
47 } // end function setBalance
48
49 // return the account balance
50 double Account::getBalance()
51 {
52 return balance;
53 } // end function getBalance
1 // Exercise 12.10 Solution: SavingsAccount.h
2 // Definition of SavingsAccount class.
3 #ifndef SAVINGS_H
4 #define SAVINGS_H
56
#include "Account.h" // Account class definition
78
class SavingsAccount : public Account
9 {
10 public:
11 // constructor initializes balance and interest rate
12 SavingsAccount( double, double );
13
14 double calculateInterest(); // determine interest owed
15 private:
16 double interestRate; // interest rate (percentage) earned by account
17 }; // end class SavingsAccount
18
19 #endif
1 // Exercise 12.10 Solution: SavingsAccount.cpp
2 // Member-function definitions for class SavingsAccount.
34
#include "SavingsAccount.h" // SavingsAccount class definition
56
// constructor initializes balance and interest rate
7 SavingsAccount::SavingsAccount( double initialBalance, double rate )
8 : Account( initialBalance ) // initialize base class
9 {
10 interestRate = ( rate < 0.0 ) ? 0.0 : rate; // set interestRate
11 } // end SavingsAccount constructor
12
13 // return the amount of interest earned
14 double SavingsAccount::calculateInterest()
15 {
16 return getBalance() * interestRate;
17 } // end function calculateInterest
1 // Exercise 12.10 Solution: CheckingAccount.h
2 // Definition of CheckingAccount class.
3 #ifndef CHECKING_H
4 #define CHECKING_H
56
#include "Account.h" // Account class definition
78
class CheckingAccount : public Account
9 {
10 public:
11 // constructor initializes balance and transaction fee
12 CheckingAccount( double, double );
13
14 void credit( double ); // redefined credit function
15 bool debit( double ); // redefined debit function
16 private:
17 double transactionFee; // fee charged per transaction
18
19 // utility function to charge fee
20 void chargeFee();
21 }; // end class CheckingAccount
22
23 #endif
1 // Exercise 12.10 Solution: CheckingAccount.cpp
2 // Member-function definitions for class CheckingAccount.
3 #include <iostream>
4 #include "CheckingAccount.h" // CheckingAccount class definition
5 using namespace std;
67
// constructor initializes balance and transaction fee
8 CheckingAccount::CheckingAccount( double initialBalance, double fee )
9 : Account( initialBalance ) // initialize base class
10 {
11 transactionFee = ( fee < 0.0 ) ? 0.0 : fee; // set transaction fee
12 } // end CheckingAccount constructor
13
14 // credit (add) an amount to the account balance and charge fee
15 void CheckingAccount::credit( double amount )
16 {
17 Account::credit( amount ); // always succeeds
18 chargeFee();
19 } // end function credit
20
21 // debit (subtract) an amount from the account balance and charge fee
22 bool CheckingAccount::debit( double amount )
23 {
24 bool success = Account::debit( amount ); // attempt to debit
25
26 if ( success ) // if money was debited, charge fee and return true
27 {
28 chargeFee();
29 return true;
30 } // end if
31 else // otherwise, do not charge fee and return false
32 return false;
33 } // end function debit
34
35 // subtract transaction fee
36 void CheckingAccount::chargeFee()
37 {
38 Account::setBalance( getBalance() - transactionFee );
39 cout << "$" << transactionFee << " transaction fee charged." << endl;
40 } // end function chargeFee
1 // Exercise 12.10 Solution: ex12_10.cpp
2 // Test program for Account hierarchy.
3 #include <iostream>
4 #include <iomanip>
5 #include "Account.h" // Account class definition
6 #include "SavingsAccount.h" // SavingsAccount class definition
7 #include "CheckingAccount.h" // CheckingAccount class definition
8 using namespace std;
9
10 int main()
11 {
12 Account account1( 50.0 ); // create Account object
13 SavingsAccount account2( 25.0, .03 ); // create SavingsAccount object
14 CheckingAccount account3( 80.0, 1.0 ); // create CheckingAccount object
15
16 cout << fixed << setprecision( 2 );
17
18 // display initial balance of each object
19 cout << "account1 balance: $" << account1.getBalance() << endl;
20 cout << "account2 balance: $" << account2.getBalance() << endl;
21 cout << "account3 balance: $" << account3.getBalance() << endl;
22
23 cout << " Attempting to debit $25.00 from account1." << endl;
24 account1.debit( 25.0 ); // try to debit $25.00 from account1
25 cout << " Attempting to debit $30.00 from account2." << endl;
26 account2.debit( 30.0 ); // try to debit $30.00 from account2
27 cout << " Attempting to debit $40.00 from account3." << endl;
28 account3.debit( 40.0 ); // try to debit $40.00 from account3
29
30 // display balances
31 cout << " account1 balance: $" << account1.getBalance() << endl;
32 cout << "account2 balance: $" << account2.getBalance() << endl;
33 cout << "account3 balance: $" << account3.getBalance() << endl;
34
35 cout << " Crediting $40.00 to account1." << endl;
36 account1.credit( 40.0 ); // credit $40.00 to account1
37 cout << " Crediting $65.00 to account2." << endl;
38 account2.credit( 65.0 ); // credit $65.00 to account2
39 cout << " Crediting $20.00 to account3." << endl;
40 account3.credit( 20.0 ); // credit $20.00 to account3
41
42 // display balances
43 cout << " account1 balance: $" << account1.getBalance() << endl;
44 cout << "account2 balance: $" << account2.getBalance() << endl;
45 cout << "account3 balance: $" << account3.getBalance() << endl;
46
47 // add interest to SavingsAccount object account2
48 double interestEarned = account2.calculateInterest();
49 cout << " Adding $" << interestEarned << " interest to account2."
50 << endl;
51 account2.credit( interestEarned );
52
53 cout << " New account2 balance: $" << account2.getBalance() << endl;
54 } // 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

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