From: B. Williams on
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



From: R. Scott Mellow on
"B. Williams" wrote in message
>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.

[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


From: B. Williams on

"R. Scott Mellow" <filter(a)rsmellow.com> wrote in message
news:tK6dnb1tPckcRSLZnZ2dnUVZ_76dnZ2d(a)giganews.com...
> "B. Williams" wrote in message
>>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.
>
> [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
>
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?


From: R. Scott Mellow on
"B. Williams" wrote
> "R. Scott Mellow" <filter(a)rsmellow.com> wrote in message

>> 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


From: B. Williams on

"R. Scott Mellow" <filter(a)rsmellow.com> wrote in message
news:qf6dnZwohYHueiLZnZ2dnUVZ_76dnZ2d(a)giganews.com...
> "B. Williams" wrote
>> "R. Scott Mellow" <filter(a)rsmellow.com> wrote in message
>
>>> 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
>
I think I have confused myself more. This is what I did.
I added this under public data.

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?