C++ Programming - OOPS Concepts - Discussion

Discussion Forum : OOPS Concepts - General Questions (Q.No. 27)
27.
Which of the following cannot be used with the keyword virtual?
class
member functions
constructor
destructor
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
20 comments Page 2 of 2.

Pooja Singh said:   1 decade ago
Yes, we can declare class as a virtual. It removes ambiguity of same function name used in different classes.

Example - hybrid inheritance.

Naej said:   1 decade ago
Why not destructor?

Mayank said:   1 decade ago
Why do we need virtual class?

Abhinav said:   1 decade ago
Why not distructor ? it will free at one time.

Biraj Borah said:   1 decade ago
A constructor cannot be virtual.

The reason behind this: is that a virtual keyword makes a virtual table to map function at run-time with help of Vptr (virtual pointer).

Now this virtual pointer Vptr is the first 4 bytes of the object.

So if the constructor has been called then the Vptr has to be present in the object allocation.

But wait a minute. To create an object we need constructor.

Babaji said:   1 decade ago
Thanks janu.

Rahul said:   1 decade ago
As simple as that..we made the model (virtual ) of those things which are difficult to understand and those which can perform more than one task..

From above questions a class can have multiple variables n member functions, access specifiers..

On the other hand , MEMBER FUNCTIONS can also perform many tasks .Therefore their model should also be made..

Constructor just need to do one thing..that is INITIALIZATION by ASSIGNMENT..n that's it..not any task..

Desrtuctors also performed many tasks..therefore their virtual also exist..

Jack said:   1 decade ago
Can you please explain more clearly?

Rekha said:   1 decade ago
At runtime looking at the type of object referred by a pointer or reference it is decided which destructor to call -- the destructor of the base class or the destructor of the derived class.

For example, suppose B is a base class and D is a class derived from B and suppose both classes have declared their destrcutor as virtual. Suppose a pointer B *ptr is initialized as follows:

B *ptr = new D();

Now the ptr is of type B* but points to an object of D. So when this object is freed or goes out of scope D's destructor will be called since the destructors have been declared as virtual.

Selvaraj said:   1 decade ago
Constructor is also a initialization part. Why we are not use virtual.


Post your comments here:

Your comments will be displayed after verification.