What are virtual functions? Describe a circumstance in which virtual functions would be appropriate.

What will be an ideal response?

Programmers use virtual function to enable generic processing of an entire class hierarchy of objects through a base-class pointer. If the program invokes a virtual function through a base-class pointer to a derived-class object, the program will choose the correct derived-class function dynamically (i.e., at execution time) based on the object type—not the pointer type. For example, in a shape hierarchy, all shapes can be drawn. If all shapes are derived from a base class Shape which contains a virtual draw function, then generic processing of the hierarchy can be performed by calling every shape’s draw generically through a base class Shape pointer. For this mechanism to work, each version of the virtual function throughout the class hierarchy must have the same function prototype.

Computer Science & Information Technology

You might also like to view...

To modify information on your Squarespace site, ________ mode must be active.

a. WYSIWYG b. Content c. Edit d. Revise

Computer Science & Information Technology

The next generation of the World Wide Web is called __________.

Fill in the blank(s) with correct word

Computer Science & Information Technology