3.23 Integrated Example



This section contains full implementations of the previous examples to allow you to compile and modify as required. The source code examples are at the bottom of the page.

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 destructor 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




These notes are copyright Dr. Derek Molloy, School of Electronic Engineering, Dublin City University, Ireland 2013-present. Please contact him directly before reproducing any of the content in any way.
ċ
Account.cpp
(2k)
Derek Molloy,
19 Oct 2013, 07:17
ċ
AccountAndCurrent.cpp
(3k)
Derek Molloy,
19 Oct 2013, 07:17
ċ
AccountAndCurrentpoint.cpp
(3k)
Derek Molloy,
19 Oct 2013, 07:17
ċ
CashSave2.cpp
(4k)
Derek Molloy,
19 Oct 2013, 07:17
ċ
CashSave3.cpp
(4k)
Derek Molloy,
19 Oct 2013, 07:17
ċ
MiscExamples.cpp
(3k)
Derek Molloy,
19 Oct 2013, 07:17
ċ
MiscExamples2.cpp
(3k)
Derek Molloy,
19 Oct 2013, 07:17
ċ
cashsave.cpp
(4k)
Derek Molloy,
19 Oct 2013, 07:17
Comments