Q1. What are Virtual Constructor and Virtual Destructor? What are the differences between a Virtual Constructor and a Constructor? What are the differences between a Virtual Destructor and a Destructor? Explain with examples.
- Virtual constructors are not allowed in C++ as constructors create objects before a vtable can be used.
- A virtual destructor ensures proper deallocation of derived objects through base class pointers.
- Without a virtual destructor, deleting a derived object via a base pointer causes memory leaks.
- Declaring a base class destructor `virtual` enables polymorphic destruction via the vtable.
Answer: In C++, the concepts of 'virtual constructor' and 'virtual destructor' are distinct, with the former being an impossibility in the language, while the latter is a fundamental mechanism for ensuring proper resource management in polymorphic class hierarchies. Understanding these concepts is crucial for writing robust and memory-safe C++ programs, especially when dealing with inheritance and dynamic memory allocation. ### Virtual Constructor A 'virtual constructor' is a concept that does not exi...