Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

polymorphism

polymorphism

  • Please log in to reply
6 replies to this topic

#1 Nightracer

Nightracer

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 126 posts

Posted 20 June 2006 - 08:00 PM

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?
  • 0

#2 Void

Void

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 403 posts

Posted 25 June 2006 - 02:21 PM


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
  • 0
Void

#3 brackett

brackett

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 192 posts

Posted 26 June 2006 - 03:13 AM

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).
  • 0

#4 Nightracer

Nightracer

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 126 posts

Posted 24 July 2006 - 06:57 PM

thanks Void!
And bracket that helped as well, thanks for explaining in plain english :)
  • 0

#5 Sionofdarkness

Sionofdarkness

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 370 posts

Posted 25 July 2006 - 09:13 AM

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).
  • 0

#6 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 25 July 2006 - 02:31 PM

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

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#7 Nightracer

Nightracer

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 126 posts

Posted 26 July 2006 - 06:53 PM

Thanks wingedpanther!
That was an extremely helpful post, Im definitely understanding this better now
  • 0





Also tagged with one or more of these keywords: polymorphism

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download