|
||||||
| General Programming Non language specific, Assembly, Linux/Unix, Mac and anything not covered in other topics. Talk about Programming Theory here. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||||
|
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 |
|
|||
|
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).
|
|
|||||
|
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;
}
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. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
| 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 |
Goal: 100,000 Posts
Complete: 98%