15 - Polymorphism and Virtual Functions Flashcards
What is polymorphism?
Polymorphism refers to the ability to associate many meanings to one function name by means of a special mechanism known as virtual functions or late binding.
When you make a function virtual, you are telling the compiler:
“I do not know how this function is implemented. Wait until it is used in a program, and then get the implementation from the object instance.”
The technique of waiting until run time to determine the implementation of a procedure is often called ..
late binding or dynamic binding.
Which programming languages have virtual functions as a default?
Java.
Why are virtual functions not default with C++?
Doing so uses more storage and makes your program run slower.
Undefined reference to Class_Name virtual table.
That’s an error message. How was it produced?
If any virtual member functions are not implemented before compiling, the compilation fails with those types of error messages.
Suppose you are designing software for a graphics package that has classes for several figures, such as rectangles, circles, ovals, and so forth. Show how virtual functions are useful.
Each figure might be an object of a different class, such as the Rectangle class or the Circle class. In a well-designed programming project, all of these classes would probably be descendants of a single parent class called, for example, Figure. Now, suppose you want a function to draw a figure on the screen. To draw a circle, you need different instructions from those you need to draw a rectangle. So, each class needs to have a different function to draw its kind of figure. If r is a Rectangle object and c is a Circle object, then r.draw( ) and c.draw( ) can be functions implemented with different code. The parent class Figure may have a function called center that moves a figure to the center of the screen by erasing it and then redrawing it in the center of the screen. The function Figure::center might use the function draw to redraw the figure to the center of the screen. By making the member function draw a virtual function, you can write code for the member function Figure::center in the class Figure and know that when it is used for a derived class - say, Circle - the definition of draw in the class Circle will be the definition used.
Suppose you are designing software for a graphics package that has classes for several figures, such as rectangles, circles, ovals, and so forth. You want a virtual function draw ( ) in a parent class Figure. How do you implement it?
You’re never going to need to draw a Figure that is not using the virtual function of the subclass, and so you need to make the Figure draw function a pure virtual function.
The way to make a member function into a pure virtual function is to mark it as virtual and to add the annotation = 0 to the member function declaration.
virtual void draw ( ) = 0;
A class has this code somewhere in its header file. virtual void draw ( ) = 0; What would you call this class?
This is a pure virtual function. A class with one or more pure virtual functions is called an abstract class.