home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!uknet!glasgow!unix.brighton.ac.uk!je
- From: je@unix.brighton.ac.uk (John English)
- Newsgroups: comp.lang.c++
- Subject: Re: Wanted: Real Example of MI
- Message-ID: <1992Nov23.090210.12365@unix.brighton.ac.uk>
- Date: 23 Nov 92 09:02:10 GMT
- References: <1992Nov19.101210.1@happy.colorado.edu>
- Organization: University of Brighton, UK
- Lines: 45
-
- In article <1992Nov19.101210.1@happy.colorado.edu> you write:
- : I am revising a course on C++. I have been looking for some examples of
- : multiple inheritance for some time now. I look thru the plethora of C++ books
- : at the local book stores and so far, I have yet to encounter one that has a
- : good example of multiple inheritance.
- :
- : Can any one give me a reasonable example of MI that is
- : semantically consistent, concise and short AND REAL?
-
- (I tried to reply by mail but it got bounced. "happy.colorado.edu" doesn't
- exist according to our machine, so I'm posting this as a followup instead).
-
- Reluctant as I am to give away trade secrets :-) I use a BankAccount
- example in my C++ courses for this. The base class BankAccount has
- an account number generated from a static member, a list of Transactions
- (a private friend class), account name (a String) and a current balance.
- Member functions are: statement(), balance(), account_number(),
- account_name().
-
- The important bit is transaction(String payee, Date when, float amount)
- which returns True if the transaction is successful (always is with
- the base class -- no overdraft limit!).
-
- From this I derive CurrentAccount which imposes an overdraft limit :-(.
- All this needs is an overdraft limit data member, a setOverdraft ()
- member, and a new constructor and transaction(). The new transaction()
- just validates the transaction against the overdraft limit and then calls
- BankAccount::transaction().
-
- Similarly I derive an ChargingAccount which costs a fixed amount per
- transaction. Then I multiply derive from these to give me a current account
- which imposes transaction charges. Then I point out that BankAccount needs
- to be a virtual base class since I'm accumulating charges in a different
- account from the one I'm withdrawing from!
-
- I agree, there aren't many examples in textbooks. Try this one.
- I can squirt you the source if you want.
- --
- -------------------------------------------------------------------------------
- John English | "Yes, understanding today's complex world of
- Dept. of Computing | the future IS a little like having bees live
- University of Brighton | in your head... but, there they are..."
- Janet: je @ unix.brighton.ac.uk | "People who live in windowed environments
- Fax: 0273 642405 | shouldn't cast pointers"
- -------------------------------------------------------------------------------
-