Sunday, August 9, 2009

virtual

When a function is made virtual.C++determinrs which function to use a run time based on type of object pointed to by the base pointer rather than type of the pointer.Thus by makintg the base pointer to pont to different objects we can execute different versions of the virtual function.
For example suppose a graphics program includes several different shapes a triangle,a ball,a suare and so on,Each of this classes has a member function draw() that causes the object to be drwn on the screen.
Now suppose you plan to make a grouping a number of these elements together and you want to draw the picture in a convinent way.One approach is to create an array that hold s pointers to all differnt objects in the oicture .The array might be defined like this
shape *ptarr[100]
If we insert pointers to all the shapes in to this array,you can then drawn an entire picture useing a simple loop.More

No comments:

Post a Comment