 Hey everybody it's Brian and this is our 21st video on C++. Today we're going to be continuing our discussion on classes and I'm going to show you how to build classes in external files. Now you notice off to the right here we have these little folders like header files, resource files, source files. I'm going to show you how to use those and this is pretty standard as C++ not just Visual Studio. So you can add a new class and let's see C++ class. Now you want the name of your class a .h and a .cpp. What are these? .h is a header file. .cpp is a C++ code file. So let's just say cat. You notice how in Visual Studio it automatically fills it in. Your editor may vary and let's just hit finish and you get two files cat.h cat.cpp. What are the difference between these two? Well the header the h file this has our class in it. Remember it's class with the name code block semicolon. In case you're wondering what this pragma once is that is Microsoft's version of if and if you know if not defined. And then we've got cat.cpp. Notice how it includes cat.h. We've been over the include preprocessor directive before so what we're saying is include cat.h. So it's literally taking this file and putting it like that. Now you understand what that looks like in memory. Now what's cat colon colon that's a namespace. You're actually accessing the cat namespace. These are called constructors and these are called destructors. What's the difference here? Actually let me get rid of that. Let's just focus on these. The constructor is called automatically when your class is created. The destructor is called when the class is destroyed. So let's just say c out and down and some of you have already probably screaming at your monitor going Brian Brian you're forgetting the standard library namespace. You're absolutely correct. We'll get to that in just a second here. Right now we've got our namespace. We've got our implementation file. We've got our header file. Now why do you split these up? That's a common question. Why do you need an h file and a cpp file? Well the reason is actually pretty simple. If you've worked with languages like C sharp or java or even visual basic or python you know that your source files can get just massive and I mean just big to the point they're unmanageable. Well what C plus plus and outright C do is you have these header files which is where you make your your definition of your class and then you have your code file which is where you put your implementation. That way instead of having one file you've got a hunt peck for and search through and all those other garbage you got one file you really have to worry about as your template and one file is your implementation and it gets a little deeper than that. When you end up building like dls and things of that nature you'll actually distribute the header file but they won't get the code file because it's compiled in a binary. That's how and why that works. Now to use that over here let's actually say include cat.h and notice it's the same syntax. No black magic going on here and now we say cat and cat and that's all we're going to do for this one. I want to run this and show you what happens. It compiles and bills and it says creating cat. Now where did that come from? Remember we said we've got a constructor here that's called automatically when the class is created and destructor that's called automatically when the class is destroyed. Now some of you out there may be tempted to do something like this. Take this throughout the destructor and say well I want to see when this thing is destroyed and run it and there's no call for the destructor. Why is that? Well if you had a very sharp eye you'll see what's going on here. It created another one. Why is that? Because you're trying to access this object object while it's being destroyed. Don't do that. Destructors actually in the real world are used to release resources. For example if you have a handle to a file or say a TCP socket something of that nature. Alright back to our original example here. Let's compile it run it. Notice how it says creating cat and then it says press any key to continue. What you're witnessing here is the creation and destruction of this object in total. I mean it's the entire life cycle right there. Now let's actually go in and add a few things. Let's just stick with the example we had last time int age and then let's create a getter and a setter. So we will say int get age. Now this is how we would have done it but instead we're just going to go back and enter a function prototype here. Now why are we doing that? You'll see here in just a second. Bear with me. Now remember from our discussion of function prototypes you are giving the prototype of the function but not the implementation. You go into your implementation file and you'll say cat colon colon and then get age. Now when you do this you're forgetting a step aren't you? You have to give it the return type. That is how that works. See if you forget the int you're actually just calling a constructor for something that doesn't exist. So you say int and then void cat colon colon set age and there's your implementation of your functions. Let's actually just run this real quick. Compile errors must return a value. Yes we totally forgot. Save our work compile it and sure enough it compiles. It doesn't do a whole lot because we haven't modified the center. Let's actually modify this. Now the thing about function prototypes is you don't have to declare a name. You can just give it a type and say int and then you can come back in here and say int m age and say age equal m age and it still compiles. Now why don't you have to include that? The reason is simple. This is a prototype. The compiler really doesn't care and you notice how it says void up here. You might have caught that earlier. Void just means that we're giving it nothing. It's the same thing as this right here. So I'm trying to walk you through these things fairly quickly without confusing you. I hope you're keeping up let's say and then we can set age and we'll say 10 and then c out cat.get age. All right now let's run this and sure enough it says creating cat in 10. Now if that wasn't enough I want to show you how to work with pointers at the same time. So let's actually there's our first cat. Let's create cat. Whoops pointer and we'll call it z cat equal new cat and remember to delete our reference to our cat here. Reference I just said reference sorry our pointer and let's go c out z cat. Now when you hit dot nothing happens why is that? Pointers take a special kind of access modifier where you have to point to it there's this dash greater than it looks like a little arrow where you're pointing to it and then you say get age. Now seems kind of cumbersome but you'll get used to it. The reason why they do that is because they really want you to understand how you're interfacing with the memory. This is on the stack. This is on the heap. You can tell instantly by the way you're accessing the properties of this class but once you run it it does exactly the same thing if we actually set this here. Let's set age and we'll say z cat is two. Let's run this. Uh oh build errors. Illegal indirection. What is wrong here? Yes and there's our two. Now you may be wondering why is that an illegal indirection? Well simple. We were saying point or two this address and then this. Well when you do that that's called an indirection meaning it's saying grab this memory address and then indirect to this part of it so it's kind of doing it for you. Trying to think if there's anything else really quick here we should probably cover um let's actually see what happens if z cat the age is not set. Notice how we're not setting the age in the template. Let's run this and find out what happens real quick here. Notice how it gives us some really wacky number because we haven't initialized that. Well that'll be the topic for another tutorial but I just want you to be aware you should always initialize your variables. This is Brian. Thank you for watching. I hope you found this video educational and entertaining and see you next time.