Use a static data member annualInterestRate to store theannual interest rate for
ID: 3542239 • Letter: U
Question
Use a static data member annualInterestRate to store theannual interest rate for each of the savers. Each member of the class contains a private data membersavingsBalance indicating the amount the saver currently has ondeposit. Provide member function calculateMonthlyInterest thatcalculates the monthly interest by multiplying the balance byannualInterestRate divided by 12; this interest should be added to savingsBalance. Provide a static member function modifyInterestRate thatsets the static annualInterestRate to a new value. Write a driver program to test class SavingsAccount. Instantiate two different objects of class SavingsAccount,saver1 and saver2, with balances of $2000.00 and $3000.00,respectively. Set the annualInterestRate to 3 percent. Then calculate the monthly interest and print the newbalances for each of the savers. Then set the annualInterestRate to 4 percent, calculatethe next monthExplanation / Answer
#include<iostream>
using namespace std;
class SavingsAccount
{
private:
double savingsBalance;
static double annualInterestRate;
public:
SavingsAccount(double b)
{
savingsBalance = b;
}
void calculateMonthlyInterest()
{
savingsBalance = savingsBalance + savingsBalance*annualInterestRate/12;
}
static void modifyInterestRate(double air)
{
annualInterestRate = air;
}
double get_balance()
{
return savingsBalance;
}
};
double SavingsAccount::annualInterestRate= 0.03;
int main()
{
SavingsAccount saver1(2000);
SavingsAccount saver2(3000);
SavingsAccount::modifyInterestRate(0.03);
saver1.calculateMonthlyInterest();
cout << "saver1 balance given by " << saver1.get_balance() << endl;
saver2.calculateMonthlyInterest();
cout << "saver2 balance given by " << saver2.get_balance() << endl;
SavingsAccount::modifyInterestRate(0.04);
saver1.calculateMonthlyInterest();
cout << "saver1 balance given by " << saver1.get_balance() << endl;
saver2.calculateMonthlyInterest();
cout << "saver2 balance given by " << saver2.get_balance() << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.