+91 90691 39140 | +1 253 214 3115 | info@hub4tech.com | hub4tech

C++ Constructors Interview Questions and Answers

What is a constructor?

A constructor is a special member function whose task is to initialize objects of its classes. Its name is same as that of its class. It is invoked when an object of that class is created.

What is Virtual destructors?

The explicit destroying of object with the use of delete operator to a base class pointer to the object is performed by the destructor of the base-class is invoked on that object.

The above process can be simplified by declaring a virtual base class destructor.
All the derived class destructors are made virtual in spite of having the same name as the base class destructor. In case the object in the hierarchy is destroyed explicitly by using delete operator to the base class pointer to a derived object, the appropriate destructor will be invoked.

What are the restrictions apply to constructors and destructors?

The following restrictions apply to constructors and destructors

Constructors and destructors don't return values.
The addresses of constructors and destructors can't be taken so we can't use references and pointers on them.
Constructors cannot be declared with the keyword virtual.
Constructors and destructors cannot be declared static, const, or volatile.

Tell me how should a constructor handle a failure?

Throw an exception

Constructors don't have a return type, so it's not possible to use return codes. The best way to signal constructor failure is therefore to throw an exception.

Explain the order in which constructors are called when an object of a derived class is created.

The constructors of any virtual base classes are called first in the order of inheritance.
Non-virtual base class constructors are called next.
The derived class constructor is called last.

Copyright ©2015 Hub4Tech.com, All Rights Reserved. Hub4Tech™ is registered trademark of Hub4tech Portal Services Pvt. Ltd.
All trademarks and logos appearing on this website are the property of their respective owners.