Before you start writing any code to use inheritance in C++, you should spend some time designing the inheritance hierarchy. Identify classes that have common behavior, and consider whether these classes would benefit from using inheritance.
In this chapter, you will define and implement an inheritance hierarchy representing different types of bank accounts. The following illustration shows how the classes will be arranged in the inheritance hierarchy.
This illustration uses Unified Modeling Language (UML) notation to represent inheritance. Each box in this diagram is a class. The arrow pointing to BankAccount denotes inheritance in UML.
BankAccount is the base class (also known as the superclass), and it defines common data members and member functions that apply for all kinds of bank accounts.
CurrentAccount and SavingsAccount are derived classes (also known as subclasses) and represent specific types of bank accounts. These derived classes inherit all the data members and member functions from BankAccount, and they can add extra data members and member functions as required for different types of bank accounts.
CurrentAccount and SavingsAccount can also override member functions defined in BankAccount. For example, the BankAccount class might have a method named CanDebit to indicate whether a certain amount of money can be debited from the account. The policy rules for allowing debits are different for each type of account; therefore, CurrentAccount and SavingsAccount can override the CanDebit method to perform the required processing for each type of account.
You will define and implement all these classes during this chapter. Let’s begin with the base class, BankAccount.