C++ Constructors Interview Preparation Guide
Download PDF
Add New Question

C++ Constructors frequently Asked Questions by expert members with experience in C++ constructors. These questions and answers will help you strengthen your technical skills, prepare for the new job test and quickly revise the concepts

51 C++ Constructors Questions and Answers:

1 :: 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.
Post Your Answer

2 :: What is virtual constructor?

A constructor of a class can not be virtual and if causes a syntax error.
Post Your Answer

3 :: What is constructor in C++?

Constructors allow initialization of objects at the time of their creation. Constructor function is a special function that is a member of the class and has same name as that of the class. An object’s constructor is automatically called whenever the object is created (statically or dynamically). Constructors are always public. They are used to make initializations at the time of object creation.

Consider following example of a stack class:

#include <iostream>
using namespace std;
class stack
{
int top, bottom;
int data[20];
stack() //constructor function
{
top = bottom;
cout <<”Inside Stack Constructor: Stack
initialized”;
}
int stackfull()
{
return ((top == max – 1)?1:0);
}
int stackempty()
{
return (top == bottom)?1:0);
}
void push(int no)
{
if(stackfull())
cout<<”Stack is full”;
else
data[++top] = no;
}
int pop()
{
if(stackempty())
cout<<”Nothing to pop.. Stack is Empty!”;
else
return(data[top--];
}
};

int main()
{
int i, no;
stack st; //object is created; hence constructor is
invoked- stack initialization done
cout <<” Entered Main\n”;
for(i = 0; i < 10; i++)
st.push(i);
no = s.pop();
cout <<”The popped element is:”<
return 0;
}

The o/p of the program would be:

Entered Main
Inside Stack Constructor: Stack initialized
The popped element is: 9

As seen above, the stack object is initialized automatically at the time of creation by the constructor. So we don’t need to write and invoke initialization functions explicitly.
Post Your Answer

4 :: Can you please explain Explain constructors and destructors?

Constructors are the member functions of the class that executes automatically whenever an object is created. Constructors have the same name as the class. Constructors initialize the class. Constructors can’t have return type. Destructors are called when the objects are destroyed.

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted. A destructor takes no arguments and has no return type.
Post Your Answer

5 :: 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.
Post Your Answer

6 :: Do you know what are destructors?

Destructors are complements of constructors. When an object is destroyed, its destructor is automatically called. Destructors are mainly useful for doing the clean up job. E.g. an object may have allocated some memory during its lifetime; destructors are the place where this memory is deallocated. Or an object may need to close some files by releasing its handles which it had previously obtained.

Destructor function has same name as that of a constructor; but the name is preceded by a tilde (‘~’) sign.

We will expand the above example of a stack class to include a destructor:

#include <iostream>
using namespace std;
class stack
{
int top, bottom;
int data[20];
stack() //constructor function
{
top = bottom;
cout <<”Inside Stack Constructor: Stack initialized\n”;
}
int stackfull()
{
return ((top == max – 1)?1:0);
}
int stackempty()
{
return (top == bottom)?1:0);
}
void push(int no)
{
if(stackfull())
cout<<”Stack is full”;
else
data[++top] = no;
}
int pop()
{
if(stackempty())
cout<<”Nothing to pop.. Stack is Empty!\n”;
else
return(data[top- -];
}
~stack() //destructor function
{
cout <<”Inside Stack Destructor: Stack Destroyed\n”;
}
};
int main()
{
int i, no;
stack st; //object is created; hence constructor is invoked- stack initialization done
cout <<”Entered Main\n”;
for(i = 0; i < 10; i++)
st.push(i);
no = s.pop();
cout <<”The popped element is:”<<no << “\n”;
return 0;
}// at the end of object’s lifetime, destructor is invoked

The o/p of the program would be:
Entered Main
Inside Stack Constructor: Stack initialized
The popped element is: 9
Inside Stack Destructor: Stack Destroyed
As seen from the o/p the constructors and destructors are automatically called.
Post Your Answer

7 :: What is virtual destructor and how to use it?

If the destructor in the base class is not made virtual, then an object that might have been declared of type base class and instance of child class would simply call the base class destructor without calling the derived class destructor.

Hence, by making the destructor in the base class virtual, we ensure that the derived class destructor gets called before the base class destructor.

class a
{
public:
a(){printf("\nBase Constructor\n");}
~a(){printf("\nBase Destructor\n");}
};

class b : public a
{
public:
b(){printf("\nDerived Constructor\n");}
~b(){printf("\nDerived Destructor\n");}
};
int main()
{
a* obj=new b;
delete obj;
return 0;
}

Output:
Base Constructor
Derived Constructor
Base Destructor

By Changing
~a(){printf("\nBase Destructor\n");}
to
virtual ~a(){printf("\nBase Destructor\n");}

Output:
Base Constructor
Derived Constructor
Derived Destructor
Base Destructor
Post Your Answer

8 :: Can you please explain the difference between copy constructor and an assignment operator?

A copy constructor is used to declare and initialize an object from another object.
E.g: integer I2(I1);

An assignment operator doesnot invoke the copy constructor. It simply assigns the values of an object to another, member by member.
Post Your Answer

9 :: What is shallow?

A shallow copy just copies the values of the data as they are. Even if there is a pointer that points to dynamically allocated memory, the pointer in the copy will point to the same dynamically allocated object.
Post Your Answer

10 :: What is deep copy?

A deep copy creates a copy of the dynamically allocated objects too. You would need to use a copy constructor and overload an assignment operator for this.
Post Your Answer
Add New Question