This section contains full implementations of the previous examples to allow you to compile and modify as required.
The source code for this example is in
Account.cpp
This example extends the Account
class to create a new class called
CurrentAccount
.
The source code for this example is in
AccountAndCurrent.cpp
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.
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!
This example fixes the example in the section called “Multiple Inheritance Example (with problems)” for this specific banking 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.
This example adds additional functionality to the Cashsave
class as defined in
the section called “The Multiple Inheritance Example Fixed”.
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.
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.
This example adds a copy constructor with specific behaviour to the example given in the section called “Friend Methods, Pointers to Objects and Destructors”.
The source code for this example is in
MiscExamples2.cpp
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.
The source code for this example is in
AccountAndCurrentpoint.cpp
© 2006 Dr. Derek Molloy (DCU).