 Hello friends, I am Sanjay Gupta. In this video, I am going to demonstrate you how you can implement the concept of virtual descriptor in C++. Before starting, you can note my information. You can follow or subscribe my YouTube channel that is Sanjay Gupta underscore tech school. You can download my Android app that is Techimace, which is available on Google Play. Now coming on to the topic for demonstration, I have implemented a code in C++. Here you can see IO stream header file is included. Then S3 name space is used. Then class A is implemented here. X is a attribute. Then in public section, first I have implemented a constructor, which will display constructor of class A called message on console. Then its destructor is implemented, which will display destructor of class A called this message on console. Then read function, which is reading a value inside X variable and then show function, which will display value of X on console. This is the implementation of class A. After that class A is inherited into class B. Class B is having an attribute that is why inside is its public section. Its default constructor is implemented, which will display the message constructor of class B called on console. Then its destructor is implemented, which will display the message destructor of class B called. Then its read and show functions are implemented. Then class B is terminated. Then in main function, a pointer of class A is created. This is pointer of base class because A is the base class. Then object of derived class is created. So you can see the comment pointer is of class A and object is of class B. So here with this new B, an object of class B is created. So it will call constructor of class B and then constructor of class A will be called. So this will print both the messages of class A constructor and class B constructor. Here base class is A, so base class constructor always executes first. So it will display constructor of class A called message on console and then constructor of class B message on console. Then its address will be assigned into P1. Now through P1, read function, show function are called. So at compilation time, this calling will be linked with base class functions because P1 is belonging to base class pointer. When this program will be executed, then base class functions will be executed because the concept of virtual function is not used till now. Then delete P1. It will delete P1 pointer and P1 pointer is storing address of B class object. So it will destroy the allocated memory. So here again pointer is of base type. So it will call destructor of base class only. It will not call destructor of derived class. Now I am executing this code so that we can see what is happening after execution. You can see and match the output. Constructor of both the classes are called. Now it is asking for a value. I am entering 3. You can see the output x equals to 3 is displayed. So these read and print functions of base classes are called and destructor of only base class is called. That is A. So I hope you have understood with this P1 pointer all the functions and destructors of base classes are working. So this is also known as static binding. Static binding means the function call is linked with base class functions at calling time, sorry, compilation time and at execution time those linked functions are executed without checking the address which is available inside the pointer. So now I am going to implement the concept of virtual with destructor and function. So I have implemented this virtual destructor and both the functions are implemented as virtual. So this time whenever P1 is used to calling these read and show function and for deletion of this P1 address which is available inside this P1 pointer. So every time virtual will be checking what address is assigned into this P1 pointer. So when P1 arrow read function will be called, virtual will be checking which address is stored inside P1 pointer. So it is containing address of B class object. So control of read function will be transferred to drive class at execution time. So at compilation time read and show of base class will be linked but at execution time control will be transferred to drive class functions. Similarly when delete P1 will be executed. So P1 is containing address of B class. So compilation time it will be linked with base class destructor but their destructor is implemented as virtual destructor. So virtual will be checking which class address is available in this pointer. So it is containing B class objects address. So first control will be transferred to drive class destructor. So drive class destructor will be executed first and then base class destructor will be executed. So now again I am executing this code. You can see it is printing both the constructor messages. I am again entering 3. Now you can see y equals to 3 is printing. So read and print of drive classes are worked here and then destructor of class B called messages printing and destructor of class A messages printing. So this way I have implemented a virtual destructor in this example and also virtual functions. So virtual always checks the address which is stored inside the pointer so that proper functions and destructors are called. So this is all about the use of virtual destructor in C++. I hope you have understood how this concept is implemented in C++. If you want to watch more programming related videos you can subscribe or follow my YouTube channel that is sanjaygupta underscore tech school. Thank you for watching this video.