home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!charon.amdahl.com!pacbell.com!decwrl!decwrl!netcomsv!ulogic!hartman
- From: hartman@ulogic.UUCP (Richard M. Hartman)
- Newsgroups: comp.lang.c++
- Subject: Re: Squares and Rectangles (Re: Const Inheritance)
- Message-ID: <561@ulogic.UUCP>
- Date: 6 Nov 92 01:05:59 GMT
- References: <9230501.21505@mulga.cs.mu.OZ.AU> <2762@devnull.mpd.tandem.com> <1992Nov4.165028.1273@wam.umd.edu>
- Organization: negligable
- Lines: 88
-
- In article <1992Nov4.165028.1273@wam.umd.edu> krc@wam.umd.edu (Kevin R. Coombes) writes:
- >
- >From a mathematical point of view, there is no question about the
- >relationship that should hold. Every rectangle IS-A polygon, with
- >four sides and four right angles. Every square IS-A rectangle with
- >four equal sides. If we are using classes Rectangle and Square to
- >model these mathematical concepts (and, say, displaying them on screen),
- >then there certainly are common operations we want to carry out.
- >For example, we would probably like to draw them, move them, intersect
- >them, .... There are enough of these common operations that it would
- >unquestionably be worthwhile to use inheritance, if only to reuse the
- >code. The crux of the matter is: should we use public inheritance, so that
- >we both reuse the code and model the obvious IS-A relationship?
- >
- >The problem arises when we try to change the sizes of the objects.
- >Rectangles have more freedom to change size. Does this prevent us from
- >deriving Squares from Rectangles? The first attempt to take this
- >difference into account yields a design rather like:
- >
- >class Rectangle {
- > public:
- > virtual void SetHeight(int);
- > virtual void SetWidth(int);
- > // other stuff that really is common
- > protected:
- > int height, width;
- >};
- >
- >class Square : public Rectangle {
- > public:
- > void SetHeight(int s) { height = width = s; }
- > void SetWidth(int s) { height = width = s; }
- >};
- >
- >Notice that you do NOT want to give Rectangle a method like SetDimensions(),
- >because that could not be used meaningfully by the Square class. (You could,
- >of course, redeclare it to be a private member to prevent its use.)
- >
- >Does this solve the problem? Or are there problems with it that I have
- >somehow overlooked?
- >
- >Kevin Coombes <krc@math.umd.edu>
-
- In your own derivation, both rectangle and square are in turn
- derived from polygon. What generic "setsize" function would work
- for it? especially one that could be inheirited? Is there such
- a concept (not being a geometrist) as a "regular polygon", defined
- as "all sides being same size"? And where does parallelogram fit
- into this?
-
- It seems to me that a more full derivation might be:
-
- Polygon
- | |
- | |
- RegularPolygon Parallelogram
- | |
- | Rectangle
- | |
- Square
-
- A "RegularPolygon" (per my guess above) might have a
- single "setSideSize()" function that sets the size of
- ALL sides of the figure to the same value.
-
- I don't think dealing with just Rectangle and Square
- there IS any good, generic "size" type of function that
- you can rely on. Especially if you do strict IS-A
- derivation using the mathematical definiations.
-
- However, as someone pointed out, it is possible
- to say: "a Rectangle IS-A Square that is separately
- adjustable in 2 dimensions". (Perhaps this makes more
- sense if you are an engineer than if you are a
- mathematician...)
-
- Then Rectangle would inherit Square's setSize(int)
- member function, and also implement it's own
- setSize(int,int) function as well.
-
- Just stirring the pot..... ;)
-
- -Richard Hartman
- hartman@uLogic.COM
-
- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
- Disco isn't dead... ...it's just in witness protection!
-
-