Deck 15: Polymorphism and Virtual Functions

ملء الشاشة (f)
exit full mode
سؤال
A pointer to objects of a derived class can be assigned pointers to objects of the base class in the inheritance hierarchy.
استخدم زر المسافة أو
up arrow
down arrow
لقلب البطاقة.
سؤال
This is legal code.
class B
{
public:
// ...
virtual void f()= 0;
};
int main(){ B b1,b2;/*...*/ }
سؤال
Downcasting causes the slicing problem.
سؤال
A derived class destructor always invokes the base class destructor.
سؤال
It is legal to have all member functions of a class be pure virtual functions.
سؤال
Upcasting causes no problems
سؤال
Virtual functions allow old code to call new code.
سؤال
The virtual function mechanism binds the "right" function to objects.
سؤال
It is useful to define a class for which no objects may be defined.
سؤال
Virtual functions are implemented with a table look up that is done at run time.
سؤال
The virtual property is not inherited.
سؤال
No objects can be defined of abstract base class type since it is an incomplete definition.
سؤال
A class that has a pure virtual member function is called a concrete base class.
سؤال
Late binding refers to a failure to secure one's ski boots.
سؤال
A the binding of virtual function is done at runtime if called using an object.
سؤال
In a class,functions declared with the virtual keyword need not be defined.
سؤال
Only member functions can be virtual.
سؤال
It is OK to assign between objects of base type and objects of derived type.
سؤال
Destructors are automatically virtual.
سؤال
The base class destructor must be virtual.
سؤال
Write a program where the destructors should be virtual.Explain.
سؤال
Write a short program that shows how to defeat the slicing problem.
سؤال
Write a class having a public pure virtual method.You need not put any other members in the class.
سؤال
What is the error?
class B
{
public:
virtual void f();
};
virtual void B::f(){/*...*/}
سؤال
Why do you not get an undefined reference in virtual table error when you don't define a pure virtual function?
سؤال
In C++,a virtual destructor is invoked whenever a virtual constructor was used to create the object.
سؤال
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.
سؤال
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.
سؤال
Redefining and overriding are exactly the same thing.
سؤال
Is there an error?
class B
{
public:
void virtual f();
};
void B::f(){/*...*/}
سؤال
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.
سؤال
Give some simple recommendation for when a destructor should be declared virtual.
سؤال
Explain the difference between virtual functions,late binding,and polymorphism.
سؤال
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 Deck
1/34
auto play flashcards
العب
simple tutorial
ملء الشاشة (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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
5
It is legal to have all member functions of a class be pure virtual functions.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
6
Upcasting causes no problems
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
7
Virtual functions allow old code to call new code.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
8
The virtual function mechanism binds the "right" function to objects.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
9
It is useful to define a class for which no objects may be defined.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
10
Virtual functions are implemented with a table look up that is done at run time.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
11
The virtual property is not inherited.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
12
No objects can be defined of abstract base class type since it is an incomplete definition.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
13
A class that has a pure virtual member function is called a concrete base class.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
14
Late binding refers to a failure to secure one's ski boots.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
15
A the binding of virtual function is done at runtime if called using an object.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
16
In a class,functions declared with the virtual keyword need not be defined.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
17
Only member functions can be virtual.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
18
It is OK to assign between objects of base type and objects of derived type.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
19
Destructors are automatically virtual.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
20
The base class destructor must be virtual.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
21
Write a program where the destructors should be virtual.Explain.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
22
Write a short program that shows how to defeat the slicing problem.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
23
Write a class having a public pure virtual method.You need not put any other members in the class.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
24
What is the error?
class B
{
public:
virtual void f();
};
virtual void B::f(){/*...*/}
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
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?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
26
In C++,a virtual destructor is invoked whenever a virtual constructor was used to create the object.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
29
Redefining and overriding are exactly the same thing.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
30
Is there an error?
class B
{
public:
void virtual f();
};
void B::f(){/*...*/}
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
32
Give some simple recommendation for when a destructor should be declared virtual.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
33
Explain the difference between virtual functions,late binding,and polymorphism.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
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:
// ...
};
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.
فتح الحزمة
k this deck
locked card icon
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 34 في هذه المجموعة.