15 - Polymorphism and Virtual Functions Flashcards

1
Q

What is polymorphism?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When you make a function virtual, you are telling the compiler:

A

“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.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The technique of waiting until run time to determine the implementation of a procedure is often called ..

A

late binding or dynamic binding.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which programming languages have virtual functions as a default?

A

Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why are virtual functions not default with C++?

A

Doing so uses more storage and makes your program run slower.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Undefined reference to Class_Name virtual table.

That’s an error message. How was it produced?

A

If any virtual member functions are not implemented before compiling, the compilation fails with those types of error messages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
A class has this code somewhere in its header file. 
virtual void draw ( ) = 0;
What would you call this class?
A

This is a pure virtual function. A class with one or more pure virtual functions is called an abstract class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly