home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16753 < prev    next >
Encoding:
Internet Message Format  |  1992-11-22  |  2.7 KB

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