Originally posted by: idNut
Lost here. Look at my files, I uploaded them for you to see where I'm lost.
.cpp
http://www.angelfire.com/goth/id_nut/P5Code.cpp
.h
http://www.angelfire.com/goth/id_nut/P5Header.h
See, I'm totally lost on all this. I hope you get what I'm trying to do. This only has to work for two savers and I declare the variables, not a user. I want one balance to be 2000 the other to be 3000. I want it to run the program with an interest rate of .3% and display the amounts for both balances then run again but change the interest rate to .4% and show the balances.
It would be easier if you posted you code to the forum so we could comment directly. But you didn't so I won't.
In any case.
The first problem I see is in your constructor for the SavingsAccount::SavingsAccount() class
First you set dSavingsBalance to 0 This seams reasonable.
Then, 2 lines later, you set it to 2000?! So, now all SavingsAccount objects start with a balance of 2000?
What you ought to do is have the default constructor (ie. the constructor that takes no arguments) set the initial balance to 0.
Then, add another constructor that takes the initial balance as an argument. Like this (I added an initial interest rate also):
SavingsAccount::SavingsAccount(double initial_balance, double initial_rate)
{
dAnnualInterestRate = initial_rate;
dSavingsBalance = initial_balance;
}
Now, you main function would look something like this:
int main()
{
SavingsAccount Saver1(2000, 0.3), Saver2(3000, 0.3);
....
}
You should probably add member access functions as well:
void SavingsAccount::set_balance(double new_balance)
{
dSavingsBalance = new_balance;
return;
}
double SavingsAccount::get_balance(void)
{
return dSavingsBalance;
}
In general, try to avoid public data members. Make them all private, and use access functions to work with them.
The advantage of this is that you can control how these variables get updated. For example, in your program, I could do this:
Saver1.dAnnualInterestRate = -0.3;
Which is clearly nonsense. I could catch this though:
void SavingsAccount::set_rate(double new_rate)
{
if(new_rate < 0)
{
cout << "WARNING: Attempting to set interest rate to a negative value!\n";
take_some_action();
}
dAnnualInterestRate = new_rate;
return;
}
If this is the
only way that dAnnualInterestRate can be set (even the constructor uses it!) Then you are protected from dAnnualInterestRate ever being set to a negative value.
Here is another trick that may be beyond the scope of your current assignment (but maybe good for brownie points?)
If all your savings account objects will always all have the same interest rate, make dAnnualInterestRatea static member function.
This makes it shared between all instances of the class. You do this:
SavingsAccount:: DAnnualInterestRate = 0.5;
And now the interest rate for saver1
and saver 2 is now 0.5
And finally, are you sure you want the fCalcMonthlyInterest() function to be changing your interest rate?