In article <16JAN199317094204@trentu.ca>, ayounes@trentu.ca (Amro Younes, Trent University, C.C. #314, Peterborough, ON, Canada K9J 7B8. (705) 749-0391) writes:
>Hi there,
>I recently joined your group to learn more about C++. Currently I am
>preparing for a presentation of C++. Before I venture into the language I
>want to introduce the participants to what object oriented languages are
>all about. Personally OOP and C++ is a completely new concept to me.
>
>I haven't been able to find a book that has a decent definition of what
>POLYMORPHISM is. If any of you have a definition of what POLOYMORPHISM is,
>could you please forward it to me via my e-mail address
>AYOUNES@TRENTU.CA
>
>I really appreciate your time. Some of the discussions about "C/C++
>Correctness (was: Re: C/C++ Speed)" has helped me tremendously in my
>evaluation of C++.
>
>Thank you,
>
>Amro Younes
Hey Amro, I won't give you a "definition" as such, but here's a few words which hopefully will clear things up for you. In OO terms, polymorphism denotes the capability of different objects to respond to the same message. For example, we can send the message DRAW to a Circle object and it will draw itself as a Circle. We can send DRAW to a Rectangle object and it will draw itslef as a rectangle. Note I haven't said anything about what language we're talking about, polymorphism is a concept that is implemen
ted differently in different languages. Lets take C++ as an example. A message send is really a method call in C++ (or a member function invocation in C++'s (ahem) unique notation). We would send the message DRAW to a an instance of the Circle class (an object), like so:
aCircle.draw();
Ok, so far, pretty trivial. Things get interesting when we add inheritance. Lets say we have an inhertiance heirarchy as such:
class Rectangle {
private:
int length, width;
public:
virtual void draw(); // draw myself
};
class Square : public Rectangle {
private:
int width;
public:
virtual void draw(); // draw myself
}
Here we have a Square inheriting from a Rectangle. We can have a pointer to the
base class (super class) Rectangle, and we can store either a Rectangle object OR a Square object in that pointer. eg:
Rectangle* rp1;
Rectangle* rp2;
rp1 = new Rectangle;
rp2 = new Square; // note this, a Rectangle pointer containing a Square.
Now we can ask the Square to draw itself:
rp2->draw();
and the correct method (Square.draw) is invoked. We say the Square IS A Rectangle, and the Square's draw method overrides the superclass's method (Rectangle.draw). For the sake of brevity I'm glossing over a few things, but hopefully the examples provided give you a better understanding of polymorphism.