You are here:

C++/function overriding

Advertisement


Question
hi 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

Answer
Hello 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

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.