Tasks: Create and test an inheritance hierarchy containing a base-class Account
ID: 3575312 • Letter: T
Question
Tasks: Create and test an inheritance hierarchy containing a base-class Account and two derived classesSavingAccount and CheckingAccount. (C++)
The base class Account:
-The class contains following private data members: accountNumber (integer from 1000 to 4999),accountOwner (name as a string), accountBalance (type double).
-The constructor receives account number, account owner's name, and an initial balance as parameters. If the initial balance is less than 0.0 the constructor should set the account balance to 0.0 and also issue an error message such as "Initial balance was invalid."
-The class provides three member functions:
1.credit: adds an amount to the current balance.
2.debit: withdraw money from the account and ensure that the debit amount does not exceed the current balance. If it does, the balance should be left unchanged, and the function should print the message likes "Debit amount exceeded account balance."
3. getBalance: returns and print out the current balance. This is a const function.
Derived class: SavingsAccount
-The SavingsAccount should inherit the functionality of an Account, but also include a private data memberinterestRate (type double value such as 0.03).
-The constructor receives an account number, account owner's name, an initial balance, as well as initial value of interest rate.
-This class should provide a public member function calculateInterest which returns the amount of interest that is calculated by multiplying the interest rate with the account balance. Don't add the interest to account balance using this function.
-The class should inherit credit and debit without redefining them
Derived class: CheckingAccount
-The CheckingAccount includes a new data member fee (type double) that represent the fee charged per transaction.
-The constructor should receive the account number, account owner, initial balance and a parameter indicating a fee amount.
-It should redefine the credit and debit function so that they subtract the fee from account balance whenever each transaction is performed successfully. The debit function should charge the fee only if money is actually withdrawn
Write a test driver program that creates objects of all three classes (Account, SavingsAccount, and CheckingAccount) and tests their member functions. In testing the SavingsAccount, add interest to the SavingsAccount object by first invoking its calculateInterest function, then passing the returned interest amount to the object's credit function.
C++ Program 2.
Tasks: Develop a polymorphism banking program using the Account hierarchy defined in Programming Assignment 4. This assignment uses the Account, SavingsAccount and CheckingAccount classes defined in Assignment 4 to make polymorphism works.
1. Create a vector of Account pointers that can point to Account, SavingsAccountandCheckingAccountobjects.
Use the C++ STL vector template for this task. Chapter 7 of textbook contain information about using vector template. Fig. 12.19 on p. 568 of textbook also contain sample codes.
2. Create different types of accounts (objects) using the new operator and assign their addresses to the pointers in the vector. Refer to Fig. 12.19 for sample codes.
3. For each account in the vector, allow the user to specify an amount of money to deposit or withdraw from the account. The polymorphism will dynamically decide on which credit or debit member functions to use for each class.
You may have to declare some member functions (e.g. credit, debit, printAccount, etc.) as “virtual functions” in the base class to make the polymorphism working.
Do not try to use “pure virtual function” in base class Account because this would make it impossible to create objects of this class.
4. After processing each transaction (credit, debit, checkBalance, …), print appropriate message about the transaction including account number or owner, the type of account (e.g. SavingsAccount), amount of money involved, and the result of transaction.
To find out the type of account, you can use the Runtime Type Information (RTTI) capability (e.g. typeidand name functions). See Fig.12.19 in textbook for sample codes. An alternative way is to define a member variable (a string) in class definition to store the name of account’s type and initialize its value with class name when an account object is created.
5. An optional exercise is to define virtual destructor for each class to release the memory space of each object (because the object was created using the new operator). This part is optional and won’t be counted for grading this assignment.
6.In your main program:
-Create five or more different bank account objects dynamically (using new operator) and store their addresses in the vector you created.
-Perform various transactions on these accounts to test the member functions and see how polymorphism works.
7. In item (4): Using RTTI to find out the type of account objects is optional. However, if you implement the following, you earn two bonus points.
Implement a member function getClass ID which uses the Runtime Type Information (RTTI) capability (e.g.typeid and name functions) to find the name string of the class that the current object belong to. For example, if the pointer points to a SavingsAccount object, then this member function should return a “SavingsAccount” string.
In the main function, use a for-loop to traverse through the vector that contains pointers to the accounting objects, and print out the class name of each object in the vector.
8. In item (5): Define virtual destructor for each class to release the memory space of each object (because the object was created using the new operator). Originally, this part is optional and won’t be counted for grading this assignment. But if you implement this part, two bonus points will be award.
Explanation / Answer
Solution:
//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 namespace std;
#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
/* Write a directive to include the Account header file */
/* Write a line to have class SavingsAccount inherit publicly from Account */
{
public:
// constructor initializes balance and interest rate
/* Declare a two-parameter constructor for SavingsAccount */
/* Declare member function calculateInterest */
private:
/* Declare data member interestRate */
}; // end class SavingsAccount
#endif
=======================================================
//SavingsAccount.cpp
// Member-function definitions for class SavingsAccount.
#include "SavingsAccount.h" // SavingsAccount class definition
// constructor initializes balance and interest rate
/* Write the SavingsAccount constructor to call the Account constructor and validate and set the interest rate value */
// return the amount of interest earned
/* Write the calculateInterest member function to return the interest based on the current balance and interest rate */
========================================================
// CheckingAccount.h
// Definition of CheckingAccount class.
#ifndef CHECKING_H
#define CHECKING_H
/* Write a directive to include the Account header file */
/* Write a line to have class CheckingAccount inherit publicly from Account */
{
public:
// constructor initializes balance and transaction fee
/* Declare a two-argument constructor for CheckingAccount */
/* Redeclare member function credit, which will be redefined */
/* Redeclare member function debit, which will be redefined */
private:
/* Declare data member transactionFee */
// utility function to charge fee
/* Declare member function chargeFee */
}; // end class CheckingAccount
#endif
========================================================================
//CheckingAccount.cpp
// Member-function definitions for class CheckingAccount.
#include <iostream>
using namespace std;
#include "CheckingAccount.h" // CheckingAccount class definition
// constructor initializes balance and transaction fee
/* Write the CheckingAccount constructor to call the Account constructor and validate and set the transaction fee value */
// credit (add) an amount to the account balance and charge fee
/* Write the credit member function to call Account's credit function and then charge a fee */
// debit (subtract) an amount from the account balance and charge fee
/* Write the debit member function to call Account's debit function and then charge a fee if it returned true*/
// subtract transaction fee
/* Write the chargeFee member function to subtract transactionFee from the current balance and display a message */
=================================================================
// bankAccounts.cpp
// Test program for Account hierarchy.
#include <iostream>
#include <iomanip>
using namespace std;
#include "Account.h" // Account class definition
#include "SavingsAccount.h" // SavingsAccount class definition
#include "CheckingAccount.h" // CheckingAccount class definition
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 << " Attempting to debit $25.00 from account1." << endl;
account1.debit( 25.0 ); // try to debit $25.00 from account1
cout << " Attempting to debit $30.00 from account2." << endl;
account2.debit( 30.0 ); // try to debit $30.00 from account2
cout << " Attempting to debit $40.00 from account3." << endl;
account3.debit( 40.0 ); // try to debit $40.00 from account3
// display balances
cout << " account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;
cout << "account3 balance: $" << account3.getBalance() << endl;
cout << " Crediting $40.00 to account1." << endl;
account1.credit( 40.0 ); // credit $40.00 to account1
cout << " Crediting $65.00 to account2." << endl;
account2.credit( 65.0 ); // credit $65.00 to account2
cout << " Crediting $20.00 to account3." << endl;
account3.credit( 20.0 ); // credit $20.00 to account3
// display balances
cout << " account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;
cout << "account3 balance: $" << account3.getBalance() << endl;
// add interest to SavingsAccount object account2
/* Declare a variable interestEarned and assign it the interest account2 should earn */
cout << " Adding $" << interestEarned << " interest to account2." << endl;
/* Write a statement to credit the interest to account2's balance */
cout << " New account2 balance: $" << account2.getBalance() << endl;
} // end main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.