Monday, May 24, 2010

What is function overriding in C++???

Function overriding allows you to write multiple functions with the same name. The compiler differentiates them from each other by the number of and types of the parameters the function takes. The compiler does this by "name mangling". So, for instance, the compiler could internally change





void foo(void); to vFooV(void)


and


void foo(float f); to vFooF(float)





Compilers are free to mangle names any way they want so long as they are consistent. However it is entirely possible that two different compilers will employ entirely different techniques.





The compiler keeps track of which one of the overridden functions is called based on the parameters present in the function call itself.





Note that overriding does not necessarily have anything to do with inheritance. Stand alone functions outside of any class can be overridden.

What is function overriding in C++???
Function Overriding refers to modify the definition of the base class method into the derived class with the same name and signature.





If we change the the definition of a method in a same class but with different signature then it is known as Function Overloading.








Check this out, Function overriding in C++/CLI. http://www.codeproject.com/KB/mcpp/cppcl...
Reply:function overloading is where you can make a function do different things depending on what information you send as parameters to the function.





Actually it isn't the function doing different things, it's multiple functions with the same name that do different things. The function that actually gets invoked depends on what parameters you send.








http://en.wikibooks.org/wiki/Computer_pr...
Reply:Function Overriding happens in the context of inheritance. When a derived class defines a function with the same signature as that from its base class.





e.g you have a base class called vehicle


with a function called "steer vehicle"





maybe you make a class... vehicle :car...


where car, inherits all the properties and functions of vehicle.


and you have the function steer vehicle in the vehicle class.





but now you make a class vehicle: bicycle


and you decide that the steer vehicle function needs to be a bit different, so you can define the function "steer vehicle" again inside the bicycle class. and when you use the steerVehicle function from the bicycle class..... it will use this function, instead of the one from the base class. in otherwords.. over-riding the function in the base class.





hope that makes sense :)


No comments:

Post a Comment