Deck 15: Polymorphism and Virtual Functions

Full screen (f)
exit full mode
Question
A pointer to objects of a derived class can be assigned pointers to objects of the base class in the inheritance hierarchy.
Use Space or
up arrow
down arrow
to flip the card.
Question
This is legal code.
class B
{
public:
// ...
virtual void f()= 0;
};
int main(){ B b1,b2;/*...*/ }
Question
Downcasting causes the slicing problem.
Question
A derived class destructor always invokes the base class destructor.
Question
It is legal to have all member functions of a class be pure virtual functions.
Question
Upcasting causes no problems
Question
Virtual functions allow old code to call new code.
Question
The virtual function mechanism binds the "right" function to objects.
Question
It is useful to define a class for which no objects may be defined.
Question
Virtual functions are implemented with a table look up that is done at run time.
Question
The virtual property is not inherited.
Question
No objects can be defined of abstract base class type since it is an incomplete definition.
Question
A class that has a pure virtual member function is called a concrete base class.
Question
Late binding refers to a failure to secure one's ski boots.
Question
A the binding of virtual function is done at runtime if called using an object.
Question
In a class,functions declared with the virtual keyword need not be defined.
Question
Only member functions can be virtual.
Question
It is OK to assign between objects of base type and objects of derived type.
Question
Destructors are automatically virtual.
Question
The base class destructor must be virtual.
Question
Write a program where the destructors should be virtual.Explain.
Question
Write a short program that shows how to defeat the slicing problem.
Question
Write a class having a public pure virtual method.You need not put any other members in the class.
Question
What is the error?
class B
{
public:
virtual void f();
};
virtual void B::f(){/*...*/}
Question
Why do you not get an undefined reference in virtual table error when you don't define a pure virtual function?
Question
In C++,a virtual destructor is invoked whenever a virtual constructor was used to create the object.
Question
If the override specifier is added to the end of a member function declaration,what happens if the function is not specified as virtual in the parent class?

A)The function is overridden in the derived class.
B)There is a compiler error.
C)The function in the parent class will always be invoked.
Question
Suppose each of the base class and the derived class has a member function with the same signature.Suppose you have a base class pointer to a derived class object and call the common function member through the pointer.Discuss what determines which function is actually called,whether the one from the base class or the one from the derived class.Consider both the situations where the base class function is declared virtual and where it is not.
Question
Redefining and overriding are exactly the same thing.
Question
Is there an error?
class B
{
public:
void virtual f();
};
void B::f(){/*...*/}
Question
It is desirable to develop your programs incrementally.Code a little,test a little.If you do this with virtual functions,you get into trouble.Discuss the problem and the (very simple)solution.
Question
Give some simple recommendation for when a destructor should be declared virtual.
Question
Explain the difference between virtual functions,late binding,and polymorphism.
Question
Which functions in class D are virtual?
class B
{
public:
virtual void f();
virtual void g();
// ...
private:
//...
};
class D : public B
{
public:
void f();
void g(int);
private:
// ...
};
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/34
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 15: Polymorphism and Virtual Functions
1
A pointer to objects of a derived class can be assigned pointers to objects of the base class in the inheritance hierarchy.
False
2
This is legal code.
class B
{
public:
// ...
virtual void f()= 0;
};
int main(){ B b1,b2;/*...*/ }
False
3
Downcasting causes the slicing problem.
False
4
A derived class destructor always invokes the base class destructor.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
5
It is legal to have all member functions of a class be pure virtual functions.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
6
Upcasting causes no problems
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
7
Virtual functions allow old code to call new code.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
8
The virtual function mechanism binds the "right" function to objects.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
9
It is useful to define a class for which no objects may be defined.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
10
Virtual functions are implemented with a table look up that is done at run time.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
11
The virtual property is not inherited.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
12
No objects can be defined of abstract base class type since it is an incomplete definition.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
13
A class that has a pure virtual member function is called a concrete base class.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
14
Late binding refers to a failure to secure one's ski boots.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
15
A the binding of virtual function is done at runtime if called using an object.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
16
In a class,functions declared with the virtual keyword need not be defined.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
17
Only member functions can be virtual.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
18
It is OK to assign between objects of base type and objects of derived type.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
19
Destructors are automatically virtual.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
20
The base class destructor must be virtual.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
21
Write a program where the destructors should be virtual.Explain.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
22
Write a short program that shows how to defeat the slicing problem.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
23
Write a class having a public pure virtual method.You need not put any other members in the class.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
24
What is the error?
class B
{
public:
virtual void f();
};
virtual void B::f(){/*...*/}
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
25
Why do you not get an undefined reference in virtual table error when you don't define a pure virtual function?
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
26
In C++,a virtual destructor is invoked whenever a virtual constructor was used to create the object.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
27
If the override specifier is added to the end of a member function declaration,what happens if the function is not specified as virtual in the parent class?

A)The function is overridden in the derived class.
B)There is a compiler error.
C)The function in the parent class will always be invoked.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
28
Suppose each of the base class and the derived class has a member function with the same signature.Suppose you have a base class pointer to a derived class object and call the common function member through the pointer.Discuss what determines which function is actually called,whether the one from the base class or the one from the derived class.Consider both the situations where the base class function is declared virtual and where it is not.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
29
Redefining and overriding are exactly the same thing.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
30
Is there an error?
class B
{
public:
void virtual f();
};
void B::f(){/*...*/}
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
31
It is desirable to develop your programs incrementally.Code a little,test a little.If you do this with virtual functions,you get into trouble.Discuss the problem and the (very simple)solution.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
32
Give some simple recommendation for when a destructor should be declared virtual.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
33
Explain the difference between virtual functions,late binding,and polymorphism.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
34
Which functions in class D are virtual?
class B
{
public:
virtual void f();
virtual void g();
// ...
private:
//...
};
class D : public B
{
public:
void f();
void g(int);
private:
// ...
};
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 34 flashcards in this deck.