 Hey everybody, it's Brian and this is our 23rd video on C++ programming. The question was raised, why would you create a class using a pointer? That's a very good question. I kind of want to cover this because it's something that I actually wrestled with for a while. You see we've got just a simple one file template here with the cat class and our main function. The cat class just has a public variable called age. What we're going to do here is we're going to say cat and we'll call it mcat. Equal new cat and then we're going to say mcat age and we'll say the age is zero. We haven't really set it yet. Then we are going to just see out mcat age and L. Now why would you want to do any of this? Let's delete this real quick before we forget and have a memory leak program this small. It won't really make a difference but it's always good form. We're going to make a function called do set age and we're going to say it takes a pointer to the cat object. Actually, we should probably use the correct notation p is shorthand for pointer to. We're just going to say pcat age and this is where we'll actually set the correct age. Let's actually just give it another parameter here. We'll say mh. Now when we call this function we have to give it two parameters. We'll say do age and we're going to give it the pointer to the cat object we created. Let's say we want to set this cat to 12 years old. Let's just do another c out so we can see the values actually changing. Let's review real quick before we run this. We're going to create a new cat object out on the heap or the free store. We're going to set the age to zero. We're just going to print out that we've set it to zero. Then we're going to call this do set age with a pointer to the cat class and the age of 12. Then we're just going to print out that we have set the age and then delete the object from memory because we don't need it. This looks simple but it's actually very complex what's going on under the hood here. The main function that we're currently in, this is the scope of execution. Meaning this is the current stack. Once it jumps into do set age, this is the scope of execution or more to the point. This is the stack and then it jumps back out and executes this code. Really what you could say is this being broken up into three parts. This is being executed. This is being executed. Then this is being executed. The problem with that is once you're in the do set age scope, you have no way of grabbing these variables. For example, if you take this MCAT that we've previously created and try doing something, you can't access it. Let's say age equal 99. Try running this and see what happens here. It's going to explode and it's going to say MCAT undeclared identifier. You don't know what that is because you only have PCAT and MH. These are the only variables allowed in this scope because we don't have any references or pointers to anything else. You basically create your object out on the free store, out on the heap. Then once the execution scope jumps into the do set age, you can still grab it using the pointer. That's what we're doing here is we're saying PCAT MH or PCAT age equals MH. What we're saying is grab that pointer out in memory and use it. For that, let's actually do one more little thing here. Let's just say at and let's just copy this and we'll say set at pH. I want to show you that this is the same memory address. That way there's really no confusion about what's going on after this tutorial because this threw me off for a long time when I was learning this. Let's actually get rid of that real quick here. That way we can see what's going on. Run this. We have some error messages. MCAT undeclared identifier. This goes right back to what we were just talking about. Here's our program. You see it starts with zero and it creates the cat object at this memory address. This is 00344FD8 and then it sets it to 12 and it's at. Now this is the do set cat right here. Remember we said set at. Notice it's the same memory address and then we jump back into the main function and notice it's the same address. That cat object is out in memory somewhere and no matter what scope we're in we can still grab and use it. Now I know some of you are sitting back knowing, wait a minute I remember we learned this and you can pass it by value or by reference. Why don't you just pass everything by reference? The problem with that comes in. Sometimes your scope of execution gets so out there that that doesn't even work and then you have to use pointers. Under the hood when you pass it by reference you are passing a pointer. You just don't see the pointer and you don't have to play around with it. Is there a correct way and a non-correct way? No, they're both tools. Use the right tool for the job. We could have very easily used a pass by reference here. Remember using the ampersand, pass by reference. The ampersand I should note simply means address of. So you are passing the address of that cat variable. Or in other words you're passing it a pointer. This is Brian. I hope you found this video educational and entertaining and thank you for watching.