C++/function overriding
Expert: Eddie - 10/3/2005
Questionhi eddie,
thanks for the answer.i understand that by using scope resolution operator we can call the base class function from the derived clas function. would you explain me in what cases function overriding may prove to be useful?
regards,
june
AnswerHello june, thank you for the question.
Function overridding is useful when you want to have multiple derived classes implementing a base class function. You can put the common code to all of the derived classes in the base class function, then in each of the derived class functions you can add the code specific to each one, and then just invoke the parent method.
Lets say we have a base class Entity, that represents any. character in a game. You write a virtual function called Update that is in the parent class. In this function you put your code for updating the position, orientating the entity in 3d space, collision code, and what have you. Then you derive a class from Entity called Bot, which will represent a NPC with AI in the game. You override the the Update function, and put your AI code to track other players in the game, then call the Entity class Update to suck in all of the common functionality.
This is a good example of when functional overriding is useful. If you have any other questions, please don't hesistate to ask.
I hope this information was helpful.
- Eddie