I have written this program for an assignment that requires a static member
function to set a static data member, but I can't figure out how to get it
to change the value once set. Would someone point me in the right direction.
I'll post all of my code, but what I need help with is the static member
function. Thanks in advance.
// prevent multiple inclusions of header file
#ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H
// SavingsAccount class definition
class SavingsAccount
{
public:
SavingsAccount( double ); //constructor
void setSavingsBalance(double);
double getSavingsBalance();
double calculateMonthlyInterest();
//static member function
static double modifyInterestRate();
private:
double savingsBalance;
//static data member
static double annualInterestRate;
}; // end class SavingsAccount
#endif
// SavingsAccount member-function definitions. This file contains
// implementations of the member functions prototyped in 10-8.h.
#include <iostream>
using std::cout;
using std::endl;
#include "10-8.h" // include definition of class SavingsAccount
//define and initialize static data member at file scope
double SavingsAccount::annualInterestRate = .03;
//define static member function
double SavingsAccount::modifyInterestRate()
{
return annualInterestRate;
} //end static function
// initializing constructor
SavingsAccount::SavingsAccount( double n )
{
setSavingsBalance( n ); // call set function to initialize SavingsBalance
} // end SavingsAccount constructors
// function to set the SavingsBalance
void SavingsAccount::setSavingsBalance( double n )
{
savingsBalance = n; // store the SavingsBalance in the object
} // end function setSavingsBalance
// function to get the SavingsBalance
double SavingsAccount::getSavingsBalance()
{
return savingsBalance; // return object's Savings Balance
} // end function getSavingsBalance
// member function that calculates the monthly doubleerest
double SavingsAccount::calculateMonthlyInterest()
{
double balance = 0;
double n1 = savingsBalance;
double n2 = annualInterestRate;
balance = ((n1 * n2) / 12) + n1;
return balance;
} // end function calculateMonthlyInterest
// solution.cpp
// Including class SavingsAccount from 10-8.h for use in main.
#include <iostream>
using std::cout;
using std::endl;
#include "10-8.h" // include definition of class Rational
// function main begins program execution
int main()
{
// create SavingsAccount object
SavingsAccount saver1 =(2000.00);
SavingsAccount saver2(3000.00);
cout << " The balance for Saver1 is\n " << "$" <<
saver1.calculateMonthlyInterest() << endl;
cout << " The balance for Saver2 is\n " << "$" <<
saver2.calculateMonthlyInterest() << endl;
return 0; // indicate successful termination
} // end main