FidoNet Echomail Archive

<<< Previous Index Next >>>

From: Darin McBride
To: Roy J. Tellason
Date: 2004-07-14 08:37:22
Subject: Polymorphism ?

Hello Roy!

Replying to a message of Roy J. Tellason to Darin McBride:

 RJT> Darin McBride wrote in a message to Neil Heller:

 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...

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

I look at it as letting the object deal with itself.  I tell it what to do,
but let it handle *how* to do it.  For example:

class Shape {
  virtual double area()=0;
// ...

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

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

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


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

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


 * Origin: Tanktalus' Tower BBS (1:250/102)
SEEN-BY: 633/267 270
@PATH: 250/102 99 10/345 106/1 2000 633/267

<<< Previous Index Next >>>