Hello, and welcome to the 57th tutorial on the c++ programming series. And in this problem, we're looking at the class destructor. A class destructor is caught at the end of an object's lifetime. It is essentially the opposite of a constructor in that it is mainly used for cleaning up instead of setting up a object. For example, when you allocate memory to a pointer, it should be cleaned up when no longer needed on a regular variable that will be cleaned up No. When no longer needed itself, the application will do it itself, which is great boxey points are a lot more powerful, hence why you use them but you do have to make sure you clean up the memory allocated memory explicitly.
So let's show you how to use a class destructor. So what we're gonna do, is a class vehicle We are going to be explicitly calling the clap destructor. But at the end of an object's lifetime make a maybe it's declare within declaring the main one once it hits return zero and has this curly brace anyone no longer be needed. So we cleaned up itself, but like I said, just to demonstrate it, we're just going to be explicitly using it. So we're going to create a constructor called vehicle. Critical.
The destructor is the same name as the constructor or the class. But the only difference is you have this little tool, this little squiggly line, then you put the class name which is the vehicle now control bracket. I'm going to create a pointer woman to implement the constructor for vehicle colon colon vehicle. And here all we're going to do is point China equal new into so we just allocated some memory and then in destructor for for the D source at vehicle colon colon, towed vehicle open closed bracket and in here to was deleted pointers and the Delete pointer and I simply what we're going to do is create an object of vehicles call it BMW do a strip BMW pointer equals 56 STD c out strip down the pointer line and if you run this it prints at 56 which is great bohlander after this is called the disruptors of BMW dot tilde vehicle, open bracket semicolon and I'm simply going to copy and paste this book was printed after we call it a destructor.
So you can see what happens if we do One app. And obviously it crashes because the memory had been deleted. But we're still trying to access it. So if we just comment this out it's moaning to the pointer in condition two equals No. Yeah, that's right, definitely have to sign it no first. So let's add this line back in.
And we get an error here simply because we have obviously called the destructor. So BMW no longer exists, you can think of it that way. And obviously the pointer no longer this. That's it for this part of the series. This policy, things to take away destructor use cleanup destructors call itself at the end of an object's life, but you can call it yourself. If you want to Delete memory when no longer needed and the destructor is a great place to do it unless you want to do it somewhere else and you're aware of what you're doing so just be careful you have any questions feel free to message us at support at sama systems calm okay?
The email will be in the description. You can comment on this video directly messages via YouTube, or the require link for source code will also be in the description. In the next part of this series, we'll be looking at the class copyist copy constructor, and as usual, thanks for watching and I hope you have a nice day.