简体   繁体   中英

Having a base class's method use inherited class's static member variable… possible?

Base class:

class SavingsAccount
{
public:
    void AddInterest();  // add interest to balance based on APR (interest rate)
private:
    static double APR;
    double Balance;
}

class CheckingAccount: public SavingsAccount
{
private:
    static double APR;
}

I've left out irrelevant members/methods etc. for simplicity.

So, here's the situation: a CheckingAccount should act the same as a SavingsAccount , however it should have a different APR (interest rate). All SavingsAccounts share the same APR and all CheckingAccounts share their own APR (hence the variables being static). This is for an assignment, and we are expected to use static member variables for the APR s.

From my research and tests I can't seem to find any way around overriding the AddInterest() method in the CheckingAccount class to have it use CheckingAccount::APR . If this is the case then most of the methods from SavingsAccount will have to be overridden, as many use the APR , which seems to kill the point of learning to inherit a class.

Am I missing something?

The AddInterest() method, for reference:

SavingsAccount::AddInterest()
{
    double interest = (this->APR/100)/12 * this->getBalance();
    this->setBalance(this->getBalance() + interest);
}

EDIT: The original issue that I ran into (before overriding APR in CheckingAccount ) was the following:

int main()
{
    SavingsAccount sav;
    CheckingAccount chk;

    sav.setAPR(0.2);
    chk.setAPR(0.1);

    cout << sav.getAPR() << endl;  // OUTPUTS "0.1"!!

    return 0;
}

Modifying the APR of CheckingAccount objects modifies the APR of SavingsAccount objects! This makes sense to me, since APR is static, but I'm not sure what the best solution is.

I would suggest a different class hierarchy:

class Account {};
class SavingsAccount : public Account {};
class CheckingAccount : public Account {};

and then, add a virtual member function to Account :

virtual double getAPR() = 0;

and then, implement Account::AddInterest() using getAPR() .

class Account
{
   public:

      virtual ~Account() {}

      // Add other functions as needed
      // ...

      void AddInterest()
      {
         // Implement using getAPR()
         double interest = (this->APR/100)/12 * this->getBalance();
         this->setBalance(this->getBalance() + interest);
      }
      virtual double getAPR() = 0;

   private:
      double Balance;
};

class SavingsAccount : public Account
{
   public:
      virtual double getAPR() { return APR; }
   private:
      static double APR;
}

class CheckingAccount : public Account
{
   public:
      virtual double getAPR() { return APR; }
   private:
      static double APR;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM