(SavingsAccount Class) Create a SavingsAccount class. Use a static data member annual- InterestRate to store the annual interest rate for each of the savers. Each member of the class con- tains 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

balance by annualInterestRate divided by 12; this interest should be added to savingsBalance. Provide a static member function modifyInterestRate that sets the static an- nualInterestRate 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 in- terest and print the new balances for each of the savers. Then set the annualInterestRate to 4 per- cent, calculate the next month’s interest and print the new balances for each of the savers.

What will be an ideal response?


```
// Header file for class SavingsAccount.
#ifndef SAVINGS_ACCOUNT_H
#define SAVINGS_ACCOUNT_H

class SavingsAccount
{
public:
// constructor sets balance to value greater than or equal to zero
SavingsAccount( double b )
{
savingsBalance = ( b >= 0.0 ? b : 0.0 );
} // end SavingsAccount constructor

void calculateMonthlyInterest(); // calculate interest; add to balance
static void modifyInterestRate( double );
void printBalance() const;
private:
double savingsBalance; // the account balance
static double annualInterestRate; // the interest rate of all accounts
}; // end class SavingsAccount

#endif
```
```
// Member-function defintions for class SavingsAccount.
#include
#include
#include "SavingsAccount.h" // SavingsAccount class definition
using namespace std;

// initialize static data member
double SavingsAccount::annualInterestRate = 0.0;

// calculate monthly interest for this savings account
void SavingsAccount::calculateMonthlyInterest()
{
savingsBalance += savingsBalance * ( annualInterestRate / 12.0 );
} // end function calculateMonthlyInterest

// function for modifying static member variable annualInterestRate
void SavingsAccount::modifyInterestRate( double i )
{
annualInterestRate = ( i >= 0.0 && i <= 1.0 ) ? i : 0.03;
} // end function modifyInterestRate

// prints balance of the savings account
void SavingsAccount::printBalance() const
{
cout << fixed << '$' << setprecision( 2 ) << savingsBalance;
} // end function printBalance
```
```
// Driver program for class SavingsAccount.
#include
#include
#include "SavingsAccount.h" // SavingsAccount class definition
using namespace std;

int main()
{
SavingsAccount saver1( 2000.0 );
SavingsAccount saver2( 3000.0 );

SavingsAccount::modifyInterestRate( .03 ); // change interest rate

cout << "Initial balances:\nSaver 1: ";
saver1.printBalance();
cout << "\tSaver 2: ";
saver2.printBalance();

saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();

cout << "\n\nBalances after 1 month's interest applied at .03:\n"
<< "Saver 1: ";
saver1.printBalance();
cout << "\tSaver 2: ";
saver2.printBalance();

SavingsAccount::modifyInterestRate( .04 ); // change interest rate
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();

cout << "\n\nBalances after 1 month's interest applied at .04:\n"
<< "Saver 1: ";
saver1.printBalance();
cout << "\tSaver 2: ";
saver2.printBalance();
cout << endl;
} // end main
```
Initial balances:
Saver 1: $2000.00 Saver 2: $3000.00
Balances after 1 month's interest applied at .03:
Saver 1: $2005.00 Saver 2: $3007.50
Balances after 1 month's interest applied at .04:
Saver 1: $2011.68 Saver 2: $3017.53

Computer Science & Information Technology

You might also like to view...

Steganography can only utilize image files to hide data.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

The Comments group is on the ________

A) Design tab B) Review tab C) Home tab D) View tab

Computer Science & Information Technology

There are two methods for calculating depreciation

Indicate whether the statement is true or false.

Computer Science & Information Technology

________ is a type of malware that hides inside legitimate programs and might place code into the operating system enabling a hacker to take control of your computer

A) A Trojan horse B) A patch C) Spam D) A keylogger

Computer Science & Information Technology