Thursday, January 25, 2007

C++ Interview Question Part One

C++ Interview Question:

1. Does a class provide default Copy Constructor?
Yes! The default copy constructor is being provided automatically by the compiler if not implemented separately. In this, it puts the code for coping the data members and other variables that stay in stack. If something is being created by malloc or new in the heap, those are not being copied by the default copy constructor provide by the compiler.

2. What does a default Copy Constructor do?
A default copy constructor is being provided by the compiler, when an attempt to copy an existing object is made. In this case the control goes to the default copy constructor. It generates a new object, and makes the values of data members of the new object which are in the stack, same as the parent object. It doesn’t copy the variables that are created in the heap. Simply speaking, a compiler supplied default copy constructor doesn’t take care of the things in an object, that are being created using malloc/calloc or new.

3. Can a program have a virtual constructor?
Generally we don’t need an overridable constructor. So constructor should not be declared as virtual. But in a class, we can make a static method, which will call the private/protected constructor and create an object. In that case the constructor is called as virtual constructor.

4. When does a programmer need to implement his own copy constructor?
Though compiler automatically provides the default copy constructor, sometime a programmer needs to implement his own copy constructor. We can take up a case here:
In the default constructor some memory allocation has been done for few data members and hence those will be created in the heap. In the destructor corresponding de-allocation code is there. Now if an attempt to copy an object is made, it won’t call the default constructor but it will call the default copy constructor which will copy the data member variable from stack and copy the pointers but won’t allocate any new memory space for the new copied object. So same pointers will exist in both the parent and copied object. This will not only create a great ambiguity but runtime error will occur when attempts will be made to delete both the objects. First object will get deleted properly. When it will try to delete the other object, the common pointer won’t exist and runtime error will come. Even if user stops the application it will try to delete all the objects in the memory and runtime error will occur. In a single word, copy constructor is needed to be implemented independently, when the parent object has some allocated memory in heap for some of its data member and that gets deleted by destructor. Copy constructor should consist that much of code for memory allocation for the newly created object.Apart from this, programmer can implement his own copy constructor to copy any other things, which he wants specifically.

5. 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.
Logically thinking, if we can consider the same as copy constructor also, big confusion will come. If we pass an object as a function parameter, by value, it will get copied first and then be passed to the function. At the time of getting copied it should call the copy constructor. So in this case, if we consider the above described constructor as copy constructor, at the very beginning of the function call, it wil attempt to copy the object and hence again call the same function and hence will go towards an infinite loop. This will probably be ended up with an unexpected stack overflow.
It will give compiler error if an object of the same class is being passed to a constructor, but it doesn’t give error if more than one different objects are being passed to the constructor . In this case it doesn’t treat it as copy constructor, but a normal overloaded constructor.

6. What is the return parameter of a constructor and why?
Constructor in never being called directly. It is being called automatically by the compiler when an object in being created (or copied). Hence it can’t return any parameter. Logically thinking, if it would have been made to return a parameter, how do we put the code to create an object?

7. Are the “default constructor” and “constructor with default parameter” same?
Default constructor is a constructor, which can be called with no argument. So a constructor with all the parameters as default argument can be called as default constructor. A constructor with one or more default parameters (but not all the parameters) can be called “constructor with default parameter” but that won’t be the default constructor.
If a constructor with no argument and a constructor with all default arguments are being implemented then object-creation will generate an ambiguity regarding which constructor is to be called.

1 comment:

saurav said...

your all para is very useful.........thaxxx for this imp para.............