 Hello friends, I am Sanjay Gupta. In this video I am going to demonstrate you the order of constructor and destructor execution in C++. Before starting, you can note my information, you can connect with me by following these details. You can download my app Techimage that is available on Google Play. It contains various programming related material. Now coming on to the topic. In this video, order of constructor and destructor execution is explained in C++. First, I have included a header file IoStream, then stdinamespace is used. Name of class is test. Inside this class, a constructor is implemented which will print constructor called message on console. Then a destructor is implemented which will print destructor called message on console. Constructor will be called when an object is created. Destructor will be called when an object goes out of scope. So whenever the function who is creating an object is terminating, then automatically destructor will be called. Then I have terminated the class. So it is a dummy class which is containing only default constructor and destructor. It is not containing any attributes or any reading, printing or any calculation related functions. So I have implemented this class for only understanding purpose. Now you can see I have created a global object. It is created outside any function. So in case of programming global variables occupies memory first. So this object will be a calling constructor at the very first. Then a function f is implemented. And then main function is implemented. So f function is not the part of class. It is an independent function similar to main, but main is called automatically by the operating system, whereas we have to call this f function. So this function is called here inside main function. So f function is called inside main function and main will be called automatically. So now we have to understand what will be the output of this program. So first global objects constructor will be called. Then control will be transferred to main. So again, x object will be created. So its constructor will be called, which will display constructor called. So for object g and for object x, two constructor messages will be displayed. Then in main, this message will be displayed. Then f function will be called. Control will be transferred inside f definition. So here object local will be created. So again, constructor called message will be displayed. Then terminating f function, this message will be displayed. Now function f is terminating. So I have already explained that any object, which is defined inside a function and if function is terminating, then object also goes out of scope. So this local object will be called, sorry, this local object will call destructor to destroy this object. So destructor called message will be displayed by destroying this local object after termination of f function. Then control will be transferred to main again. So it will display terminating main. Now scope of main is terminating. That's why x will be calling its destructor. So again, destructor called message will be displayed and at last scope of global object will be finished. So it will be calling its destructor. So now we have to see the output of this program. You can see the output. Now I'm again going to explain the output with the help of this code. So first object g is created. So constructor called is printed. Then test x object is created. So again, constructor called is printed. Then see out in main. This is printed. Now f function is called. So object local is created. So again, at four, fourth line constructor called is printed. Then after that terminating f function is displayed on console. Now local object is destroying. So it's destructor called message is printed after terminating f function. You can see it here. Then control is again transferred to main. So main is printing terminating main. You can see the message terminating main. Then twice destructor called message is printed one for x and one for g. So friends, this is the order of constructor and destructor calling. And I hope you have understood how a destructor is calling as function is finished and you can say object of sorry scope of an object is finished. So I have created two local function f and main. Main is called automatically by operating system, whereas f is called by main function and classes containing only constructor and destructor. So I hope you have understood the working of constructor and destructor. If you want to watch more programming related videos, you can subscribe my YouTube channel. Thank you for watching this video.