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.

Kamelsh singh said:   1 decade ago
How come data members can be declared as virtual ?

Venky said:   1 decade ago
What is a virtual ?

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.

Bhavik said:   1 decade ago
But destructors can be declared virtual right?

Omkar said:   1 decade ago
Why destructor declare as a virtual?

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.

Sachin said:   1 decade ago
Constructor can not be virtual. But destructors can be declared virtual.

Vikas said:   1 decade ago
Agree with sachin destructors can be declared virtual.

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.


Post your comments here:

Your comments will be displayed after verification.