Could someone explain the difference between Overloading, Redefining, and Overriding functions?
A nutshell answer will work fine; I just need something to stick in my brain for tomorrow's test. I know the concepts behind these tools, but I just need a simple way of defining the difference between them.
An overloaded function is a function that shares its name with one or more other functions, but which has a different parameter list. The compiler chooses which function is desired based upon the arguments used.
An overridden function is a method in a descendant class that has a different definition than a virtual function in an ancestor class. The compiler chooses which function is desired based upon the type of the object being used to call the function.
A redefined function is a method in a descendant class that has a different definition than a non-virtual function in an ancestor class. Don't do this. Since the method is not virtual, the compiler chooses which function to call based upon the static type of the object reference rather than the actual type of the object.
For example, if you have an Animal *george , and george = new Monkey; , where Monkey inherits from Animal, if you say george->dosomething() the Animal.dosomething() method is called, even though george is a Monkey (even if a Monkey.dosomething() method is available).
The overhead isn't so great as everybody goes on and on about. However, you should only make virtual those functions which need to be virtual. Typically only public and some protected methods are virtual, while private and some protected methods are static.
Ah, ok. Yeah, my book touched on the overhead of virtual functions a bit.
Since we're on the topic of Polymorphism, do you have any good programming exercises I might be able to do? Strange question, I know. The book I'm using has really bad, somewhat non-realistic (in terms of in-the-field application) projects that put Polymorphism to use. My professor said he was going to try to find a better project, but I haven't heard anything yet. Can you think of a real-world application that might use polymorphism? If not, no problem. I just know you're obviously in the field doing this stuff and figured you might have an idea right off.
Any time you can group objects together into a single 'class' of thing you can use polymorphism. The concept behind COM objects are an example. Objects in a video game. Elements in a Raytracer (mesh, camera, curve, etc., materials, etc.). You can come up with all kinds of examples on your own.