In c++ there is only virtual destructors, no constructors. Why?

Answer Posted / bala preethi

Since OOPS basics says always base class object to be
created and than its derivatives. So just consider the case
where we try to create object of derived class with virtual
contructor since pointer points to derived type object it
fetches derived constructor directly, without its base
class object constructed( instantiated )...
So Constructor cannot be made virtual bt when comes to case
of Destructor object destuction process must be reverse...
So we must make destructor as virtual.
This applies only when we going for Dynamic Memory
Allocation...

If, on the other hand, you were to have a pointer to object
A and assign it a dynamic object B, when you destroy it,
only object A's destructor is called and object B part of
the object is never destroyed. Virtual destructors are used
to solve this problem and destroy the objects in the right
order.

Is This Answer Correct ?    47 Yes 14 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can a destructor be called directly?

595


Advantage and disadvantage of routing in telecom sector

780


What is the full form of oops?

605


What is Difeerence between List obj=new ArrayList(); and ArrayList obj=new ArrayList()?

2098


Can we have inheritance without polymorphism?

606






Can you explain polymorphism?

576


Why is it so that we can have virtual constructors but we cannot have virtual destructors?

3818


What are the components of marker interface?

600


String = "C++ is an object oriented programming language.An imp feature of oops is classes and objects".Write a pgm to count the repeated words from this scenario?

1938


What is the advantage of oop over procedural language?

621


What are benefits of oop?

631


They started with the brief introduction followed by few basic C++ questions on polumorphism, inheritance and then virtual functions. What is polymorphims? How you will access polymorphic functions in C? How virtual function mechanism works?

1403


write string class as your own class in java without using any built-in function

1974


What is overloading in oops?

593


What is encapsulation example?

545