Question

In C++ Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit (i.e., credit) money into their accounts and withdraw (i.e.,...

In C++
Create an inheritance hierarchy that a bank might use to represent customers’ 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 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 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 SavingsAccount object by first invoking its calculateInterest function, then passing the returned interest amount to the object’s credit function.
2 2
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

#include <iostream>

using namespace std;

// Creating base class Account

class Account

{

private:

// Declaring variables

double balance;

public:

// parameterized constructor

Account(double balance)

{

if (balance > 0.0)

{

this->balance = balance;

}

else

{

this->balance = 0.0;

cout << "** Initial balance was invalid **" << endl;

}

}

// Function declarations

void credit(double amount);

bool debit(double amount);

double getBalance();

void setBalance(double amt);

};

/* Function implementation

* which gets current balance

*/

double Account::getBalance()

{

return this->balance;

}

void Account::setBalance(double amt)

{

this->balance = amt;

}

void Account::credit(double amount)

{

balance+=amount;

}

bool Account::debit(double amount)

{

if(balance>=amount)

{

balance-=amount;

return true;

}

else

{

return false;

}

}

// Creating the sub class derived from Base class Account

class SavingsAccount : public Account

{

private:

// Declaring variables

double rate;

public:

// parameterized constructor

SavingsAccount(double balance, double rate)

: Account(balance)

{

this->rate = rate;

}

// Function declarations

double calculateInterest();

// Function declarations

void credit(double amount);

bool debit(double amount);

};

/* Function implementation

* which calculates the interest amount

*/

double SavingsAccount::calculateInterest()

{

return getBalance() * (rate / 100);

}

/* Function implementation

* which adds money to the current balance

*/

void SavingsAccount::credit(double amt)

{

double interest = calculateInterest();

setBalance(getBalance() + amt + interest);

cout << "Account Balance after credit :$" << getBalance() << endl;

}

/* Function implementation

* which subtract money to the current balance

*/

bool SavingsAccount::debit(double amount)

{

if (amount > getBalance())

{

cout << "Debit amount exceeded account balance." << endl;

return false;

}

else

{

setBalance(getBalance() - amount);

return true;

}

}

// Creating a Checking Account class derivied from Account class

class CheckingAccount : public Account

{

private:

double feePerTx;

public:

// parameterized constructor

CheckingAccount(double balance, double feePerTx)

: Account(balance)

{

this->feePerTx = feePerTx;

}

// Function declarations

void credit(double amount);

bool debit(double amount);

};

/* Function implementation

* which adds money to the current balance

*/

void CheckingAccount::credit(double amount)

{

setBalance(amount - feePerTx);

}

/* Function implementation

* which subtract money to the current balance

*/

bool CheckingAccount::debit(double amount)

{

double amt = (amount + feePerTx);

if (!Account::debit(amt))

{

cout << "Debit amount exceeded account balance." << endl;

return false;

}

else

{

Account::debit(amt);

return true;

}

}

int main()

{

//Declaring variables

double damt,camt;

  

//Creating Instances of SavingsAccount class and CheckingAccount class

SavingsAccount sa1(50000, 7);

CheckingAccount ca1(65000, 2);

  

damt=20000;

camt=45000;

//Performing transactions on SavingsAccount

cout<<"__Savings Account__"<<endl;

cout<<"Account Balance :$"<<sa1.getBalance()<<endl;

sa1.debit(damt);

cout << "Account Balance After Debit $"<<damt<<" is :$"<< sa1.getBalance() << endl;

sa1.credit(camt);

cout << "Account Balance After Credit $"<<camt<<" is :$"<< sa1.getBalance() << endl;

cout<<"-----------------"<<endl;

  

//Performing transactions on CheckingAccount

cout<<"__Checking Account__"<<endl;

cout<<"Account Balance :$"<<ca1.getBalance()<<endl;

ca1.debit(damt);

cout << "Account Balance After Debit $"<<damt<<" is :$"<< ca1.getBalance() << endl;

ca1.credit(camt);

cout << "Account Balance After Credit $"<<camt<<" is :$"<< ca1.getBalance() << endl;

cout<<"-----------------"<<endl;

  

return 0;

}

__________________

Output:

CProgram Files (x86) Dev-Cpp\MinGW64)bin AccountSavingsCheckingCreditDebitTransactionFe... Savings Account Account Balance :S

