Hello(Son *s). you're simply overloading the function Hello with different class types as arguments. you could even do :
Parent* a = new Parent();Parent* b = new Parent();a->Hello((Son*)b); // calls Hello(Son* p);// orParent* a = new Parent();Son* b = new Son();b->Hello((Daughter*)a); // probably should use static_cast
heh, its been awhile since ive thought about this stuff... but i don't understand your first example. how is it possible for a parent to turn into a child pointer? i thought only a child could be used as a parent, and not vice-versa. i mean, what if Son has members which Parent doesn't? if you cast the parent to a son, i just dont see how this is possible because then the function could mess with Son:: members when the pointer really belongs to a parent, who doesnt have those members. i just dont see how that works out... i mean, my only guess is that those members are only temporary and when the function returns it loses any child members. heh, didnt even know about that.. i guess its about time i learned about the casting of pointers in situations like this, i've been putting it off for awhile because i haven't needed it [smile]
thanks for any help.
[Edited by - graveyard filla on October 25, 2004 12:23:07 AM]
I fail to see where your confusion lies. You're deriving a child class from a parent class, then calling a function on the child class. That's pretty basic. There's no polymorphism here.
The hello function will always resolve to the highest level of the parameters.
so...
Son son; Parent dad; dad.Hello(&son);
Will just call the hello function on the Parent object which took Son* as a parameter.
ie. Parent::Hello( Son* p );
Nothing you have here is declared virtual, so it is impossible for you to get any polymorphism out of this.
Polymorphism is used to call the function of a derived class when you have a pointer to the base class...
Example:
class Parent { public: virtual void HellO(); };
class Child : public Parent { public: virtual void Hello(); } ----- Parent* p = new Child;
In this case you've got what's called "A base pointer to a derived class". Since Hello is virtual, calling
p->Hello();
Will actually call Child::Hello(). That's polymorphism.
i guess my main confusion was i didn't understand which function would be called when the parameters were ambiguous. for example, lets say that the Hello(Son *p); function wasn't a member of the Parent class. if i did:
Parent Dad; Son son;
Dad.Hello(&son);
this would call the Hello(Parent*) version of the function, no? i guess i just didn't understand which function it would call when both functions where defined. isnt this considered polymorphism when the son pointer is treated as a Parent pointer? i guess thats what i thought, anyway [smile]. i already understand the purpose of and how to use virtual functions though. thanks again.
Now I understand your question. When a class defines multiple functions, each with the same name, but taking different parameters, its called function overloading. This you probably already knew.
If the parameters happen to be derived from each other, the compiler doesnt care. It will first try and resolve it against the class which is being passed in. If it cant, it'll try and find a compatible type, such as a parent.
So if Child derives from Parent as in our previous examples..and a function is defined which takes both a Parent and a Child, then passing in a Child will call the one which expects a child. However, if there is no function which takes a child, it will call the one that expects a parent.
And no, treating a child pointer as a base pointer isn't polymorphism. Its just a benefit of Inheritance. Polymorphism as I said before is when you have a base pointer to a derived class, and through the use of virtual functions, are still able to call child functions. Cheers and Good Luck!