home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 15886 < prev    next >
Encoding:
Text File  |  1992-11-08  |  3.8 KB  |  82 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!ukma!darwin.sura.net!haven.umd.edu!wam.umd.edu!krc
  3. From: krc@wam.umd.edu (Kevin R. Coombes)
  4. Subject: Re: Const Inheritance
  5. Message-ID: <1992Nov6.212419.23310@wam.umd.edu>
  6. Sender: usenet@wam.umd.edu (USENET News system)
  7. Nntp-Posting-Host: rac2.wam.umd.edu
  8. Organization: University of Maryland, College Park
  9. References: <2762@devnull.mpd.tandem.com> <Bx970t.B72@cdsmn.mn.org>
  10. Date: Fri, 6 Nov 1992 21:24:19 GMT
  11. Lines: 69
  12.  
  13. In article <Bx970t.B72@cdsmn.mn.org> wells@cdsmn.mn.org (Rich Wells) writes:
  14. >Duane Voth (duanev@devnull.mpd.tandem.com) wrote:
  15. >: frank@Cookie.secapl.com (Frank Adams) writes:
  16. >: >The idea comes from one of the standard examples of an inheritance
  17. >: >hierarchy: shapes.  Assume we have a class Shape, with a subclass
  18. >: >Rectangle, and we want to add Square as a class.  It seems logical that
  19. >: >Square should be a subclass of Rectangle.
  20. >
  21. >Let us not confuse "subset" with "subclass".  "Subclass" is a concept
  22. >we use for implementation; it need not correspond directly to the
  23. >"subset" and "superset" concepts we may be modeling.
  24. >
  25. >So: although the set of squares is a subset of the set of rectangles,
  26. >it does not (necessarily) mean that the class "square" should be a
  27. >subset of class "rectangle".  Inheritance can be used for both
  28. >genericity AND specialization.  In this case, perhaps Square may be
  29. >the immediate subclass of Shape, with Rectangle a more generic
  30. >subclass of Square containing an extra field for the extra degree
  31. >of freedom.
  32. >
  33. Standard dogma: public inheritance models an IS-A relationship.
  34. Inheriting Square from Rectangle asserts that "a Rectangle is a Square",
  35. so it can be used everywhere that a square is expected. This assertion
  36. is clearly false. If I really need a Square, then just any old Rectangle
  37. will not suffice. The reason, of course, was already mentioned:
  38.  
  39. >This leaves us with objects of type Square * which point
  40. >to objects which violate the basic tenets of Squaredom, which is that
  41. >all 4 sides are equal.  Therefore....
  42. >
  43. >: Perhaps the common mathematical definition of a square has led to
  44. >: this delema.  Sounds as if, in this instance, there are more differences
  45. >: between a square and a rectangle than there are similarities.
  46. >: Would making class Square and class Rectangle peers of each other
  47. >: cause so much duplication that the simplicity is unwaranted?
  48. >
  49. >No.  That is, Yes they may as well be peers.  The differences far
  50. >outweigh the similarities.
  51.  
  52. I am not convinced by this last sentence. Most of the manipulations you
  53. would need to carry out can be defined for rectangles: displaying them,
  54. moving them, intersecting them, rotating them, etc. The code to implement
  55. these operations could be written for rectangles without worrying about
  56. whether or not the manipulated object was actually a square. The only
  57. difference I see is that the lengths of the sides of the square are the
  58. same, and this assertion does not necessarily hold true for rectangles.
  59.  
  60. I've just thought of another method to resolve some of the difficulty.
  61. The essential problem seems to boil down to this
  62.  
  63. 1. If you are working with a Rectangle, and change the Width, you do
  64. not expect the Height to change as well.
  65.  
  66. 2. If you are working with a Square, you must keep the Width and the
  67. Height the same.
  68.  
  69. 3. Consequently, the call ptr_to_rect->SetWidth(3) cannot satisfy both
  70. properties 1 and 2 simultaneously (assuming a virtual function with
  71. Rectnagle a base class for Square).
  72.  
  73. Perhaps SetWidth should return a new Rectangle instead of modifying
  74. the old one? That is, if you try to change the Width of a Square, you
  75. do not get the same Square back; instead, you get a new Rectangle with
  76. the new width, but with height equal to the height of the square? You
  77. could, of course, arrange things to allow the method to modify the
  78. original object if it really is a rectangle to begin with.
  79.  
  80. Kevin Coombes <krc@math.umd.edu>
  81.  
  82.