Constructor Destructor – C++ Programming Interview Questions

C++ tricky and conceptual programming interview questions  and answers on constructor and destructor with explanation asked in technical interviews.

Topic – Constructor and Destructor


Q-In below class, write a constructor to initialize const data member.

class A{
	const int id;
public:

	//Here, Write constructor to initialize const int variable id;

	void display(){
		cout<<"Value :"<< this->id;
	}	
};

Answer: To initialize a const data member we need to use initializer list in the class constructor.

We can not initialize const data member in C++ using assignment operator in constructor e.g.

A(int b) // constructor
	{	
		this->count = b;
	}

Example: correct way to initialize const data

class A{
	const int id;
public:

	A(int b):id(b) // Initializer list
	{		
	}

	void display(){
		cout<<"Value :"<< this->id;
	}	
};
int main()
{
	A ob(5);
	ob.display();

	return 0;
}


Q-What is the output of below program? Explain your answer.

#include
using namespace std;

class Boy{
public:
	Boy(){		
		cout<<"Boy constructor\n"; Boy *b = new Boy(); b->run();
	     }
	void run(){
		cout<<"Run...\n";
	}
	void stop(){
		cout<<"Stop...\n";
	}
};
int main()
{
	Boy ob;
	ob.stop();

	return 0;
}

Answer: This program will print “Boy constructor” for infinite time.

Explanation: When we create the object in main() i.e. Boy ob; class constructor will be called. And, inside the constructor body “Boy constructor” gets printed on console and again.



Q) In below C++ program, when destructor will be called in main () and what will be the order of destructor call?

class Fruit
{
public:	
	virtual void colour(){
		cout<<"Default colur"<<endl;
	}
	~Fruit(){
		cout<<"Fruit destructor"<<endl;
	}
};

class Apple:public Fruit{

public:
	void colour(){
		cout<<"Green..."<<endl;
	}
	~Apple(){
		cout<<"Apple destructor"<<endl;
	}
};

int main()
{
	Fruit *p = new Apple();
	delete p;

	return 0;
}

Answer:

For a C++ class object, destructor is called when we de-allocate and object or we can say when we delete a pointer that hold the object.

So, in above C++ code examle, destructor will be called at “delete p” statement.

In C++ object oriented programming the execution of order of constructor calling is from base to derived and order of destructor class is from vice versa i.e. from derived to base class.

However, in above program only base class destructor will be called, Hence output will be

Fruit destructor

Actually, in polymorphic classes( that contain virtual function) to maintain the hierarchy of destructor call we use virtual desturctor in base class, other wise derived class destructor will not be called.

So, to get the output as

Apple desturctor
Fruit destructor

We need to make base class constructor as virtual. for example

class Fruit
{
public:	
	virtual void colour(){
		cout<<"Default colur"<<endl;
	}
	//make destructor virtual to
	//maintain the hiararchy of destrctor
	//call
	virtual ~Fruit(){
		cout<<"Fruit destructor"<<endl;
	}
};

You can read more about virtual destructor with example in CPP object oriented programming.


Related Posts