________________Thank You

Add a comment
Know the answer?
Add Answer to:
In C++ Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit (i.e., credit) money into their accounts and withdraw (i.e.,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Should be in C# Create an inheritance hierarchy that a bank might use to represent customers’...

    Should be in C# Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this back 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. Create base class Account and derived classes SavingsAccount and CheckingAccount that inherit...

  • Please show it in C++. Thank you! Problem Definition Create an inheritance hierarchy containing base class...

    Please show it in C++. Thank you! Problem Definition Create an inheritance hierarchy containing base class Account and derived class Savings-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 baiance and uses it to initialize the data member. The class should provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money...

  • For this week's assignment , please create an application following directions in the attached document.(windows form...

    For this week's assignment , please create an application following directions in the attached document.(windows form application) Create a base class named Account and derived classes named SavingsAccount and CheckingAccount that inherit from class Account. Base class Account should include one private instance variable of type decimal to represent the account balance. The class should provide a constructor that receives an initial balance and uses it to initialize the instance variable with a public property. The property should validate the...

  • question

    : Implement the following given scenario in C++ a. Create an inheritance hierarchy by writing the source code, containing base class BankAccount and derived classes SavingsAccount and CheckingAccount that inherit from class BankAccount. b. Implement Polymorphism where required to achieve the polymorphic behavior. c. Multiple constructors be defined for initializing the account of a user. d. The class should provide four member functions. i. Member function Credit should add an amount to the current balance. ii. Member function Debit should...

  • Introduction Extend the inheritance hierarchy from the previous project by changing the classes to template classes....

    Introduction Extend the inheritance hierarchy from the previous project by changing the classes to template classes. Do not worry about rounding in classes that are instantiated as integer classes, you may just use the default rounding. You will add an additional data member, method, and bank account type that inherits SavingsAc-count ("CD", or certicate of deposit). Deliverables A driver program (driver.cpp) An implementation of Account class (account.h) An implementation of SavingsAccount (savingsaccount.h) An implementation of CheckingAccount (checkingaccount.h) An implementation of...

  • MAIN OBJECTIVE       Develop a polymorphic banking program using the Account hierarchy created (below). C++ Capture...

    MAIN OBJECTIVE       Develop a polymorphic banking program using the Account hierarchy created (below). C++ Capture an output. polymorphism. Write an application that creates a vector of Account pointers to two SavingsAccount and two CheckingAccountobjects. For each Account in the vector, allow the user to specify an amount of money to withdraw from the Account using member function debit and an amount of money to deposit into the Account using member function credit. As you process each Account, determine its...

  • (C++) How do I develop a polymorphic banking program using an already existing Bank-Account hierarchy? For each account...

    (C++) How do I develop a polymorphic banking program using an already existing Bank-Account hierarchy? For each account in the vector, allow the user to specify an amount of money to withdraw from the Bank-Account using member function debit and an amount of money to deposit into the Bank-Account using member function credit. As you process each Bank-Account, determine its type. If a Bank-Account is a Savings, calculate the amount of interest owed to the Bank-Account using member function calculateInterest,...

  • TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static...

    TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static constant FEE that represents the cost of clearing onecheck. Set it equal to 15 cents. Write a constructor that takes a name and an initial amount as parameters. Itshould call the constructor for the superclass. It should initializeaccountNumber to be the current value in accountNumber concatenatedwith -10 (All checking accounts at this bank are identified by the extension -10). There can be only one...

  • Bank Accounts Look at the Account class Account.java and write a main method in a different...

    Bank Accounts Look at the Account class Account.java and write a main method in a different class to briefly experiment with some instances of the Account class. • Using the Account class as a base class, write two derived classes called SavingsAccount and CheckingAccount. A SavingsAccount object, in addition to the attributes of an Account object, should have an interest variable and a method which adds interest to the account. A CheckingAccount object, in addition to the attributes of an...

  • In C++: Define a class named Account that stores a balance (monetary amount). The class should...

    In C++: Define a class named Account that stores a balance (monetary amount). The class should have a private double variable balance. Add one accessor getBalance function that returns the balance and one deposit function that increases the balance by a given double amount. Then subclass Account with a SavingsAccount class. This class should have a private double variable interest. Add an addInterest function that increases the balance (from the superclass Account) according to the interest. Demonstrate the use in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT