C++ code to do following Create a SavingsAccount class. Use a static data member
ID: 3909433 • Letter: C
Question
C++ code to do following
Create a SavingsAccount class. Use a static data member annualInterestRate to store the annual interest rate for each of the savers. Each member of the class contains a private data member savingsBalance indicating the amount the saver currently has on deposit. Provide member function calculateMonthlyInterest that calculates the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12; this interest should be added to savingsBalance. Provide a static member function modifyInterestRate that sets 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 new balances for each of the savers. Then set the annualInterestRate to 4 percent, calculate the next month’s interest and print the new balances for each of the savers.
Explanation / Answer
// C++ Program
#include <iostream>
using namespace std;
class SavingsAccount
{
public:
SavingsAccount(){}
SavingsAccount(int value);
~SavingsAccount(){}
static float anumIR;
void monthInt();
static void modifyIR(float value);
float gtebal() const
{
return SB;
}
private:
float SB;
};
// copy constructor
SavingsAccount::SavingsAccount(int value)
{
SB = value;
}
float SavingsAccount::anumIR = 0;
void SavingsAccount::monthInt()
{
SB += ((SB * anumIR) / 12);
}
void SavingsAccount::modifyIR(float value)
{
anumIR = value;
}
int main()
{
SavingsAccount saver1(2000.00);
SavingsAccount saver2(3000.00);
// set the annual interest rate 3%
SavingsAccount::modifyIR(3);
saver1.monthInt();
cout<< "Saver 1 Savings Balance: $" <<saver1.getbal() << endl;
saver2.monthInt();
cout<< "Saver 2 Savings Balance: $" <<saver2.getbal() << endl;
cout<<endl;
// set the annual interest rate 3%
SavingsAccount::modifyIR(4);
saver1.monthInt();
cout<< "Saver 1 Savings Balance: $" <<saver1.getbal() << endl;
saver2.monthInt();
cout<< "Saver 2 Savings Balance: $" <<saver2.getbal() << endl;
cout<<endl;
return 0;
}
/* output
Saver 1 Savings Balance: $2500
Saver 2 Savings Balance: $3750
Saver 1 Savings Balance: $3333.33
Saver 2 Savings Balance: $5000
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.