// 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
[condensed]
> class SavingsAccount
> {
> public:
> static double modifyInterestRate();
> private:
> static double annualInterestRate;
> };
> //define and initialize static data member at file scope
> double SavingsAccount::annualInterestRate = .03;
> //define static member function
> double SavingsAccount::modifyInterestRate()
> {
> return annualInterestRate;
> }
Do you find it odd to have named the static member function
"modifyInterestRate" when all it does is return the current value of
"annualInterestRate"?
First, think about how you're going to know, inside this function, what new
value to assign to annualInterestRate.
Secondly, think about what value you want to return from this function. The
new rate? The old rate?
Do you know how to pass values to functions?
Do you know how to assign values to variables?
Do not get confused about this being a static member function or about the
rate being a static member variable. All that means is that each object you
create of this class type will have the same interest rate and you don't
need a specific object to change that rate. Other than this, it is just like
any other function and variable (apart from the way it needs to be
initialized).
--
Randy
void setInterestRateNew(double)
and set the values to .03 and .04 respectively
Is this what you meant?
>> Do you find it odd to have named the static member function
>> "modifyInterestRate" when all it does is return the current value of
>> "annualInterestRate"?
>>
>> First, think about how you're going to know, inside this function, what
>> new value to assign to annualInterestRate.
>> Secondly, think about what value you want to return from this function.
>> The new rate? The old rate?
>>
>> Do you know how to pass values to functions?
>> Do you know how to assign values to variables?
>>
>> Do not get confused about this being a static member function or about
>> the rate being a static member variable. All that means is that each
>> object you create of this class type will have the same interest rate and
>> you don't need a specific object to change that rate. Other than this, it
>> is just like any other function and variable (apart from the way it needs
>> to be initialized).
> Randy,
> I think I am following you. I am going to create a
> void setInterestRateOld(double)
>
> void setInterestRateNew(double)
>
> and set the values to .03 and .04 respectively
>
> Is this what you meant?
Not exactly but those function prototypes look like you have the right idea.
Here's an example of how I'd approach it. One thing you'll want to think
about is whether or not you want to keep track of the old interest rate.
#include <iostream>
class account
{
public:
static double rate;
static double changeRate(double);
};
double account::rate = 0.3;
double account::changeRate(double r)
{
double oldRate = rate;
rate = r;
return oldRate;
}
int main()
{
std::cout << "current rate = " << account::rate << '\n';
double oldRate = account::changeRate(0.4);
std::cout << "changed rate to " << account::rate
<< " from " << oldRate << '\n';
return 0;
}
--
Randy
void setInterestRateOld(double);
double getInterestRateOld();
void setInterestRateNew(double);
double getInterestRateNew()
I added this as private data.
double interestRateOld;
double interestRateNew;
This is what I added in the function definitions
// function to set the Old interest rate
void SavingsAccount::setInterestRateOld( double n )
{
n = .03;
interestRateOld = n; // store the old interest rate in the object
} // end function setInterestRateOld
// function to get the Old interest rate
double SavingsAccount::getInterestRateOld()
{
return interestRateOld; // return object's Old interest rate
} // end function getInterestRateOld
// function to set the New interest rate
void SavingsAccount::setInterestRateNew( double n )
{
n = .04;
interestRateNew = n; // store the new interest rate in the object
} // end function setInterestRateNew
// function to get the New interest rate
double SavingsAccount::getInterestRateNew()
{
return interestRateNew; // return object's New interest rate
} // end function getInterestRateNew
I changed this function to initialize to 0 instead of 0.3
//define and initialize static data member at file scope
double SavingsAccount::annualInterestRate = 0;
//define static member function
double SavingsAccount::modifyInterestRate()
{
return annualInterestRate;
Now I'm down to the main, but I can't figure out how to get the different
values
cout << " The balance for Saver1 is\n " << "$" <<
saver1.calculateMonthlyInterest() << endl; <-- I would like to have
interestRateOld used here, but I am doing something incorrect.
Is there a way I can get this program to work using my existing code with a
modification of the above line?
OK. I think I may have confused you. I will start again with your original
problem. Remember that you only needed to be able to set the static member
variable with your static member function. I believe that the whole concept
of an old interest rate was introduced by my initial reply. For now, forget
about saving the old rate. Go back to your original code and change your
static member function to look like this:
void modifyInterestRate(double newRate)
{
annualInterestRate = newRate;
}
Once you have your program working then you can start thinking about keeping
track of the old rate. However, based on your OP, I don't think it was part
of your assignment.
--
Randy