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.

Recommended Answers

All 6 Replies

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).

Good luck.

commented: Well said. Really helped me to put concepts into words. Thanks! +4

Thanks! Great explanation!

So would you say to never use redefined methods? Is it worth the resource overhead to make all functions virtual (as opposed to redefined)?

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.

Alright, thanks! Marking this one as solved. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.