Exam 15: Polymorphism and Virtual Functions

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

Downcasting causes the slicing problem.

Free
(True/False)
4.9/5
(37)
Correct Answer:
Verified

False

It is legal to have all member functions of a class be pure virtual functions.

Free
(True/False)
4.8/5
(37)
Correct Answer:
Verified

True

Write a program where the destructors should be virtual.Explain.

Free
(Essay)
4.9/5
(36)
Correct Answer:
Verified

The requested code and further discussion follow.
#include<iostream>
using namespace std;
class B
{
public:
B():bPtr( new int[5]){ cout << "allocates 5 ints\n";}
virtual ~B()
{ delete[] bPtr;cout << "deallocates 5 ints\n";}
private:
int * bPtr;
};
class D:public B
{
public:
D():B(),dPtr(new int[1000]){cout << "allocates 1000 ints\n";}
~D(){ delete[] dPtr;cout << "deallocates 1000 ints\n";}
private:
int * dPtr;
};
int main()
{
for (int i = 0;i < 100;i++)
{
B* bPtr = new D;
delete bPtr;
}
cout << "\nWithout virtual destructor,\n";
cout << "100 * 1000 + 500 ints allocated\n"
<< "but only 500 ints deallocated.\n";
cout << "\nWith virtual destructor,\n";
cout << "100 * 1000 + 500 ints allocated,and "
<< "100 * 1000 + 500 ints deallocated.\n";
}
In this code,if the base class destructor is not virtual,the system must bind the pointer to the destructor at compile time.The pointer in main,bPtr,is of type B*,i.e. ,pointer to B,so it is bound to B's member functions.The destructor for D,~D fails to run.
If the keyword virtual is removed from the destructor in the base class,class B,the derived class destructor ~D is not called.Approximately 400K of memory would then be leaked.

Redefining and overriding are exactly the same thing.

(True/False)
4.9/5
(41)

Write a class having a public pure virtual method.You need not put any other members in the class.

(Essay)
4.9/5
(42)

A derived class destructor always invokes the base class destructor.

(True/False)
4.9/5
(35)

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.

(Essay)
4.8/5
(41)

A class that has a pure virtual member function is called a concrete base class.

(True/False)
4.8/5
(43)

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.

(Essay)
4.8/5
(36)

A pointer to objects of a derived class can be assigned pointers to objects of the base class in the inheritance hierarchy.

(True/False)
4.9/5
(47)

A the binding of virtual function is done at runtime if called using an object.

(True/False)
4.9/5
(31)

It is OK to assign between objects of base type and objects of derived type.

(True/False)
4.7/5
(32)

Virtual functions are implemented with a table look up that is done at run time.

(True/False)
4.9/5
(36)

No objects can be defined of abstract base class type since it is an incomplete definition.

(True/False)
4.8/5
(36)

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: // ... };

(Essay)
4.9/5
(45)

Write a short program that shows how to defeat the slicing problem.

(Essay)
4.8/5
(34)

Upcasting causes no problems

(True/False)
4.8/5
(34)

Late binding refers to a failure to secure one's ski boots.

(True/False)
5.0/5
(32)

Only member functions can be virtual.

(True/False)
4.9/5
(33)

The virtual property is not inherited.

(True/False)
4.8/5
(43)
Showing 1 - 20 of 34
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)