c++ - Having trouble updating an object's attributes? -


i have virtual method called update account, based on pointer found find method , returned main, account update accordingly.

there parent class called account, savings derived from.

however, savings not output updated interest , not take interest effect.

all accounts have balance , deposit not change among accounts, that's why calling account's deposit method

void savings::updateaccount(date *date) {      int   interestmonths;      short lastyear;      short lastmonth;      float currbal;       //these current date - differs account creation      //date , allows 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));              (int index = 0; index < interestmonths; index++)             {                 currbal = currbal + (currbal * interestrate);             }             //this method takes calculated current balance,            //passes parent class method update             //private accountbal attribute.              setbalance(currbal);         } } 

the issue method not updating balance object , i'm interest rate calculation not issue.

thank - method works.

you're updating a balance, on wrong account.

void savings::updateaccount(date *date)const {      int   interestmonths;      short lastyear;      short lastmonth;      float currbal;      account myaccount; 

here, myaccount local variable, unrelated account found (which this) ...

 myaccount.setbalance(currbal); 

... , it's balance of account you're updating.

you want modify object you're calling function on,

setbalance(currbal); 

and remove const function — can't have function updates account doesn't modify it.

you don't need add "savings::" inside definition of savings member —

 lastmonth = getaccountmonth();  lastyear = getaccountyear();  currbal = getaccountbal(); 

should work fine.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -