Problem with function

B

B. Williams

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
 
V

Victor Bazarov

B. Williams said:
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();

I think the point of the exercise is to create a function similar to
'setSavingsBalance' that will *set* the 'annualInterestRate' member
to the value passed as the argument (similar to 'setSavingsBalance'),
don't you think?
private:

double savingsBalance;

//static data member

static double annualInterestRate;

}; // end class SavingsAccount

#endif

[..]

V
 
B

B. Williams

Victor Bazarov said:
B. Williams said:
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();

I think the point of the exercise is to create a function similar to
'setSavingsBalance' that will *set* the 'annualInterestRate' member
to the value passed as the argument (similar to 'setSavingsBalance'),
don't you think?
private:

double savingsBalance;

//static data member

static double annualInterestRate;

}; // end class SavingsAccount

#endif

[..]

V
Victor,
That is how I interpreted it. I can write the function to set the
annualInterestRate as I did, but I would like the function to have the
ability to change the intertest rate.
 
V

Victor Bazarov

B. Williams said:
Victor Bazarov said:
B. Williams said:
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.
[...]

I think the point of the exercise is to create a function similar to
'setSavingsBalance' that will *set* the 'annualInterestRate' member
to the value passed as the argument (similar to 'setSavingsBalance'),
don't you think?
[...]
Victor,
That is how I interpreted it. I can write the function to set the
annualInterestRate as I did, but I would like the function to have the
ability to change the intertest rate.

I am sorry. You can write the function to set *as you did*? You *did*
that already? I don't see the difference between "to set" or "to change".

You initialised your static data member to 0.03, but there is no function
that *sets* or *changes* that data member. If you need clarification,
I suggest you speak to your teacher/instructor/coach.

V
 
B

B. Williams

Victor Bazarov said:
B. Williams said:
Victor Bazarov said:
B. Williams wrote:
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.
[...]

I think the point of the exercise is to create a function similar to
'setSavingsBalance' that will *set* the 'annualInterestRate' member
to the value passed as the argument (similar to 'setSavingsBalance'),
don't you think?
[...]
Victor,
That is how I interpreted it. I can write the function to set the
annualInterestRate as I did, but I would like the function to have the
ability to change the intertest rate.

I am sorry. You can write the function to set *as you did*? You *did*
that already? I don't see the difference between "to set" or "to change".

You initialised your static data member to 0.03, but there is no function
that *sets* or *changes* that data member. If you need clarification,
I suggest you speak to your teacher/instructor/coach.

V
I understand what you are talking about, I think.

I have created some additional functional to set and get the interest rates.

// 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 wish it was as easy as speaking to an instructor. This is the 4th week of
class (online) and we haven't even received feedback from week two
assignments. I utilize you guys as my adjuct instructor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top