home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16685 < prev    next >
Encoding:
Text File  |  1992-11-20  |  2.7 KB  |  59 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!haven.umd.edu!wam.umd.edu!krc
  3. From: krc@wam.umd.edu (Kevin R. Coombes)
  4. Subject: Re: Squares and Rectangles (Re: Const Inheritance)
  5. Message-ID: <1992Nov21.012816.19421@wam.umd.edu>
  6. Sender: usenet@wam.umd.edu (USENET News system)
  7. Nntp-Posting-Host: rac1.wam.umd.edu
  8. Organization: University of Maryland, College Park
  9. References: <1992Nov19.171731.23740@ucc.su.OZ.AU> <1992Nov20.012340.19437@fcom.cc.utah.edu>
  10. Date: Sat, 21 Nov 1992 01:28:16 GMT
  11. Lines: 46
  12.  
  13. In article <1992Nov20.012340.19437@fcom.cc.utah.edu> val@news.ccutah.edu (Val Kartchner) writes:
  14. >In article <721507350snx@trmphrst.demon.co.uk> nikki@trmphrst.demon.co.uk writes:
  15. >>In article <1992Nov4.165028.1273@wam.umd.edu> krc@wam.umd.edu (Kevin R. Coombes) writes:
  16. >>> From a mathematical point of view, there is no question about the
  17. >>> relationship that should hold. Every rectangle IS-A polygon, with
  18. >>> four sides and four right angles. Every square IS-A rectangle with
  19. >>> four equal sides. 
  20. >
  21. >>I think our problem here is that we have two separate meanings of "is-a".
  22. >>In the geometrical sense, any particular Square "is-a" Rectangle.
  23. >
  24. >The mathematical heirarchy is inverted for OO.  Perhaps the "is-a" relationship
  25. >should be "is-a-...-with-additions".  For example:
  26. >
  27. >    shape        // Abstract base class
  28. >       ^
  29. >       |
  30. >    square        // base class
  31. >       ^
  32. >       |
  33. >    rectangle    // a square with both dimensions adjustable
  34. >       ^
  35. >       |
  36. >    parallelogram    // a rectangle with an adjustable angle
  37. > [ more levels omitted...]
  38. >There would also be other branches in the 'shape' heirarchy, but this
  39. >illustrates the principle.
  40. >
  41.  
  42. I don't understand some of the details of this hierarchy. I particularly
  43. don't understand how to reuse certain code. What methods does square have?
  44. Which are overloaded in Rectangle? Which are virtual? Which data methods
  45. do they have? For example, a Square probably has a location (specified by
  46. its center, or its upper left, or  any convention you like) and a side length.
  47. It should have a constraint (though these are unenforcable in the current
  48. language---interested parties should look at A++) that keeps it square.
  49. Some of the methods you might like to have are the ability to intersect
  50. it with other shapes, or to display it. If you intersect two squares, the
  51. best you can hope to get is a rectangle (I'll continue to assume the sides
  52. are aligned, to keep things simple). Where do you put the method? Is it a
  53. virtual method in square? A pure virtual method? What happens when you want
  54. to intersct two rectangles? Is there any way to write this hierarchy so that
  55. you can reuse both the code to intersect (or display) shapes and the code
  56. to modify dimensions?
  57.  
  58. Kevin Coombes <krc@math.umd.edu>
  59.