Create an inheritance hierarchy containing baseclass Account

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 of 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. 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 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 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 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 the return value to determine whether 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.

Solution

#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

#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

#ifndef SAVINGS_H

#define SAVINGS_H

#include \"Account.h\"

class SavingsAccount : public Account

{

public:

SavingsAccount( double, double );

double calculateInterest();

private:

double interestRate;

};

#endif

#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; // add amount to balanc

}

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;

}

#include <iostream>

using namespace std;

#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;

}

#include \"SavingsAccount.h\"

SavingsAccount::SavingsAccount( double initialBalance, double rate ): Account( initialBalance )

{

interestRate = ( rate < 0.0 ) ? 0.0 : rate;

}

double SavingsAccount::calculateInterest()

{

return getBalance() * interestRate;

}

#include <iostream>

using namespace std;

#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;

}

Create an inheritance hierarchy containing base-class Account and derived classes SavingsAccount and CheckingAccount that inherit from class Account. Base-class
Create an inheritance hierarchy containing base-class Account and derived classes SavingsAccount and CheckingAccount that inherit from class Account. Base-class
Create an inheritance hierarchy containing base-class Account and derived classes SavingsAccount and CheckingAccount that inherit from class Account. Base-class
Create an inheritance hierarchy containing base-class Account and derived classes SavingsAccount and CheckingAccount that inherit from class Account. Base-class
Create an inheritance hierarchy containing base-class Account and derived classes SavingsAccount and CheckingAccount that inherit from class Account. Base-class
Create an inheritance hierarchy containing base-class Account and derived classes SavingsAccount and CheckingAccount that inherit from class Account. Base-class

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site