FidoNet Echomail Archive

<<< Previous Index Next >>>

From: Roy J. Tellason
To: Darin McBride
Date: 2004-07-15 04:06:36
Subject: Polymorphism ?

Darin McBride wrote in a message to Roy J. Tellason:

 DM>> Polymorphism is practically defined by the virtual method.  That
 DM>> is, a method which may behave differently just because your
 DM>> reference is actually to an object derived from what you think it is. 
 DM>> As the previous example, having a reference to an object of type A,
 DM>> but you really refer to an object of type B or C.  Since B and C are
 DM>> both derived from A, your reference is allowed to refer to them, but
 DM>> when you call do_stuff, it does different stuff depending on the
 DM>> real underlying object.

 RJT> Ack!

 RJT> No wonder this oop-talk makes my eyes glaze over...

 DM> It all depends on how you look at it.  I still somewhat remember my
 DM> eyes glazing over talking about pointers... ;-)

I don't think I ever had that much of a problem with them,  myself.  Took
some getting used to the finer points,  but...

 DM> I look at it as letting the object deal with itself.

That would be "methods" attached to "objects"?
 DM> I tell it what to do, but let it handle *how* to do it.  For 
 DM> example:

 DM> class Shape {
 DM> public:
 DM>   virtual double area()=0;
 DM> // ...
 DM> };

 DM> class Rectangle : public Shape {
 DM> public:
 DM>   virtual double length() { return m_length; }
 DM>   virtual double height() { return m_height; }
 DM>   double area() { return length() * height(); }
 DM> };

 DM> class Square : public Rectangle {
 DM> public:
 DM>   double height() { return length(); } // lock it to be the same };

 DM> When I have a Square object reference, but I only know it's a Shape
 DM> reference, I can get its area, and let the area method figure stuff
 DM> out.  I don't need to know the details.

 DM> e.g.:

 DM> class 3DPrism {
 DM> public:
 DM>   3DPrism(Shape* s, double depth);
 DM>   double volume() { return s->area * depth; }
 DM> };

 DM> I can create a 3DPrism from a Rectangle, a Square, or any other
 DM> Shape object I want.  And the volume() calculation will be correct.
 DM> I think it's neat.  :-) 

I think I'll just stick to plain old c,  for now.

 * Origin: TANSTAAFL BBS 717-838-8539 (1:270/615)
SEEN-BY: 633/267 270
@PATH: 270/615 150/220 3613/1275 123/500 106/2000 633/267

<<< Previous Index Next >>>