QnA on Constructor & Destructor in C++
Below are some set of questions based on the Constructor and Destructor topics. These questions are designed for a quick revision of the constructor concepts
and also helpful for technical interviews.
-
Q: What is a Constructor ?
Ans: Constructor is the feature of the OOPS concept in C++ which generally creates an object of the class and also initializes it. Constructor is just like a function with the name similar to the class name. Constructor is called every time when a new object of the class. -
Q: What is Destructor ?
Ans: Destructor is used to destroy the object created by the constructor and free the memory. It is the function with the name similar to the class and start with the '~' key. Destructor is called every time whenever the created object goes out of scope or when delete function is called. -
Q: Write a code snippet to describe the syntax of constructor and destructor.
Ans:
class testClass{ … testClass(); // default constructor ~testClass(); // destructor … }
-
Q: What are functions provided by the class by default ?
Ans: The default function provided by the class are :-
-
Constructor
-
Destructor
-
Copy Constructor
-
Assignment Operator
-
-
Q: What is a default constructor ?
Ans: Default constructor is provided by the class and does not contain any argument. -
Q: Can a constructor throws an exception?
Ans: No, Constructor never throws an exception. -
Q: What is a copy constructor ?
Ans: Copy Constructor is a member function of the class which generally initialize the object using another object of the same class. If we don't implement the copy constructor in our class then the compiler implement the default the one for us which does a member-wise copy between objects.
Syntax of copy constructor is:-
ClassName(const ClassName &obj); -
Q: When will the copy constructor is called ?
Ans: Copy constructor is called in the following scenarios:-
-
When a function returns an object of the class
-
When an object is passed by value in the argument of the function.
-
When we construct an object based on another object of the same class. Ex-
testClass obj2(obj1); or testClass obj2 = obj1;
-
-
Q: Why "const reference" used in the argument of copy constructor ?
Ans:
Reason for using const:- Const is used as function parameter in the copy constructor because it allows to access the previously created object but not change the existing value of that object.
Reason for using reference:- Copy constructor itself is a function. So if we pass an argument by value in a copy constructor, a call to copy constructor would be made to call copy constructor which becomes a non-terminating chain of calls. Therefore compiler doesn’t allow parameters to be passed by value. -
Q: Why we need the user defined copy constructor if compiler itself provides a default copy constructor ?
Ans: Action of default copy constructor is only limited to shallow copy, but when we are working with the pointers and need to perform the deep copy then we have to define our own copy constructor. -
Q: Explain 'Shallow copy' and 'Deep Copy' ?
Ans:
Shallow Copy:- When compiler do the member-wise copy it will directly copy each and every members of the class object(say obj1) to the newly created object(say obj2). In this way the new object now contains the exact copy of every member of the previous object. But in this case both the objects obj1 and obj2 will point to the same memory location where the members variables are the stored. Compiler will not create the separate memory location for the new object obj2.
Deep Copy:- Shallow copy works fine in general but it is not sufficient when we deals with pointers. In case of pointers we need to copy both the value as well as address. So we define our own copy constructor that will copy the members to the new object and also define the new memory space for them.
-
Q: Can we define the constructor as a private member of the class ?
Ans: Yes, a constructor can be declared as the private member of the class. One example of this concept is the singleton design pattern where our main goal is to ensure that only one instance or object of the class will be created and is used throughout he program. -
Q: Differentiate between working of copy constructor and overloaded assignment operator ?
Ans: In case of copy constructor, a new object is created using the context of existing object of the same class. Also in case of copy constructor a separate memory block will be created for the newly created object.
In case of assignment operator, it is called when and already declared or initialized object is assigned a new value for another existing object. In this case there is no separate memory block will be created. -
Q: Can we define copy constructor as a private member of the class ?
Ans: Yes, a copy constructor can be defined as the private member of a class. When we defined it as a private member then objects of that class become non-copyable. It is useful in the scenario when our class has pointers or dynamically allocated resources. -
Q: Can a constructor be virtual ?
Ans: No, a constructor cannot be a virtual. We know that constructor will be called automatically when an instance or object of the class is created. And in order to create the object compiler needs to know the complete information of the class means the exact type that we needs to create. Also we know that the concept of virtual function works on the basis of vtable(virtual table). Now the main catch here is when the constructor of a class is executed then by that time there is no vtable present in the memory, it means there is no vptr(virtual pointer) defined yet. So, it is not possible to define the constructor as virtual. -
Q: What is a virtual destructor ?
Ans: Yes, the concept of virtual destructor is available in C++. It will be used whenever in our program we are having a Derived class which is defined from a Base class, and we are doing the upcasting, means defining a base class pointer pointing to the derived class.
Ex. Base bptr* = new Derived;
Here "Base" is the name of our base class and "Derived" is the name of our derived class. Now in this scenario when we are not using the virtual destructor concept and delete the pointer bptr then only base class destructor is called and the instance of derived class remain undestructed which is not good it might result in the memory leak.
So here when we define the destructor of Base class as virtual then, first the Derived's class destructor will be called and after that Base's class destructor will get called. -
Q: Is it possible to overload the destructor for a class ?
Ans: No, the destructor can't be overloaded. Every class has only one destructor of it own and it never takes any parameter. -
Q: What is the initializer list and when do we use it ?
Ans: Initializer is the list of comma separated values used for initializing the class member variables. This provided inside the class definition when we declare the constructor followed by the colon.
Scenarios when we use the initializer list to initialize the data members are:--
To initialize the member objects which do not have default constructor.
-
To initialize the non-static const data members.
-
To initialize the members of the base class.
-
Also using the initializer list method is bit faster than the using the assignment operator while initializing the member variables.
-
For detailed C++ Study material follow the link:- C++ Learning Series