Archived

This topic is now archived and is closed to further replies.

Daivuk

virtual (OO)

Recommended Posts

It is basically for generic coding. The purpose of that is to reduce volume of code required to accomplish a given task through code reuse. Say you have some code that will draw a list of graphic objects. Those objects can be points, lines, rectangles and ellipses. You could have four seperate lists, one for each type. You would then have a function that draws a list of points, another does a list of lines, etc. You could alternatively put it in one list and have one function with an if statement saying is this one a point, a line, etc. Every time you add a new type of graphic object you have to add to that conditional. Virtual functions allow you to eliminate that conditional and the need to update that code every time you add a new type of graphic object. Instead all you have to do is in the definition of the new graphic object provide a virtual function that will draw it. It inherits off of a generic graphic object that also defines the same virtual function. The code that draws the list doesn''t know anything about a specific type of graphic object. It only knows about generic graphic objects and that they can be drawn. So it does care which types of graphic objects are actually in the list and thus which function needs to be called. It just calls draw for the generic graphic object and the compile handles calling the right function depending upon the type of graphic object it actually is.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I''m wondering.. Is Québec an area in Canada where people use French as their primary language?

Share this post


Link to post
Share on other sites