Finish the Savings class that inherits the class Account and supports display(),…
- User
Finish the Savings class that inherits the class Account and supports display(), deposit(), and withdraw() functions. The Savings class should have an extra data member interestRate to track how much interest is generated (which we will calculate each time we make a deposit). Add an interestRate data member that can store a decimal value (e.g. 0.05 means 5% interest) Complete the constructor to take in an additional argument which is the interest rate and then calls the base class constructor and initializes the interest rate member. Write display() to provide the same display results of the base Account class, BUT also show the interest rate as the last entry. We provide a cout statement in the comments that you can use to meet our format. deposit() should first add interest and then add the value entered by the user to the balance. Each time we deposit, compute interest based on the current balance (BEFORE making the deposit) and using the interest rate data member. Add that value to the balance in addition to the deposit amount. For example, if the current balance is 1000, an interest rate of 5% is used, and a deposit of 200 is made, then the updated balance is the old balance, 1000, plus the interest (1000*0.05), 50, and the deposit amount, 200 for a total of 1250. withdraw() should behave the same as the Account implementation of withdraw().
class Account { private: string customer_name; int accountNumber; string accountType; protected: double balance;
void displayAccountDetails() { cout<<"AccountType : "<< accountType << endl; cout<<"Customer Name : "<< customer_name << endl; cout<<"Account Num : "<< accountNumber << endl; cout<<"Balance : "<< setprecision(2) << fixed << balance << endl; }public: Account(string name, int accnum, string acctype){ customer_name = name; accountNumber = accnum; balance = 0; accountType = acctype; } string getName(){ return customer_name; } int getAccountNum(){ return accountNumber; } double getBalance(){ return balance; }
void setName(string name){ customer_name = name; } void setAccountNum(int num){ accountNumber = num; } /* DO NOT ALTER */ virtual void deposit() { double deposit_amount; cout<<" Enter amount to Deposit : "; cin>> deposit_amount; cout << endl; balance = balance + deposit_amount; } /* DO NOT ALTER */ virtual void withdraw() { float withdraw_amount; cout << "Enter amount to be withdraw :"; cin >> withdraw_amount; if(balance >= withdraw_amount) { balance=balance-withdraw_amount; } else { cout<< endl << " Insufficient Balance" << endl; } } // Display specific account info virtual void display() = 0;};
// Complete - Understand this implementation of a derived class class Checking : public Account { private: string linkedDebitCardNum; public: Checking(string name, int accnum, string linkedCardnum) : Account(name, accnum, "Checking") { linkedDebitCardNum = linkedCardnum; }
/* DO NOT ALTER */ void display() { displayAccountDetails(); cout<<"Linked Card Num : "<< linkedDebitCardNum <<endl; }};
class Savings : public Account{ private:
public: // Complete the constructor and link it with the constructor of Base class Savings(string name, int accnum, double intRate) {
} // Your code for Option 1 : Deposit // Everytime you make a deposit you also earn additional // interestRate * initialBalance (i.e. initialBalance is balance // before the deposit) void deposit() { //write code below this line - think about how to use // the deposit() function defined in Account to do the majority of // the deposit work and, here, just handle the interest } // Option 2: Do you need to provide new/updated code for Withdrawal? // Your code for Option 3 : Display which should include the normal // account info but also the interest rate after that info. // Use the following line in an appropriate place. // cout<<"Interest Rate : "<< setprecision(2) << fixed << interestRate << endl; void display() { // write code below this line }};
- Assistant
Below is the completion of the Savings class that inherits from the Account class. The Savings class constructor initializes the base Account class with the specified parameters and also initializes the
interestRatemember. Thedeposit()method is overridden to calculate interest on the current balance before adding the deposit amount. Thewithdraw()method remains unchanged from the Account class, as the requirements specify that it should behave the same as in the Account implementation. Finally, thedisplay()method is overridden to include the interest rate in the output.#include <iostream> #include <string> #include <iomanip> // For setprecision using namespace std; // Existing Account declaration is assumed here class Savings : public Account { private: double interestRate; public: // Constructor initializes the Account parts and the interestRate Savings(string name, int accnum, double intRate) : Account(name, accnum, "Savings"), interestRate(intRate) {} // Overridden deposit() to include interest calculations void deposit() { double deposit_amount; cout << "Enter amount to Deposit: "; cin >> deposit_amount; cout << endl; // Calculate interest on the current balance before deposit double interest = balance * interestRate; balance += interest; // Add interest to balance balance += deposit_amount; // Add deposit amount to balance } // Display method includes interest rate void display() { displayAccountDetails(); cout << "Interest Rate : " << setprecision(2) << fixed << interestRate << "%" << endl; } }; // The rest of the provided Account and Checking classes remain unchanged.In this completion:
- The
Savingsclass constructor correctly calls the baseAccountconstructor to initialize thename,accnum, and defaultaccountTypeto "Savings", and then initializesinterestRatewith the provided interest rate. - The
deposit()method calculates interest based on the current balance before adding the deposit amount. Note that the interest is added directly to thebalancebefore the deposit amount. This reflects the description provided, where interest is computed based on the pre-deposit balance. - The
display()method shows the account details inherited from theAccountclass by callingdisplayAccountDetails()before outputting the interest rate. This is accomplished by using the inherited method for displaying common account information and then adding the specific information about the interest rate, as instructed. - The
withdraw()method does not need to be overridden in this implementation because the requirements indicate that it should behave the same as in theAccountclass, so it inherits the behavior directly without modifications.
This implementation covers the need for handling of deposits (including interest accrual) and displaying account details including the interest rate, as requested.
- The