home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19980 < prev    next >
Encoding:
Text File  |  1993-01-23  |  4.8 KB  |  147 lines

  1. Xref: sparky comp.lang.c++:19980 comp.object:5078
  2. Path: sparky!uunet!charon.amdahl.com!pacbell.com!decwrl!hal.com!olivea!spool.mu.edu!howland.reston.ans.net!paladin.american.edu!darwin.sura.net!sgiblab!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Newsgroups: comp.lang.c++,comp.object
  5. Subject: Re: HELP : Array of objects of diff classes ??
  6. Message-ID: <1993Jan26.210837.28022@ucc.su.OZ.AU>
  7. Date: 26 Jan 93 21:08:37 GMT
  8. References: <1jdgecINNp06@aurora.engr.LaTech.edu> <1993Jan19.112059.2882@ucc.su.OZ.AU> <30588@castle.ed.ac.uk>
  9. Sender: news@ucc.su.OZ.AU
  10. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  11. Lines: 133
  12. Nntp-Posting-Host: extro.ucc.su.oz.au
  13.  
  14. In article <30588@castle.ed.ac.uk> eanv20@castle.ed.ac.uk (John Woods) writes:
  15. >vinay> I have a problem related to programming in an object oriented fashion.
  16. >vinay>    Here`s the problem description :
  17. >vinay>    There is an object class called Holder. The object holder can
  18. >vinay> hold various things : Pencils, Pens, Erasers etc. I`ve declared
  19. >vinay> different classes for these things ( class PENCIL, class PEN,
  20. >vinay> class ERASER etc. ).  
  21. >vinay> The user of this program will choose at the runtime what objects to put
  22. >vinay> in the holder. The problem is how do i store this information (i.e the
  23. >vinay> contents ) in class HOLDER. I cannot create a single array of objects
  24. >vinay> because each object is of different class.
  25. >
  26. >maxtal>    Forget Object Oriented Fashions, whatever they are.
  27. >
  28. >I'm not sure that's such a good idea, myself :-)
  29.  
  30.     I suggest this because people seem to think they can replace
  31. all their concepts and ideas with inheritance. It is perhaps
  32. best to use inheritance when the situation *demands* it,
  33. and not as a matter of course. 
  34.  
  35.     Good programming doesn't come from using inheritance everywhere
  36. you possibly can, and such programming isnt necessarily Object Oriented
  37. either. Nor is OO the only way to write programs.
  38. >
  39. >maxtal>    Use a discriminated union. Oh, C++ doesn't have one,
  40. >maxtal>so emulate one with an idiom.  Below I use pointers for 
  41. >maxtal>illustration, you can put copies of the actual objects in
  42. >maxtal>if you like (just get rid of appropriate *'s)
  43. >
  44. >    [ code deleted ]
  45. >
  46. >maxtal>Do not be scared of using a switch. 
  47. >
  48. >I'm not sure that's such a good idea, either.  I'd avoid them like the
  49. >plague.  
  50.  
  51.     Try avoiding inheritance like the plague. It is a very
  52. poor construction in may circumstances: it introduces strong
  53. coupling (usually BAD) and the interface is then not
  54. explicit (also considered BAD).
  55.  
  56. >My own feeling as to what would constitute an approach in the
  57. >`object oriented fashion' follows:
  58. >
  59. >All things, like `pen', `eraser', `pencil' etc.  share the property
  60. >that they can be put in the holder.  
  61.  
  62.     No they do not. They do not share any properties at all.
  63. They are completely independent objects. The only thing they
  64. share is that they are objects, and you could derive them
  65. all from class Object:
  66.  
  67.     class Object {
  68.     public:
  69.         virtual ~Object()=0;
  70.     };
  71.  
  72. which has the benefit of ensuring that any Object gets deleted
  73. properly -- nothing more.
  74.  
  75. >So, define class HolderTool.
  76. >Then define subclasses of this, such as Pencil, Pen and Eraser.  Then
  77. >one's holder is simply an array of HolderTool instances. 
  78.  
  79.     I would consider this really, really BAD.
  80.  
  81. >What language are you using, BTW?
  82. >
  83.  
  84.     What sort of holder tools do you have: dont you
  85. put pens and pencils in:
  86.  
  87.     class Desk
  88.     class PencilCase
  89.     class Tray
  90.     class DeskDrawer
  91.     class CoffeeCup
  92.     class Mouth // Yuk
  93.     class BriefCase
  94.     class Pocket
  95.  
  96. Are you really going to make ALL these things base classes
  97. of class Pen? Are you going to modify Pen, Pencil,
  98. Eraser, Pin, Stapler, Chalk ... every time you think
  99. of a new place you can put them. This is exactly opposite
  100. to the Open/Closed principle that you can close off
  101. your classes permanently.
  102.  
  103.     Inheritance is best used (IMHO) for subclassing,
  104. wherein ALL the properties of the objects have the same
  105. interface (except construction details).
  106.  
  107.     This enables polymorphism.
  108.  
  109.     class Point {
  110.     public:
  111.         virtual void Draw();
  112.     };
  113.  
  114.     class Circle : public Point {
  115.     public:
  116.         virtual void Draw();
  117.     };
  118.  
  119.     class Square : public Point {
  120.     public:
  121.         virtual void Draw();
  122.     };
  123.  
  124. *Same* interface for each class. The're all Points. Subclassing.
  125.  
  126.     class Float {
  127.     public:
  128.         float x;
  129.     };
  130.  
  131.     class Complex : public Float {
  132.     public:
  133.         float y;
  134.     };
  135.  
  136. OH DEAR. Not subclassing. A complex is not a float. Its the 
  137. other way around. What's been done here is an abuse
  138. of inheritance: we should have used composition.
  139. We do do this sort of thing sometimes to reuse an implementation,
  140. but it is NOT subclassing.
  141.  
  142. -- 
  143. ;----------------------------------------------------------------------
  144.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  145.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  146. ;------ SCIENTIFIC AND ENGINEERING SOFTWARE ---ph:  2 799 8223 --------
  147.