Lost Password?


Go Back   CodeCall Programming Forum > Software Development > General Programming

General Programming Non language specific, Assembly, Linux/Unix, Mac and anything not covered in other topics. Talk about Programming Theory here.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-21-2006, 12:00 AM
Nightracer's Avatar   
Nightracer Nightracer is offline
Programmer
 
Join Date: Jun 2006
Posts: 131
Rep Power: 9
Nightracer is on a distinguished road
Default polymorphism

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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 06-25-2006, 06:21 PM
Void's Avatar   
Void Void is offline
Programming Expert
 
Join Date: Jun 2006
Posts: 411
Rep Power: 11
Void is on a distinguished road
Default


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.com/od/beginnerct.../aa120602a.htm
__________________
Void
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-26-2006, 07:13 AM
brackett brackett is offline
Programmer
 
Join Date: May 2006
Posts: 193
Rep Power: 11
brackett is on a distinguished road
Default

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).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-24-2006, 10:57 PM
Nightracer's Avatar   
Nightracer Nightracer is offline
Programmer
 
Join Date: Jun 2006
Posts: 131
Rep Power: 9
Nightracer is on a distinguished road
Default

thanks Void!
And bracket that helped as well, thanks for explaining in plain english
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-25-2006, 01:13 PM
Sionofdarkness Sionofdarkness is offline
Programming Expert
 
Join Date: Jul 2006
Posts: 384
Rep Power: 11
Sionofdarkness is on a distinguished road
Default

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).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 07-25-2006, 06:31 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,278
Last Blog:
wxWidgets is NOT code ...
Rep Power: 36
WingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to all
Default

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:
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.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-26-2006, 10:53 PM
Nightracer's Avatar   
Nightracer Nightracer is offline
Programmer
 
Join Date: Jun 2006
Posts: 131
Rep Power: 9
Nightracer is on a distinguished road
Default

Thanks wingedpanther!
That was an extremely helpful post, Im definitely understanding this better now
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -5. The time now is 06:00 PM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 98%

Ads