Please Show Output of the final program ! Tasks: Create and test an inheritance
ID: 3579826 • Letter: P
Question
Please Show Output of the final program !
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
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 << " transaction fee charged." << endl;
}
------------------------------------------------------------------------------------------------------------------------
main.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(50.0);
SavingsAccount account2(25.0, .03);
CheckingAccount account3(80.0, 1.0);
cout << fixed << setprecision(2);
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);
cout << " Attempting to debit $30.00 from account2." << endl;
account2.debit(30.0);
cout << " Attempting to debit $40.00 from account3." << endl;
account3.debit(40.0);
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);
cout << " Crediting $65.00 to account2." << endl;
account2.credit(65.0);
cout << " Crediting $20.00 to account3." << endl;
account3.credit(20.0);
cout << " account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;
cout << "account3 balance: $" << account3.getBalance() << endl;
double interestEarned = account2.calculateInterest();
cout << " Adding $" << interestEarned << " interest to account2."<< endl;
account2.credit(interestEarned);
cout << " New account2 balance: $" << account2.getBalance() << endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.