Need to get the following output by Editing ChekingAccount.h ,ChekingAccount.cpp
CheckingAccount
Derived class CheckingAccount that inherits from base class Account and include an additional data member of type double that represents the fee charged per transaction (transactionFee).
Write Checking- Account’s constructor that receives the initial balance, as well as a parameter indicating a transaction fee amount. If transaction fee is less than zero, the transactionFee will be set to zero.
Write the chargeFee member function that updates the balance by deducting the transactionFee from the balance.
Override member functions debit for class CheckingAccount so that it subtracts the transactionFee from the account balance (call chargeFee). If the operation is successful, it will return true otherwise it does nothing and will return false (debit is successful if the amount is not greater than the balance). CheckingAccount’s versions of this function should invoke the base-class Account version to perform the debit operation.
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.
Override member functions credit for class CheckingAccount so that it subtracts the transactionFee from the account balance (call chargeFee). CheckingAccount’s versions of this function should invoke the base-class Account version to perform the credit operation.
Override the display function in the Account class that insert that print a SavingsAccount in the following format (example):
Account.h:
#ifndef ICT_ACCOUNT_H__
#define ICT_ACCOUNT_H__
#include
using namespace std;
namespace ict
{
class Account
{
private:
double balance;
// data member that stores the balance
protected:
double
getBalance() const; // return the account balance
void setBalance(
double ); // sets the account balance
public:
// TODO: Write a
prototype for constructor which initializes balance
Account(double);
// TODDO: Write
a function prototype for the virtual function credit
virtual void
credit(double);
// TODO: Write a
function prototype for the virtual function debit
virtual bool
debit(double);
// TODO: Write a
function prototype for the virtual function display
// Add the
prototype of pure virtual function display that receives a
reference to ostream.
virtual void
display(ostream &) = 0;
};
};
#endif
Account.cpp:
#include "Account.h"
using namespace std;
namespace ict
{
// constructor
// Write the constructor that validates the
initial balance to ensure that it’s greater
// than or equal to 0. If not, set the balance
to the safe empty state
// (balance equals to -1.0).
Account::Account(double bal)
{
if(bal >= 0)
balance =
bal;
else
balance =
-1.0;
}
// credit (add) an amount to the account
balance
//Write the virtual member function credit that
adds an amount to the current balance.
void Account::credit(double amount)
{
balance += amount;
}
// debit (subtract) an amount from the account
balance return bool
// Write the virtual member function debit that
withdraws money from the account and
// ensure that the debit amount does not exceed
the Account’s balance. If the balance
// is less the amount, the balance should be
left unchanged and the function should
// return false (otherwise it should return
true).
bool Account::debit(double amount)
{
if(amount > balance)
return
false;
balance -= amount;
return true;
}
double Account::getBalance() const
{ return balance; }
void Account::setBalance( double newBalance
) { balance = newBalance;
}
}
SavingsAccount.h:
#ifndef ICT_SAVINGSACCOUNT_H__
#define ICT_SAVINGSACCOUNT_H__
#include "Account.h"
using namespace std;
namespace ict
{
class SavingsAccount : public Account
{
private:
double
interestRate; // interest rate (percentage)
public:
// TODO: put
prototypes here
SavingsAccount(double bal, double interest);
double
calculateInterest();
void
display(ostream&);
};
};
#endif
SavingsAccount.cpp:
#include "SavingAccount.h"
#include
using namespace std;
namespace ict
{
// TODO: Implement SavingsAccount member
functions here
// Write the SavingsAccount’s constructor that
receives the initial balance, as well
// as an initial value for the SavingsAccount’s
interest rate, and then initializes
// the object. If interest rate is less than
zero, the interstRate will be set to zero.
SavingsAccount::SavingsAccount(double bal,
double interest) : Account(bal)
{
if(interest < 0)
interestRate =
0;
else
interestRate =
interest;
}
// Write the public member function
calculateInterest for SavingsAccount class that
// returns the amount of interest earned by an
account. Member function
// calculateInterest should determine this
amount by multiplying the interest rate
// by the account balance.
double SavingsAccount::calculateInterest()
{
return interestRate *
getBalance();
}
void SavingsAccount::display(ostream
&out)
{
out << "Account type: Saving"
<< endl;
out << "Balance: $ " <<
fixed << setprecision(2) << getBalance() <<
endl;
out << "interest Rate (%): "
<< fixed << setprecision(2) << interestRate * 100
<< endl;
}
}
Main.cpp
#include
#include "Account.h"
#include "SavingsAccount.h"
#include "CheckingAccount.h"
using namespace ict;
using namespace std;
int main()
{
// Create Account for Angelina
Account * Angelina_Account[2];
// initialize Angelina Accounts
Angelina_Account[ 0 ] = new SavingsAccount( 400.0, 0.12 );
Angelina_Account[ 1 ] = new CheckingAccount( 400.0, 1.0);
cout << "**************************" << endl;
cout << "DISPLAY Angelina Accounts:" << endl;
cout << "**************************" << endl;
Angelina_Account[0]->display(cout);
cout << "-----------------------" << endl;
Angelina_Account[1]->display(cout);
cout << "**************************" << endl ;
cout << "DEPOSIT $ 2000 $ into Angelina Accounts ..." << endl ;
for(int i=0 ; i < 2 ; i++){
Angelina_Account[i]->credit(2000);
}
cout << "WITHDRAW $ 1000 and $ 500 from Angelina Accounts ..." << endl;
Angelina_Account[0]->debit(1000);
Angelina_Account[1]->debit(500);
cout << "**************************" << endl;
cout << "DISPLAY Angelina Accounts:" << endl;
cout << "**************************" << endl;
Angelina_Account[0]->display(cout);
cout << "-----------------------" << endl;
Angelina_Account[1]->display(cout);
cout << "-----------------------" << endl;
return 0;
}
The following is the exact output of the tester program:
DISPLAY Angelina Accounts:
**************************
Account type: Saving
Balance: $ 400.00
Interest Rate (%): 12.00
-----------------------
Account type: Checking
Balance: $ 400.00
Transaction Fee: 1.00
**************************
DEPOSIT $ 2000 $ into Angelina Accounts ...
WITHDRAW $ 1000 and $ 500 from Angelina Accounts ...
**************************
DISPLAY Angelina Accounts:
**************************
Account type: Saving
Balance: $ 1400.00
Interest Rate (%): 12.00
-----------------------
Account type: Checking
Balance: $ 1898.00
Transaction Fee: 1.00
-----------------------
ChekingAccount.cpp
#include "CheckingAccount.h" |
using namespace std; |
namespace ict{ |
// TODO: define CheckingAccount class member functions here |
}
ChekingAccount.h
#ifndef ICT_CHECKINGACCOUNT_H__ |
#define ICT_CHECKINGACCOUNT_H__ |
#include "Account.h" |
using namespace std; |
namespace ict{ |
class CheckingAccount : public Account { |
private: |
double transactionFee; |
// TODO: chargeFee subtract transaction fee form the balance |
public: |
// TODO: constructor initializes balance and transaction fee |
// TODO: Write a function prototype to override credit function |
// TODO: Write a function prototype to override debit function |
// TODO: Write a function prototype to override display function |
}; |
}; |
#endif
#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
// Account.cpp // Member-function definitions for class Account. #include <iostream> using namespace std; #include "Account.h" // include definition of class Account // 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
// SavingsAccount.h // Definition of SavingsAccount class. #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
// Lab 1: 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 // subtract transaction fee /* Write the chargeFee member function to subtract transactionFee from the current balance and display a message */
// 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 << "\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 /* Declare a variable interestEarned and assign it the interest account2 should earn */ cout << "\nAdding $" << interestEarned << " interest to account2." << endl; /* Write a statement to credit the interest to account2's balance */ cout << "\nNew account2 balance: $" << account2.getBalance() << endl; } // end main
Get Answers For Free
Most questions answered within 1 hours.