Class Hierarchy

 

"In C we had to code our own bugs. In C++ we can inherit them."

 
 --Unknown, 1991

In an object-oriented language:

In a non-object-oriented langage:

Task: Develop a new CurrentAccount class that extends the functionality of the Account class.

Solution: Define a new class CurrentAccount that inherits the functionality of account, so: Account is the base class of CurrentAccount and equivalently CurrentAccount is the derived class of Account (i.e. CurrentAccount IS A Account).

What additional functionality should the CurrentAccout class have? Well, a current account is usually a non-interest earning account that has overdraft facilities, so we could add:

So, this can be visualised as Figure 3.2, “The Account and CurrentAccount class relationship.” where an overdraftLimit state and a setOverdraft() method have been added to the CurrentAccount class description. The class also inherits the states and methods of the Account class, i.e. balance, accountNumber, display(), makeLodgement() and makeWithdrawal().

Figure 3.2. The Account and CurrentAccount class relationship.

The Account and CurrentAccount class relationship.

So, inheritance allows a class to be inherited and extended with new functionality. A problem still remains in that the display() method that we have inherited does not work sufficiently for the CurrentAccount class. The display() method only displays the balance and accountNumber state details - it does not display the overdraftLimit. So we need to replace the behaviour (or over-ride the behaviour) of the display() method with an updated method that also displays the overdraftLimit details. So to over-ride the display method, it can simply be re-defined in the CurrentAccount class. This discussion is also relevant for the makeWithdrawal() method, as it will have to behave slightly differently when a negative balance is reached. This inheritance structure is illustrated as in Figure 3.3, “The extended Account and CurrentAccount class relationship.”.

Figure 3.3. The extended Account and CurrentAccount class relationship.

The extended Account and CurrentAccount class relationship.

So, inheritance allows a derived class to extend its parent with newly inherited functionality, and also allows functionality to be modified.

The definition of this new CurrentAccount is as follows:


  class CurrentAccount: public Account
  {
      float overdraftLimit;

    public:
      
      CurrentAccount(float bal, int actNum, float limit);
      virtual void setOverDraftLimit(float newLimit);
      virtual void display();
      virtual void makeWithdrawal(float amount);
  };

A full source code example for this is in CurrentAccount.cpp The implementation/definition of these methods is as follows.


  CurrentAccount::CurrentAccount(float bal, int actNum, float limit):
      Account(bal, actNum), overdraftLimit(limit) 
      {}

  void CurrentAccount::display()
  {
    cout << "Account number: " << accountNumber
         << " has balance: " << balance << " Euro" << endl;
    cout << "  And overdraft limit: " << overdraftLimit << endl;
  }

  void CurrentAccount::makeWithdrawal(float amount)
  {
    if (amount < (balance + overdraftLimit)) 
    {
      balance = balance - amount;
    }
  }

  void CurrentAccount::setOverDraftLimit(float limit)
  {
    overdraftLimit = limit;
  }