C++ help (Polymorphic?)

xchangx

Golden Member
Mar 23, 2000
1,692
1
71
Project

Need help on a project. Could someone explain polymorphic functions to me? This thing was due tonight at 9, but I'm turning it in late for a 30% reduction.

HELP!

Michael
 

Alphathree33

Platinum Member
Dec 1, 2000
2,419
0
0
Sure I could, but then again I have my weekly C++ assignment to do too and I have barely enough time to do that, nevermind yours too.
 

RaynorWolfcastle

Diamond Member
Feb 8, 2001
8,968
16
81
I didn't read your project but here's my explanation of polymorphic methods:
suppose you have a bunch of classes man, woman, child etc... that all inherit from person. Now suppose you give each "person" a method "Speak()". you could have an array "family" of persons John (a man), Sally (a woman), and Mike (a child). Now you do things like say family[0].Spak(). even though you don't know the method "Speak()" may mean different things for each type of person, it doesn't matter and you can use it for all of them.
polymorphism means "many forms", so the method Speak can take many forms yet to you, it remains the same

I hope that clears it up at least a bit :)
 

xchangx

Golden Member
Mar 23, 2000
1,692
1
71
yeah, that helps a little bit. Thanks!

Alphathree33: If I wanted you to do the project I wouldn't ask for help on a specific thing. I would ask if someone had a solution.
 

manly

Lifer
Jan 25, 2000
13,533
4,217
136
Umm, don't you have a decent book formally covering object oriented fundamentals? If you're interested in Java, Bruce Eckel's Thinking in Java is highly acclaimed, and a free download to boot.

Anyhow, to expand on icecool83's explanation a bit, the important characteristic of polymorphism is that the correct behavior is automagically performed at runtime.

In old procedural code, you'd have to do something like this pseudo-code:

function speak(Person thisPerson)
if (thisPerson.type is "Man")
Print "Hi I'm Mr. " thisPerson.lastName
else if (thisPerson.type is "Woman")
Print "Hi I'm Ms. " thisPerson.lastName
else if (thisPerson.type is "Baby")
Print "Ga-ga"

With polymorphism, you set up your class hierarchy as icecool83 suggested, override the speak() behavior in each derived class and the code is much simpler:

function greeting(Person thisPerson)
thisPerson.speak()

What happens is the runtime automatically determines the actual derived type of Person and then invokes the proper behavior for that type, whether it is Man, Woman or Baby. The important points of polymorphism are that you're working with a Person object (for which the actual type is not even visible) but the runtime dynamically calls the correct behavior on the actual subtype. Hence, the Person type can take on "many forms".

This is one of the essential building blocks of object oriented programming, and it's extremely powerful. More so than it initially appears. Polymorphism is one of the keys to writing extensible software.

Finally, try to think in terms of classes and their objects (instances). Methods themselves aren't really polymorphic, but method calls are. The reason your assignment differentiates by using the term "polymorphic function" is only because in C++, method calls are by default not polymorphic. They are polymorphic only if the methods are declared as virtual. As suggested above, the mechanism behind polymorphism is dynamic binding (aka dynamic dispatch or late binding).
 

kherman

Golden Member
Jul 21, 2002
1,511
0
0
Originally posted by: icecool83
I didn't read your project but here's my explanation of polymorphic methods:
suppose you have a bunch of classes man, woman, child etc... that all inherit from person. Now suppose you give each "person" a method "Speak()". you could have an array "family" of persons John (a man), Sally (a woman), and Mike (a child). Now you do things like say family[0].Spak(). even though you don't know the method "Speak()" may mean different things for each type of person, it doesn't matter and you can use it for all of them.
polymorphism means "many forms", so the method Speak can take many forms yet to you, it remains the same

I hope that clears it up at least a bit :)

I didn't read it either, but here's the best way of understanding it.

You have a base class called shape with an abstract (or virtual or undefined method, however you want to call it) method called draw.
Now you create classes called Square, Triangle and Circle which each define the draw method.

Now you can have an array of shapes, all being different. You can parse the array, telling each element to draw itself, not caring what it is.

example pseudocode:
Shape shapes [3];
shapes[0] = new Square();
shapes[1] = new Triangle();
shapes[2] = new Circle();

for (int i = 0; i <=2; i++)
shape.draw();


This would have output of:
---------
|-------|
|-------|
---------
square

.../\
../--\
./----\
-------
triangle

...---
./----\
.\----/
...---
circle

Not the pretiest drawings, but point is, you didn't care what the shape was, you just cared that it was a shape. A square is a shape. But a shape is not a always square.