Jump to content

polymorphism

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Nightracer

Nightracer

    Programmer

  • Members
  • PipPipPipPip
  • 131 posts
Can anyone explain to me in simple terms what polymorphism is in relation to programming?
I do understand the theory of it, but how do I incorporate this into my code?

#2
Void

Void

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 411 posts

Polymorphism in C++ is exhibited by the ability of a pointer or reference to a base class type to behave in different ways when it is used to manipulate objects of different subtypes of that base class. Said another way, in our code a base class pointer or reference is used to access an object of some subclass, possibly unknown at compile time and known only during execution time when the program is running. This base class pointer can access the correct subclass method. That is, it can access the method of the object of the subclass it is pointing to or referencing, rather than the corresponding method within the base class.


http://cplus.about.c...l/aa120602a.htm
Void

#3
brackett

brackett

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts
Basically allows you to have different implementations of a class that can be interchanged in your program. For instance, if MethodA needs an Animal, you can pass in a Dog, Cat, etc. as they all derive from the abstract Animal class (or implement an IAnimal interface, etc).

#4
Nightracer

Nightracer

    Programmer

  • Members
  • PipPipPipPip
  • 131 posts
thanks Void!
And bracket that helped as well, thanks for explaining in plain english :)

#5
Sionofdarkness

Sionofdarkness

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 384 posts
Sadly, I didn't really understand Brackett either, but he was definately more clear than Void (I didn't understand a word of that explanation).

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Here's an example: Let's say you build an Animal class, and you give it the following methods: speak(), move(), rollover(), fetch().
Each of these would be virtual functions, meaning that the implementation will be done in the subclasses. Then you could create some subclasses, such as:
Bird, Dog, Cat, Fish, each with appropriate implementations of the methods.

Now, if you have the following code:
int main()
{
  Bird mybird;
  Dog mydog;
  Cat mycat;
  Fish myfish;
  Animal* mypet=&mybird;
  mypet->move();
  mypet=&mydog;
  mypet->fetch();
  mypet=&mycat;
  mypet->fetch();
  return 0;
}
would return something like:
The bird flies around.
The dog fetchs the stick.
The cat waits for you to fetch the stick.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
Nightracer

Nightracer

    Programmer

  • Members
  • PipPipPipPip
  • 131 posts
Thanks wingedpanther!
That was an extremely helpful post, Im definitely understanding this better now