lovestogame227
lovestogame227

Reputation: 31

Having trouble updating an object's attributes?

I have a virtual method called Update Account, where based on the pointer found from a find method and returned to main, the account will update accordingly.

There is the parent class called Account, where Savings is derived from.

However, the savings will not output the updated interest and will not take the interest into effect.

All accounts have a balance and the deposit will not change among accounts, that's why I am calling the Account's deposit method

void Savings::UpdateAccount(Date *date)
{
     int   interestMonths;
     short lastYear;
     short lastMonth;
     float currBal;

     //THESE GET THE CURRENT DATE - Differs from the account creation
     //date and allows for the calculation of interest

     lastMonth = GetAccountMonth();
     lastYear  = GetAccountYear();
     currBal   =  GetAccountBal();

        if (((date -> GetYear ( ) - lastYear) * 12 +
           (date -> GetMonth ( ) - lastMonth )) > 0)
        {
            interestMonths = ((date -> GetYear ( ) - lastYear) * 12 +
                             (date -> GetMonth ( ) - lastMonth));

            for (int index = 0; index < interestMonths; index++)
            {
                currBal = currBal + (currBal * interestRate);
            }

           //This method takes the calculated current balance, then
           //passes it into the parent class method to update the 
           //private accountBal attribute. 

           SetBalance(currBal);
        }
}

The issue is that this method is not updating the balance for the object and I'm fairly certain my interest rate calculation is not the issue.

Thank you for the help - this method now works.

Upvotes: 0

Views: 93

Answers (1)

molbdnilo
molbdnilo

Reputation: 66431

You're updating a balance, but on the wrong account.

void Savings::UpdateAccount(Date *date)const
{
     int   interestMonths;
     short lastYear;
     short lastMonth;
     float currBal;
     Account myAccount;

Here, myAccount is a local variable, unrelated to the account you just found (which is this) ...

 myAccount.SetBalance(currBal);

... and it's the balance of that account that you're updating.

You want to modify the object you're calling the function on, so just say

SetBalance(currBal);

and remove the const from the function — you can't have a function that updates an account but doesn't modify it.

You also don't need to add "Savings::" inside the definition of a Savings member —

 lastMonth = GetAccountMonth();
 lastYear = GetAccountYear();
 currBal = GetAccountBal();

should work just fine.

Upvotes: 2

Related Questions