1. What is destructor?
Ans. Destructor is a member function which is called when an object is deleted/destroyed or goes out of scope.
2. What is the purpose of using a destructor in C++?
Ans. The main purpose of destructor is to free all the resources (opened files, opened sockets, database connections, resource locks etc.) which are allocated during your object’s life time.
3. What is the use of a constructor?
Constructor is a special function having same name as class name. Constructor is called at the time of creating object to your class. Constructor is used to initialize the instance variables of an object while creating it. Constructor is also used to create virtual tables for virtual functions.
4. What if I don’t use copy constructor? Where does it create problem?
Please see copy constructor
5. Does C++ compiler create default constructor when we write our own?
In C++, compiler by default creates default constructor for every class. But, if we define our own constructor, compiler doesn’t create the default constructor.
6. What is the order of constructor execution in C++?
Ans. First base class constructor is executed and then derived class constructor, so execution happens from top to bottom in inheritance tree.
7. What is the order of destructor execution in C++?
Ans. Generally derived class destructor, and then base class destructor. Except in case if we are taking a derived class object into a baseclass pointer (or reference variable), and we forget to give virtual keyword for base class destructor. See virtual destructor for details.
8. Can we have virtual destructors? If so what is the use of virtual destructors.
Ans. Yes, we can. This is to make sure that the correct class destructor is called at run time. Specifically when we use base class pointer or reference to hold the derived class object. If we don’t have virtual destructor, then it will end up in calling only base class destructor.
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- Playing with Destructors in C++
- Destructors in C++
- C++ Internals | Default Constructors | Set 1
- When are Constructors Called?
- When does compiler create default and copy constructors in C++?
- Output of C++ programs | Set 26 (Constructors)
- How to initialize Array of objects with parameterized constructors in C++
- Move Constructors in C++ with Examples
- Constructors in C++
- Commonly Asked C++ Interview Questions | Set 1
- Commonly Asked OOP Interview Questions | Set 1
- Commonly Asked C++ Interview Questions | Set 2
- C++ interview questions on virtual function and abstract class
- Range-based for loop in C++
- Policy based data structures in g++
- Different types of range-based for loop iterators in C++
- Reversed Range-based for loop in C++ with Examples
- Cvent Interview Experience (On campus for Internship and Full Time)
- Amadeus Labs Bangalore Interview experience
- Intel Graphics Software Engineer Interview Experience
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.