C++ Programming - Constructors and Destructors - Discussion

Discussion Forum : Constructors and Destructors - General Questions (Q.No. 18)
18.
Which of the following cannot be declared as virtual?
Constructor
Destructor
Data Members
Both A and C
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
20 comments Page 1 of 2.

SOFFIA said:   1 decade ago
@Venky.

Virtual functions are mainly use in inheritance..and
Use to mainly save space..

Example - class A is the base class and B and C are the derived class from A..so class B and C both inherit the features of class A.

When we make another derived class whose name is D from B and C then features of class A will come 2 times.. one time from class b and second time from class c so to control this we use virtual functions.

I HOPE YOU WILL UNDERSTAND SOMETHING

Navin said:   1 decade ago
Constructor can not be virtual. Virtual keyword performs very vital role in dynamic binding of function if object is created on heap. But to create object we first need a static binding method which is nothing but constructor. Again dynamic binding task is accomplished only with the help of Virtual table. But Virtual table itself created if object is created on heap using constructor.

Rahat said:   1 decade ago
Constructor can't be declared as virtual. Because if A is base class, B is derived class, then.

When A * a1=new B; is executed, derived class B's constructor is called, but the base class A'S constructor is not called, it create conflict, compiler show compile time error, to solve this clone function is used.

Jpb said:   2 years ago
In C++, constructors cannot be declared as virtual. Virtual functions are used for dynamic dispatch and polymorphism, but constructors are called at the time of object creation and are not inherited like other member functions. Therefore, constructors cannot be virtual.

So, the correct answer is Constructor.

Natasha said:   1 decade ago
Because if you don't declare a destructor as virtual in case of inheritance if you get out of scope the objects of the derived class won't be destroyed so the answer isn't correct as in some cases you MUST declare destructor as virtual.

Prasanthreddy said:   9 years ago
The virtual constructor is not possible because the constructor is called when an object creates but virtual is possible only after object creation so, virtual constructor not possible but the virtual destructor is possible.

Tushar Wagh said:   8 years ago
I Agree that constructor can't be virtual but destructures can be virtual.

Virtual distructor is used to call the base class destructor in reverse order of herachiy of classes.

Wangchuk said:   1 decade ago
How you can say that destructor cannot be declare as virtual. In the some text book I have found that destructor can be declare as virtual.

Vedi G said:   6 years ago
All ways constructor can not be virtual, but we can apply destructor as a virtual.

It shows the wrong answer.

Laxmareddy said:   1 decade ago
Both can be declared as virtual. There is no limitation to declare only destructor as virtual.


Post your comments here:

Your comments will be displayed after verification.