Destructor in C++ | C++ Tutorial | Mr. Kishore

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi welcome to nourish oddity this is Kishore and now I am going to discuss about destructors okay improvisations we have disgusted what is a constructor and how this one is working and I have completed that what is a default constructor parameter is constructor and copy constructor now I am going to discuss about destructors now what is a destructor okay to know about the destructor first of all we have to learn about constructor constructor is used for what constructing the object data means what every object is having data members data members means for nothing but generally variables and variables are going to storing stack or heap based on the declaration type now the point is it is stack or heap okay they are nothing but memory areas that means they need some memory that's why whenever an object is created it is going to occupy some memory that means whenever a constructor is executed object is defined that is what memory is allocated now to release this memory are to delete this memory we are in the destructor concept thus why constructors construct slow memory constructor construct the memory or constructor construct the object data and destructor is used to delete this memory created by the construct that's why a destructor is a function it is also one of the function okay special member function which is easy to delete the memory created by the constructor that's what it is called destructor generally defector means one destroy this time is for deletion the side it is a good practice to use the structures in our program because of it is automatically releasing the memory occupied by the constructor next what is what are the rules for destructors what are the rules destructors now the first rule for destructor is okay the major rule for this sector is the destructor name should be equivalent to the class name it is the first common rule already we have discuss it in constructor the constructor name should be equivalent or similar to the class name and here also the destructor name should be similar to the class name okay and followed by are preceded by tilde operator here the name should be similar to the class name preceded by tilde operator then only your compiler understands it is a destructor for example sub class sample now it is the sample class and here sample now something is there okay here see this anything symbol is there no now class name sample function name sample that's why it is called constructor okay now it is called constructor now what is destructor destructor name also similar to the class name that is why sample okay now it is looking to be a constructor but I want to declare a destructor that is why you should be preceded with the tilde operator that's all now this one is called destructor okay that's why the major difference between constructor and destructor is what constructor never contains the tilde operator but every destructor should have to start with the tilde operator it is the most major difference between constructor a mistake next here another important rule okay another important rule is what it should be declared in public area already we have discussed constructor also declared in public here and destructor also declared a public area otherwise it is not invoked code okay that's what the structure should be declared in public area it is a another next another major important thing constructor is having arguments constructor may or may not have the argument that is why hear arguments are maybe army not optional but hear arguments never it is a another important tool instructor may or may not have the arguments but destructor never contains any arguments okay due to this what happens suppose this is a constructor now can I send one argument ok yes in 2nd of second constructor I can send two organs in third one three argument like this that's why it is possible to define several constructors which is called constructor overloading which is called constructor overloading for example here only one argument I want to send sample of a another constructor sample of a comma B now watch it here it is constructed with one argument it is constructed with to earn but here the problem is destructor never contains the argument that is why it is not possible to overload the destructors okay it is another important rule now destructors never participate in overloading next another important thing as usual constructor never contains the return data type or return value as usual destruct also never contains never contains or never returns a return value the same rule is applied for both constructor and destructor clear Desa here it is another important and it is also make a Blissett call to the new and delete operator it is also calls automatically for the new and delete operators okay these are the rules and regulations for destructors okay it is how to define a destructor in our program okay now I will show you how to create a destructor and constructor in a problem and here one more thing be also not participated in inheritance okay all reconstructors never participate in inheritance and here also destructor never parts fit directly in inheritance and they should not be called with the object name normal member functions are called with what object name followed by dot operator and destructor never called with the object name followed by dot operators okay so now I will show you how to work with the destructor first a common hydrofoils hash include iostream dot H next hash include con euro - now class sample okay here I want to declare data members that's why int a comma B - data members and the rule for constructor and destructor they should be declared in public that is why first sample now what it is it is a constructed okay now here a equal to for example 10 B equal to for example 20 now here a initialize it with it'll be initialize it with the 20 next now I am going to write the destructor sample and it is called destructor and here I want to bring a value C out a equal to a and L and here C out B equal to the end l no class completed this off here what it is constructor which construct the object data and here it is going to print that means I want to print this a and a B data using the restrictor now watch is it is the main function it is the main function first of all I want to create a object sample yes now s is the what object when object is created what happens constructor is in vocal means what this constructor is activated when constructor is in vocal a value becomes 10 B value becomes 20 that's why here in stack now the memory allocated for s and a value becomes 10 B value becomes 20 and totally they are going to equate two into two bytes means for voice si here totally a and B are integers now the sides are going to occupy 2 into 2 by 16 bit compiler in a 16 bit compiler the search is going to take four words okay now when the object is created automatically object it reinitialize it which is called automatic initialization means without calling the function without calling any function the members are initializing the so it is called automatic initial edge next now the point is okay I want to erase this data I want to erase this data but here for erasing this data okay there is no special function I want to use the destructor next when the destructor is executed when the object is deleted that's right now the destructor is going to delete the object data object means what a and B values are going to be deletion how this one is possible made here just close the bracket okay everybody knows when a bracket is closer to what happens the local variables are going to delete from memory here local variable means what if because of SE the object object meaning is what it is a variable of type class okay s is a object of type class that's why SC is what variable that means what now it is declared inside the main function when it is declared inside a function or a block what it is called local variable now when local variable lifetime our availability is what until the block is executed means now the block is executed now automatically this object is deleted when it is deleted okay here what it is destructor destructor is having what code a value B value now it is going to show the data okay that means what the object is going to be deleted okay here the object values are going to be deleted before going to delete the data it is going to print the a and B that is why here it is better to use get CH here okay now get CH now what happens when this bracket is closer it is going to be executed in a bus surface Z value printed let you print it later program stopped that is why on the screen you are having a value 10 B value 20 here watch it only object is created when object is created automatically constructor is executed when object is deleted means destructor of thing that is why here there is no need of function callings it is how the destructor is working that is why constructor construct the object data and destructor districts the object means it is especially designed to delete the memory allocated by the constructor that means how juice that's why destructor is a special number function this factor is a special member function whose name is similar to the class name for preceded by tilde operator and it is used to destroy the memory allocated by the constructor means it is easy to delete the object created by the constructors it is how to work with the destructor in our programming okay thank you for watching in next session we are going for another examples thank you [Music]
Info
Channel: Naresh i Technologies
Views: 71,866
Rating: undefined out of 5
Keywords: Destructor in C++, Destructor, C++, CPP, C++ Tutorial, Kishore, Naresh IT
Id: FDnvm5QAo1A
Channel Id: undefined
Length: 13min 26sec (806 seconds)
Published: Wed Apr 12 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.