Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 1: Implement the member functions. In main, write a “driver program” that f

ID: 3620082 • Letter: P

Question

Part 1:
Implement the member functions. In main, write a “driver program” that fully tests the functionality of the class. This means create an object and call all the members to make sure they work. Test the static function by calling it with the class name, not an object name. Also test the static data member by accessing it with the class name. Don’t forget to test the friend function.

class SavingsAccount
{
public:
SavingsAccount(double b) { savingsBalance = b >= 0 ? b : 0; } // no negs
void calculateMonthlyInterest();
static void modifyInterestRate( double );
void printBalance() const;
private:
double savingsBalance;
static double annualInterestRate;
friend double getInterestRate(SavingsAccount);// use a friend for practice
};
double SavingsAccount::annualInterestRate = 0.0;


Part 2:
A) Write a main function that creates three SavingsAccount objects. Create one in the global code space, one on the stack (a local in main), and one in dynamic memory (the heap) using new.
B) Draw 3 diagrams with a pencil that show where all the variables are in memory for the following cases:
a. just before the friend function terminates
b. just before the friend function terminates, with the functions parameter type changed to SavingsAccount &.
c. just before main terminates
(Don’t forget the static class data member and all the locals that you explicitly know about.)


What i have so far is:
#include<iostream>
using namespace std;
class SavingsAccount
{
public:


SavingsAccount(double b) {savingsBalance = b >= 0 ? b :0; };//default constructor
//to 0 if no argument


void calculateMonthlyInterest();
static void modifyInterestRate(double );
void printBalance() const;//returns pointer to current balance
~SavingsAccount();//destructor

private:
double *savingsBalance;
static double annualInterestRate;
friend double GetInterestRate(SavingsAccount);
};
//SavingsAccount class defintion

double SavingsAccount::annualInterestRate=0.0;//define and intialize static data
//member at file scope


SavingsAccount::SavingsAccount(double amount)
:savingsBalance(new double(amount))//intialize savingsBalance to point to new object
{//empty body
}//end of constructor

double SavingsAccount::printBalance()const
{
return void;

}

double SavingsAccount::calculateMonthlyInterest()
{
double monthlyInterest=((*savingsBalance)*annualInterestRate)/12;

*savingsBalance=*savingsBalance+monthlyInterest;

return monthlyInterest;
}

void SavingsAccount::modifyInterestRate(double interestRate)
{
annualInterestRate=interestRate;
}

SavingsAccount::~SavingsAccount()
{
delete savingsBalance;
}//end of destructor

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

SavingsAccount::modifyInterestRate(0.05);//set interest rate to 3%

cout<<"Saver1 monthly interest: "<<saver1.calculateMonthlyInterest()<<endl;
cout<<"Saver2 monthly interest: "<<saver2.calculateMonthlyInterest()<<endl;

cout<<"Saver1 balance: "<<saver1.printBalance()<<endl;
cout<<"Saver2 balance: "<<saver2.printBalance()<<endl;

return 0;
}

Explanation / Answer

#include<iostream>

using namespace std;

class SavingsAccount

{

public:

SavingsAccount(double value);

static double annualInterestRate;

void calculateMonthlyInterest();

static void modifyIntererestRate(double value);

double GetBalance() const { return savingsBalance; }

friend double getInterestRate(SavingsAccount);

                        // use a friend for practice

private:

// Each member of the class contains a private data member

// savingsBalance indicating the amount the saver currently has

// on deposit.

double savingsBalance;

};

// copy constructor to initialize the value at instantiation

SavingsAccount::SavingsAccount(double b)

{

savingsBalance = b>= 0 ? b : 0;;

}

// Use a static data member annualInterestRate to store the annual interest

// rate for each of the savers.

double SavingsAccount::annualInterestRate = 0;

// Provide member function calculateMonthlyInterest that calculates the

void SavingsAccount::calculateMonthlyInterest()

{

savingsBalance +=

           ((savingsBalance * annualInterestRate) / 12);

}

//Provide a static member function modifyIntererestRate //that sets the static annualInterestRate to a new value.

void SavingsAccount::modifyIntererestRate(double value)

{

annualInterestRate = value;

}

double getInterestRate(SavingsAccount acc)

{

     return acc.annualInterestRate;

}

int main()

{

// Instantiate two different objects of class SavingsAccount

SavingsAccount account1(2000.00);

SavingsAccount account2(3000.00);

// Set the annualInterestRate to 3%.

SavingsAccount::modifyIntererestRate(3);

// Then calculate the monthly interest and print the new balances for

// each of the savers.

account1.calculateMonthlyInterest();

cout << "Account 1 Savings Balance: $" << account1.GetBalance() << endl;

account2.calculateMonthlyInterest();

cout << "Account 2 Savings Balance: $" << account2.GetBalance() << endl;

cout << endl;

// Then set the annualInterestRate to 4%

SavingsAccount::modifyIntererestRate(8);

// and calculate the next month's interest and print the new balances

// for each of the savers

account1.calculateMonthlyInterest();

cout << "Account 1 Savings Balance: $" << account1.GetBalance() << endl;

cout<<"Account 1 Interset Rate:"<<getInterestRate(account1)<<"%"<<endl;

account2.calculateMonthlyInterest();

cout << "Account2 Savings Balance: $" << account2.GetBalance() << endl;

cout << endl;

system("pause");

return 0;

}