Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!samsung!uunet!meaddata!gordon From: gordon@meaddata.com (Gordon Edwards) Newsgroups: comp.object Subject: Re: ??Bidirectional Inheritence?? Message-ID: <1737@meaddata.meaddata.com> Date: 23 Oct 90 12:54:27 GMT References: <6957@uwm.edu> <304@mtiame.oz> Sender: usenet@meaddata.com Reply-To: gordon@meaddata.com (Gordon Edwards) Distribution: comp Organization: Mead Data Central, Dayton OH Lines: 48 In article <304@mtiame.oz>, jep@mtiame.oz (Jesper Peterson) writes: |> In article <6957@uwm.edu> ctumey@csd4.csd.uwm.edu (Carol T Tumey) writes: |> >[....] |> >In the small amount of reading I have done on OOP/OOD I have |> >not seen anything on the subject of parents inheriting |> >values from their children nor can I see the benfits of |> >such hierarchical incest :-) |> |> Do virtual functions in C++ qualify? (I'm asking) |> An example given in Bjarne Stroustrup's book is: [page 37] |> A "Shape" class which does not have the attributes required to |> sensibly respond ro a "draw" message. It does, however, |> declare the "draw" function as "virtual". |> Derive some classes from Shape, such as Circle, Square etc. Each |> of these properly define the "draw" function. The word virtual tells the compiler that the function draw() should be bound at run-time. The default (no virtual keyword) binds the draw() function at compile time. |> You can now store a mixture of Circles and Squares in an array |> of Shapes. What you are describing here is a polymorphic array. The array of Shape& can store Shapes and Circles and Squares (which are also Shapes). |> You can loop through this array and sensibly send "draw" |> messages to instances of Shape rather than the specialised |> classes. If I have an instance X of Circle, then I also have an instance of Shape. When X calls draw(), the class definition of Circle is checked and if draw() exists X is bound to it. If not, the class definition of Shape is checked and if draw() exists, X is bound to it. This continues up the class hierarchy. |> |> As I understand it, Shape effectively "inherits" "draw" from it's children. |> Not really, Shape's draw is never examined. This is hard to explain without being able to talk face-to-face and draw pictures. Try to remember that the order of evaluation starts at the sub-class (or leaf) and works up through the class hierarchy. -- Gordon (gordon@meaddata.com)