C++ Interview questions based on constructors/ Destructors.

1. What is destructor?
Ans. Destructor is a member function which is called when an object is deleted/destroyed or goes out of scope.

filter_none

edit
close

play_arrow

link
brightness_4
code

class String {
private:
    char* s;
    int size;
  
public:
    String(char*); // constructor
    ~String(); // destructor
};

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate destructors.
class Geeks {
private:
  
    // a private-access pointer to integer
    int* myPrvIntPtr; 
public:
    Geeks()
    {
        // default constructor
        myPrvIntPtr = new int(0);
  
        // allocate a new integer, place its address in myPrvIntPtr
    }
  
    ~Geeks()
    {
        // de-allocate the integer whose address 
        // is stored in myPrvIntPtr
        delete myPrvIntPtr;       
    }
};

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program without virtual destructor
// causing undefined behavior
#include <iostream>
  
using namespace std;
  
class base {
public:
    base()
    {
        cout << "Constructing base \n";
    }
    ~base()
    {
        cout << "Destructing base \n";
    }
};
  
class derived : public base {
public:
    derived()
    {
        cout << "Constructing derived \n";
    }
    ~derived()
    {
        cout << "Destructing derived \n";
    }
};
  
int main(void)
{
    derived* d = new derived();
    base* b = d;
    delete b;
    getchar();
    return 0;
}

chevron_right




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.