 Okay, by now we understand that a class is a blueprint and that we can create instances of that blueprint. Let's go ahead and make a new one. We'll call this laptop and we're gonna discuss what are called constructors and destructors and look a little bit at what happens under the hood when a class is created and destroyed memory here. All right, so we've got our class here and we're gonna go ahead and flesh this out a little bit. Say include, we want Qtabug. And inside of Qtabug, let's go ahead and we're going to just add a comment here. This is our constructor. This is a function that gets called automatically when the class is created. If you've taken a C++ class, you know there's such a concept as a default constructor. We're not using the default constructor because we're inheriting QObject and this is the constructor for QObject. If we switch to the source view, you can see that we have a QObject parent and it's just a pointer to a QObject. Now a pointer, remember a pointer is just a location in memory. So that object exists somewhere out there in memory. We're just giving it that location. This is part of memory management and we're really gonna dive into this into the future but just understand that there is a parent child hierarchy here going on. So when the parent's destroyed, the child's automatically destroyed with it. Let's go back here and we're gonna make what's called a deconstructor. A deconstructor takes a special syntax which may look a little bit weird. You have what's called a tilde. If you don't know what that key is, it's on the upper left hand part of your keyboard usually in front of the one key and it's the class name with the ending parameters or ending parentheses here. And then we're just going to refractor and add definition and here's our deconstructor. So this is called when created and this is called when destroyed. Hence constructor deconstructor. So why would you need these things? Well, it's actually pretty simple. You need them so that you know the lifespan of your object in memory. Let's say for example, you're opening like a TCP socket to a server or a file or a database connection. You may wanna actually open that up when it's created and if your class is destroyed, you're gonna wanna free those resources up. Remember, C++ is very powerful but at the same time very stupid in the sense that you have to tell it exactly what you want it to do. All right, let's go back into our header here and let's just add some things in. We're gonna say int weight, key string, name. We're just making some variables. We're gonna make a function there and that is a function prototype meaning we're going to actually implement that in our class file and we're gonna say void test. We're gonna make another function prototype here and you remember the reason why you would make a function prototype instead of doing this in the header is so that you could actually segregate this thing off into a library or a shared object or DLL, whatever it's called in your system and you would hand the in developer your header file, just this and they would never ever see the implementation. All right, so let's look at as kilograms and test. We're gonna add some functionality into here. I'm gonna say return this dot weight. Notice I said dot but it makes this little arrow. Let's revisit that. When I hit dot on the keyboard, bang, it automatically makes that arrow. So what's the difference between this dot and this arrow? Well, it has to do with whether or not it's a pointer. And I'll show you here in just a second. Let me finish this out. We're gonna times that by 0.45, 35, 92. No, I'm not a genius. I have that written in my notes. So this dot and we want weight. Because it's a pointer, this is the word this. It points to, hence the arrow points to a location. We're pointing to the weight variable. We know this is a pointer. How do we know this? Because this is intrinsically part of every class. There's an invisible variable somewhere right here that looks something like this. So really what it's saying is take this class, make a pointer, call the variable this. And it literally points to this object, which is why it's called this. Very confusing, but when you talk about the concept of this, know that it is created right here. Next, let me put that in quotes here. This is automatically created for us. So we can say things like this dot name and then set it to something. Remember, you can create multiple instances of this blueprint. So this is the current instance you're working on. That'll be readily apparent here in just a minute, but let me finish this up real quick. We're gonna say qinfo this and name. Name and we want as kilograms. So really all we're doing for our test function here is we're saying qinfo. So we're gonna send some information out on the screen. We're gonna say this, remember this is a pointer. So it'll give us the memory location, the name variable. And then we're gonna return a double from as kilograms. Let's go ahead and go in here and let's change our constructor around a little bit. What we wanna do here is be able to name these laptops. So we're gonna say qstring name. Whoops. And you see this little light bulb. You can click or you can press alt return, but if you click this applies changes to definition. In other words, it automatically modifies it for you so you don't have to remember to type that. Now we can say this dot name equal name. Notice that's a bit confusing. We have name and name. This dot name is actually declared up in the header here. Ta-da, right here. However, name for the function is different. There are two different variables. And you determine that by saying this dot name. If you did something like this, notice how the color changed. And well, it's a gamble, which one's actually gonna change. So when in doubt and you have a naming conflict, always use this. Now that we have this in there, let's go ahead and just put in some info so we can see what's going on. We're just gonna say this constructed and let's get this in here. That was a little bit confusing. Let's get this in here, all right. So we're just gonna pump out when it's constructed and when it's deconstructed, we have as kilograms and a test function in here. Let's actually use this bad boy. So we're gonna use our laptop header and let's make a laptop. I'm gonna call this one mine. And this is how we access our constructor. We just use the parentheses. You can see we have a pointer to a queue object, which is a null pointer, which means there's nothing there. Sounds a little confusing. And what we can do is just enter zero, meaning there's nothing and let's give it a name. Now you may be saying, now wait a minute, zero and nothing are two different things from our last couple videos. Well, that's actually true. A null pointer when it's set to zero means there's just nothing there. Null pointer and zero are pretty much the same thing because a pointer is a memory location. So if you say zero, that means there's no location in memory. It's a bit confusing, but it's one of the caveats you need to understand. So this is a laptop, yours. And we can do the same thing. We just say null pointer. And to do that, we would just say the keyword null, which is gonna get translated for us under the hood to a zero. Doing that for illustrative purposes, you may see it both ways. And I don't want you to get confused between zero and null when it comes to pointers. They're basically the same thing. And it's gonna say zero as a null pointer constant. It's kind of gonna give you a warning there. Now the reason why we're setting the parent to zero is because the main function doesn't really have anything going on for us here. Meaning, well, this is not a Q object, this is just a function. What we should be able to do is gives us a reference to A. That way, when this is deleted, actually I should say when this is deleted, this will automatically get deleted as well. Let's go ahead and do that. No, it's not really needed because we're doing what's called an operation on the stack. And we're gonna get into memory management in a future section, but this is all on the stack, quote unquote, just burn that into memory, which means it's gonna get deleted automatically. So we could definitely just do this and it would work just fine. But we would just basically wanna get rid of this annoying warning here. So we're gonna say, now if you're wondering what type of object, you notice how we used a QCore application, can we reference into this? Well, anything that is a subclass or inherits Q object. And because we know that the class, the QCore application is a Q object class, see, inherits Q object, we can use that. That's why Q object is so fundamental to understand because almost everything inherits off Q object. You can do some amazing things with it. So we're gonna say mine, whoops, mine.wait. And I want my laptop to be nice and light because I travel yours. And let's see yours is a bit heavier because you will, you like to do online gaming or something. I like to do online gaming, don't get me wrong, but you're gonna have heavier laptop. Save that, run it, let's make sure nothing goes boom. And you can see laptop, my laptop was constructed, laptop, your laptop was constructed. Notice they are two different memory addresses. You can just take the last few numbers and see they are different. We have 760 and 780. So they are in different memory locations. Close that out. One thing you might not have noticed, unless we do that, is there was no deconstructor message. See, I stopped the application, but it didn't say deconstructed. Well, that's because we're following the flow of the QCore application, meaning the whole application stopped. So there actually is messages out here you wouldn't see. Let's go ahead and let's actually fix that for demonstrative purposes. Let's put this here and let's move this up here like this. Notice how suddenly it can't do that because we don't have this. So let's just say this, move that around and let's go ahead and call this. If you're getting a little confused between this star and this ampersand and which one's which, remember that when you see the star, it's a pointer too. Think of it just like somebody standing in traffic pointing you which direction you need to go. When you see this ampersand, that's a reference too, meaning it's somebody telling you, hey, go to this location. In other words, think of this actually returns the pointer. All right, so we're gonna go here and from here, let's go ahead and add in some functionality. Let's make another one. So we've got a laptop in here and we're gonna say machine.test. Things seem pretty simple. Let's go ahead and add this and we're gonna say mine. There's a couple of issues going on here that we really need to understand fundamentally. Let's go ahead and run this and see what happens. You can see now we can see the full life cycle here. We can see, and we're just gonna follow my laptop here is constructed, my laptop test, and then my laptop deconstructed. So that object no longer exists out memory. And because we've set the parent right here, we've set A and then given a reference to A. If it were anywhere other than the stack and we killed the application, it would automatically get deleted for us. We wouldn't have to worry about memory management. That's a pretty awesome feature of Qt, by the way. If that was a bit confusing, don't worry, we're gonna talk about memory management in future videos. But there's something else lurking here you should really understand. Look at this test function right here. We have a laptop and we're passing that by reference, not by value. What happens if we pass it by value? You can see, uh-oh, suddenly, call to implicit delete copy constructor of laptop. What in the world is going on here? Well, its Q object has been explicitly marked deleted here. Note passing an argument to the parameter machine. Basically what we're saying here is this object no longer exists because the stack will delete it. So your application comes in and it says, all right, run the main, run make laptops, starts here, does this, does this, does this, does this, goes to do that. And what it's doing is it's saying take this, send it here by value and do this. So basically by value would try to make another copy, which is a big no-no with QObject. QObjects don't like to be copied. So what we're gonna do here is just add that in by reference and then suddenly everything works as expected. So let's try to run this and see what would happen. As you guessed, we get a big fat crash, meaning the compiler's not even gonna let it happen. And we get this very hard to decipher thing that says QObject has been explicitly marked deleted here and we're trying to figure out what's going on and if you click on it, you'll get this, which is not helpful at all. But you can see right here, QDisableCopy. What that means is you cannot copy a QObject. Let's just scroll all the way up here. You can see there's a lot going on. All of this is what really makes QObject work under the hood. And because you can't copy a QObject, you avoid a lot of common pitfalls. That's one of the powerful features of Q. This QDisableCopy will save you a ton of headaches. It may seem like kind of a pain that you have to remember to do this with your code, but you're gonna avoid a lot of memory management issues in the future. Let's go ahead and fix this and let's run it. Everything works as expected. I hope you enjoyed the video you just watched. That was a preview of one of the videos out in the Qt Core for Beginners with C++ course I have out on Udemy. I am going to be making an intermediate in advance and then we're gonna start working on GUI technologies. For example, showing buttons, lists, tree views, you name it, and then moving on to things like QML. The reason why I've restarted this whole thing is if you're watching these videos out on YouTube, they are a little old. This one was done in 2011 and the technology's changed over time. So because the technology's changed, some of these videos, as good as they are, really don't line up with the current cute technology stack anymore. So I wanted to start from scratch. I hope to see you out on Udemy and also in the Voidrums Facebook group. See you there.