home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 15841 < prev    next >
Encoding:
Internet Message Format  |  1992-11-06  |  3.6 KB

  1. Path: sparky!uunet!charon.amdahl.com!pacbell.com!decwrl!decwrl!netcomsv!ulogic!hartman
  2. From: hartman@ulogic.UUCP (Richard M. Hartman)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Squares and Rectangles (Re: Const Inheritance)
  5. Message-ID: <561@ulogic.UUCP>
  6. Date: 6 Nov 92 01:05:59 GMT
  7. References: <9230501.21505@mulga.cs.mu.OZ.AU> <2762@devnull.mpd.tandem.com> <1992Nov4.165028.1273@wam.umd.edu>
  8. Organization: negligable
  9. Lines: 88
  10.  
  11. In article <1992Nov4.165028.1273@wam.umd.edu> krc@wam.umd.edu (Kevin R. Coombes) writes:
  12. >
  13. >From a mathematical point of view, there is no question about the
  14. >relationship that should hold. Every rectangle IS-A polygon, with
  15. >four sides and four right angles. Every square IS-A rectangle with
  16. >four equal sides. If we are using classes Rectangle and Square to
  17. >model these mathematical concepts (and, say, displaying them on screen),
  18. >then there certainly are common operations we want to carry out.
  19. >For example, we would probably like to draw them, move them, intersect
  20. >them, .... There are enough of these common operations that it would
  21. >unquestionably be worthwhile to use inheritance, if only to reuse the
  22. >code. The crux of the matter is: should we use public inheritance, so that
  23. >we both reuse the code and model the obvious IS-A relationship?
  24. >
  25. >The problem arises when we try to change the sizes of the objects.
  26. >Rectangles have more freedom to change size. Does this prevent us from
  27. >deriving Squares from Rectangles? The first attempt to take this
  28. >difference into account yields a design rather like:
  29. >
  30. >class Rectangle {
  31. >  public:
  32. >    virtual void SetHeight(int);
  33. >    virtual void SetWidth(int);
  34. >    // other stuff that really is common
  35. >  protected:
  36. >    int height, width;
  37. >};
  38. >
  39. >class Square : public Rectangle {
  40. >  public:
  41. >    void SetHeight(int s) { height = width = s; }
  42. >    void SetWidth(int s) { height = width = s; }
  43. >};
  44. >
  45. >Notice that you do NOT want to give Rectangle a method like SetDimensions(),
  46. >because that could not be used meaningfully by the Square class. (You could,
  47. >of course, redeclare it to be a private member to prevent its use.)
  48. >
  49. >Does this solve the problem? Or are there problems with it that I have
  50. >somehow overlooked? 
  51. >
  52. >Kevin Coombes <krc@math.umd.edu>
  53.  
  54. In your own derivation, both rectangle and square are in turn
  55. derived from polygon.  What generic "setsize" function would work
  56. for it?  especially one that could be inheirited?  Is there such
  57. a concept (not being a geometrist) as a "regular polygon", defined
  58. as "all sides being same size"?  And where does parallelogram fit
  59. into this?
  60.  
  61. It seems to me that a more full derivation might be:
  62.  
  63.             Polygon
  64.             |    |
  65.             |    |
  66.         RegularPolygon   Parallelogram
  67.             |    |
  68.             |    Rectangle
  69.             |    |
  70.             Square
  71.  
  72. A "RegularPolygon" (per my guess above) might have a
  73. single "setSideSize()" function that sets the size of
  74. ALL sides of the figure to the same value.
  75.  
  76. I don't think dealing with just Rectangle and Square
  77. there IS any good, generic "size" type of function that
  78. you can rely on.  Especially if you do strict IS-A 
  79. derivation using the mathematical definiations.
  80.  
  81. However, as someone pointed out, it is possible
  82. to say: "a Rectangle IS-A Square that is separately
  83. adjustable in 2 dimensions".  (Perhaps this makes more
  84. sense if you are an engineer than if you are a 
  85. mathematician...)
  86.  
  87. Then Rectangle would inherit Square's setSize(int)
  88. member function, and also implement it's own
  89. setSize(int,int) function as well.
  90.  
  91. Just stirring the pot.....            ;)
  92.  
  93.         -Richard Hartman
  94.         hartman@uLogic.COM
  95.  
  96. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  97. Disco isn't dead...            ...it's just in witness protection!
  98.  
  99.