Full Source Code Examples

This section contains full implementations of the previous examples to allow you to compile and modify as required.

The Account Class

Figure 3.19. The full working Account class.

The full working Account class.

The source code for this example is in Account.cpp

The Account and CurrentAccount Classes

This example extends the Account class to create a new class called CurrentAccount.

Figure 3.20. The full working Account and CurrentAccount classes.

The full working Account and CurrentAccount classes.

The source code for this example is in AccountAndCurrent.cpp

Multiple Inheritance Example (with problems)

This incomplete example shows the use of multiple inheritance in the creation of a "Cashsave" class that has parent classes of both DepositAccount and CurrentAccount classes.

Figure 3.21. The incomplete Cashsave class example.

The incomplete Cashsave class example.

The source code for this example is in Cashsave.cpp

Problems with this example are:

  • Multiple inheritance has caused the Account class to be instantiated twice, once as the parent of CurrentAccount and once as the parent of DepositAccount.

  • I made a lodgement to the cashsave account, but, I have to specify which makeLodgement() method I wish to call - CurrentAccount::makeLodgement() or DepositAccount::makeLodgement() - now I have two balances!

The Multiple Inheritance Example Fixed

This example fixes the example in the section called “Multiple Inheritance Example (with problems)” for this specific banking example.

Figure 3.22. The full working CashSave class example.

The full working CashSave class example.

The source code for this example is in CashSave2.cpp

The problem in the section called “Multiple Inheritance Example (with problems)” has been fixed by inserting the keyword virtual before Account in the definition of the CurrentAccount and DepositAccount classes.

A Final Working Version of the Cashsave example

This example adds additional functionality to the Cashsave class as defined in the section called “The Multiple Inheritance Example Fixed”.

Figure 3.23. The final working CashSave class example.

The final working CashSave class example.

The source code for this example is in CashSave3.cpp

The modifications are:

  • A more correct display() method.

  • Corrections to the access modifiers of overdraftLimit and interestRate states.

This example shows that the inherited methods from both CurrentAccount and DepositAccount are working correctly for this new child CashSaveAccount class.

Friend Methods, Pointers to Objects and Destructors

Figure 3.24. Friend Methods, Pointers to Objects and Destructors

Friend Methods, Pointers to Objects and Destructors

The source code for this example is in MiscExamples.cpp

Watch very carefully in the code as the objects go out of scope. See how the destructor is called and see the way that the descructor calls a friend method - could be an external printing method. See the way that this friend method has full access to the private states of the object. See also the way that the object that I created anonymously goes out of scope without having its destructor called. The memory is lost. It is only regained for allocation by the operating system when the program has run to completion.

Now with a Copy Constructor!

This example adds a copy constructor with specific behaviour to the example given in the section called “Friend Methods, Pointers to Objects and Destructors”.

Figure 3.25. A copy constructor example.

A copy constructor example.

The source code for this example is in MiscExamples2.cpp

Destructors and Arrays.

This example adds destructor with specific behaviour to the example given in the section called “Now with a Copy Constructor!”, demonstrating destructors when using arrays of objects and static/dynamic types.

Figure 3.26. Destructors and Arrays Example.

Destructors and Arrays Example.

The source code for this example is in AccountAndCurrentpoint.cpp