Please use C++ Create an inheritance hierarchy containing base class Account and
ID: 3823338 • Letter: P
Question
Please use C++
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 balance and uses it to initialize the data member. The class should provide three member functions. Member function credit should add an amount ot 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. 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.
--SavingsAccount should inherit member functions credit and debit as is without redefining them.
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
class Account
{
public:
Account(double initBal=0);
bool credit(double amt);
bool debit(double amt);
double getBalance();
private:
double balance;
};
class Saving :public Account
{
public :
Saving(double initBal, double initRate);
void yearlyUpdate();
double calcInterest();
private:
double interestRate;
};
//========================================================
Account::Account(double initBal){
balance = initBal;
}
bool Account::credit(double amt){
if(amt<0){
cout<<"attempt to negative deposit"<<endl;
return false;
}
balance += amt;
return true;
}
bool Account::debit(double amt){
if(amt<0){
cout<<"attempt to negative withdrawl"<<endl;
return false;
}
if(amt>balance){
cout<<"debit amount exceeds balance"<<endl;
return false;
}
balance -=amt;
return true;
}
double Account::getBalance(){
return balance;
}
Saving::Saving(double initBal, double initRate):Account(initBal){
interestRate = initRate;
}
void Saving::yearlyUpdate(){
Account::credit(calcInterest());
}
double Saving::calcInterest(){
return (Account::getBalance()*1*interestRate)/100.0;
}