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?
Discussion:
20 comments Page 2 of 2.
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.
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.
Vikas said:
1 decade ago
Agree with sachin destructors can be declared virtual.
Sachin said:
1 decade ago
Constructor can not be virtual. But destructors can be declared 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.
Omkar said:
1 decade ago
Why destructor declare as a virtual?
Bhavik said:
1 decade ago
But destructors can be declared virtual right?
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.
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
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
Venky said:
1 decade ago
What is a virtual ?
Kamelsh singh said:
1 decade ago
How come data members can be declared as virtual ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers