C++ interview question & answer

Back
What is constructor or ctor?
Constructor creates an object and initializes it. It also creates vtable for virtual functions. It is different from other methods in a class.

What about Virtual Destructor?
Yes there is a Virtual Destructor. A destructor can be virtual as it is possible as at runtimedepending on the type of object caller is calling to, proper destructor will be called.

What is the difference between a copy constructor and an overloaded assignment operator?
A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.

Can a constructor throws an exception?
The constructor never throws an error.

What is default constructor?
Constructor with no arguments or all the arguments has default values.

What is copy constructor?
Constructor which initializes the it's object member variables ( by shallow copying) with another object of the same class. If you don't implement one in your class then compiler implements one for you. for example:
(1) Boo Obj1(10); // calling Boo constructor
(2) Boo Obj2(Obj1); // calling boo copy constructor
(3) Boo Obj2 = Obj1;// calling boo copy constructor


When are copy constructors called?
Copy constructors are called in following cases:
(1) When a function returns an object of that class by value
(2) When the object of that class is passed by value as an argument to a function
(3) When you construct an object based on another object of the same class
(d) When compiler generates a temporary object
What is Virtual Destructor?
Using virtual destructors, you can destroy objects without knowing their type - the correct destructor for the object is invoked using the virtual function mechanism. Note that destructors can also be declared as pure virtual functions for abstract classes. if someone will derive from your class, and if someone will say "new Derived", where "Derived" is derived from your class, and if someone will say delete p, where the actual object's type is "Derived" but the pointer p's type is your class.

Can a copy constructor accept an object of the same class as parameter, instead of reference of the object?
No. It is specified in the definition of the copy constructor itself. It should generate an error if a programmer specifies a copy constructor with a first argument that is an object and not a reference.

What's the order that local objects are destructed?
In reverse order of construction: First constructed, last destructed. In the following example, b's destructor will be executed first, then a's destructor:
void userCode()
{
Plaza a;
Plaza b;
...
}

What's the order that objects in an array are destructed?
In reverse order of construction: First constructed, last destructed. In the following example, the order for destructors will be a[9], a[8], ..., a[1], a[0]:
void userCode()
{
Plaza a[10];
... }


How virtual functions are implemented C++?
Virtual functions are implemented using a table of function pointers, called the vtable. There is one entry in the table per virtual function in the class. This table is created by the constructor of the class. When a derived class is constructed, its base class is constructed _rst which creates the vtable. If the derived class overrides any of the base classes virtual functions, those entries in the vtable are overwritten by the derived class constructor. This is why you should never call virtual functions from a constructor: because the vtable entries for the object may not have been set up by the derived class constructor
What is a "pure virtual" member function?
The abstract class whose pure virtual method has to be implemented by all the classes which derive on these. Otherwise it would result in a compilation error. This construct should be used when one wants to ensure that all the derived classes implement the method defined as pure virtual in base class.

What is pure virtual function? or what is abstract class?
When you de_ne only function prototype in a base class without implementation and do the complete implementation in derived class. This base class is called abstract class and client won't able to instantiate an object using this base class. You can make a pure virtual function or abstract class this way..
class Boo
{
void foo() = 0;
}
Boo MyBoo; // compilation error

What is Pure Virtual Function? Why and when this is used?
The abstract class whose pure virtual method has to be implemented by all the classes which derive on these. Otherwise it would result in a compilation error. This construct should be used when one wants to ensure that all the derived classes implement the method defined as pure virtual in base class.

How Virtual functions call up is maintained?
Through Look up tables added by the compile to every class image. This also leads to performance penalty.

What is a virtual destructor?
The simple answer is that a virtual destructor is one that is declared with the virtual attribute. The behavior of a virtual destructor is what is important. If you destroy an object through a caller or reference to a base class, and the base-class destructor is not virtual, the derived-class destructors are not executed, and the destruction might not be complete.

Share your requirement with me and give your business a much needed thrust.
CONTACT HERE