C++ Programming - OOPS Concepts - Discussion

Discussion :: OOPS Concepts - General Questions (Q.No.27)

27. 

Which of the following cannot be used with the keyword virtual?

[A]. class
[B]. member functions
[C]. constructor
[D]. destructor

Answer: Option C

Explanation:

No answer description available for this question.

Naej said: (Apr 30, 2012)  
Why not destructor?

Selvaraj said: (May 24, 2012)  
Constructor is also a initialization part. Why we are not use virtual.

Rekha said: (Jun 2, 2012)  
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.

Jack said: (Sep 2, 2012)  
Can you please explain more clearly?

Rahul said: (Sep 2, 2012)  
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..

Babaji said: (Sep 18, 2012)  
Thanks janu.

Biraj Borah said: (Nov 19, 2012)  
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.

Sushil said: (Dec 12, 2012)  
There is nothing like Virtual Constructor. The Constructor can't be virtual as the constructor is a code which is responsible for creating an instance of a class and it can't be delegated to any other object by virtual keyword means.

Abhinav said: (Dec 7, 2013)  
Why not distructor ? it will free at one time.

Mayank said: (Dec 18, 2013)  
Why do we need virtual class?

Rajendra said: (May 4, 2014)  
Can we declare virtual class?

Pooja Singh said: (Jun 6, 2014)  
Yes, we can declare class as a virtual. It removes ambiguity of same function name used in different classes.

Example - hybrid inheritance.

Prosenjit Santra said: (Feb 24, 2015)  
I think we cannot create virtual constructor as well as virtual destructor.

Sumit said: (Jun 5, 2015)  
You are thinking well. But, we can create virtual destructor.

Prakash Kuthi said: (Aug 6, 2016)  
Hi, it was helpful thanks to all.

Gelu Menumorut said: (Aug 11, 2016)  
There is no such a thing as a "virtual class", but only the expression "virtual base class" that is just an euphemism that has sense only in the context of inheritance, meaning the base class involved in virtual inheritance, here the word "virtual" is actually an attribute of the inheritance not of the class. Is like "C++ slang", but altering the expression and it's context and making it official by saying it's a correct answer is weird, to say the least.

Pankaj said: (Aug 30, 2016)  
Constructors can be virtual and static. So right answer is destructor D option.

Nitin said: (Jun 22, 2017)  
The virtual not work with destructor.

Rabz said: (Sep 17, 2020)  
Constructor cannot be virtual because the time when constructor is called, there is no virtual table in the memory yet. But destructor can be virtual.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.