 Hello everybody, I'm gonna start a new tutorial series learning you guys C++. Why C++ and not any other programming language? Because C++ is an industry standard when it comes to making games, and I eventually want to make tutorials to teach you how to make games, like 2D games, maybe even 3D games if we get that far, but that's still a long way ahead. And this tutorial series is aimed for people that don't know anything about programming yet, but really want to get into making games. Since that's what I'm here to teach you. First of all, we're gonna need something that we can write C++ in, and so we need some sort of editor. The editor that I've chosen, that's Visual Studio. Why Visual Studio? What is Visual Studio? Visual Studio is a Microsoft app that is a combination of an editor, a compiler, a linker, and a debugger. Basically a compiler that transforms your code into an EXE, so you can run it. It basically transforms what you've written into something that a computer can read. Then you have linker. What a linker does is if you have external libraries, so you're trying to make some sort of 2D game and you need code that somebody else has written for you, since you didn't want to make all the rendering code yourself, then it somehow needs to link that code to your code, since it's something that's not in this. Yeah, it's not a project itself. So then you have a linker that links everything together so it can build it correctly. And you have a debugger, which is really really important, so you're gonna do a lot of debugging. A lot of debugging. That's one of the biggest things that you're gonna do. But that basically makes it so you can step through the code, you can watch how things are happening while they're happening, and it's just a really really handy tool to have. Okay, now the installation of Visual Studio. We're just gonna go to Google and we're gonna search for Visual Studio. Okay, Visual Studio, we're gonna go to the Visual Studio website and then we're gonna download the free version of Visual Studio, which is the Community Edition. So we're just gonna press download community 2015. If you're watching this later and it's a different version of the Community Edition, feel free to download that one, just get the latest one. And like the coding is gonna be the same. Maybe the UI is a little bit different, but I'll probably still be around to help you out if you need me. If you ever have any questions, let's get it out of my face. If you ever have any questions about C++ or Visual Studio or anything, I can help you. If you ever want to like get a bit further, like ahead of the tutorial, I can give you some, I can like advise you to do certain things. I'm here to help you. Okay, but now we're gonna continue with installation. So I've opened the installer and now it's just initializing the setup. Visual Studio is a huge program. You can make so many different things with it. That's why the setup, it's gonna take a while. It's gonna take a while, especially if you select more things that are normally not there. Okay, since I've already installed Visual Studio, I can probably press modify. Yeah, that will show you what you would normally see. This is what you would normally see if you install, if you have not installed Visual Studio yet. All these different things, like probably a few things that I've added myself in this list, but it basically is good as it is. You just have to press update or install in your case, and that will do it. Then the installation will continue, you're gonna have to wait for a while and yeah, everything will be fine. After that, oh, yeah, I want to close the setup. After that, we're gonna open Visual Studio. You can instantly do that after the installation is complete, and we're gonna write some sort of simple program to test if it's working. Okay. We're gonna create a new project, since otherwise we can't do anything. New project is either on the start page or it's under file. I'm gonna do it that way so you can like know for later. You can also press on file, new project. That will do the same. Then it's really important that we select Visual Studio, official C++ and a Win32 console application. Otherwise, we're not gonna have to console and things are gonna be bad. Things are not gonna work the way that it's supposed to work. Then we're gonna give it a name. The name that I'm gonna give it is tutorial0. Okay. Now we press OK. Next, and just finish. You don't have to worry about any of this right now. Just press finish. It will make stuff for you, and that stuff will work. Okay, if it wants to work. Okay, so here we are. We are in our first project. So what are we gonna do now? Like if we just press Local Windows Debugger, so if you're gonna run the program right now, nothing's gonna happen. It's gonna open a window and it's gonna close a window. Nothing much after that. Like it opened a window. Now it's loading some DOLs. It might do that for you too, but as you can see it's closed. It's closed already. So first thing that we need to do is hashtag include. That will include a file. Also, it will include something like a header or a pre-compiled header or something that is already in your system. Probably already a system right now. We're gonna do include and then smaller than and we'll automatically do bigger than and then we're gonna type iostream. Don't worry about what this means right now. Just include it so we can make a test program that works. Then we're gonna type std, which is the namespace. I'm gonna talk all about namespaces in a later episode. cout, which is then for console out. So we're gonna write something to the console. Then we'll do a smaller than smaller than. That means that we want to push something into the cout. So we want to push something into the console. Then we're gonna use the double quotation marks and in that we're gonna type something like hello world. Since that's always a programist first thing to write. Hello world. Then we want to push something else into the console. And that is an std. Oh std. Endline. Endle. That stands for endline. Also like an enter. Basically, I'm now doing an endline. That's the same as typing std.endle or doing the slash n, like the backslash n or the forward slash. I think it's a backslash, backslash n. Okay, and don't forget always end the line of code with a semi-colon. Otherwise, it's not gonna work. Like it's gonna give you errors. And now if you press either F5 or local windows debugger. F5 is a shortcut for running debugger. Then it will run a program, but as you can see it, like it didn't even give me time to drag the window from my face over to my main screen. Like it just ran and stopped. That's because normally code just, it just runs and eventually gets to an end and then it just stops. It doesn't keep the, oh god, pop-ups. It doesn't keep the window open, but we right now want to keep the window open. There are two different ways that we can do that. We can either press control F5, which right now the only thing that we need to know is that it will wait at the end of the file. At the end of the program, it would stop and wait for a key press. So that's what I've just done. Now it says press any key to continue. Or we can add another line of code called system, then opening with curvy brackets, and then once again the double quotation marks, and then we type in pause. And once again we end with a semicolon. Okay, now if we press F5, you will see that it will do the same as it did only in a different font. Okay, that's annoying. It's a little bit tiny, but if you can see it, it says hello world and then press any key to continue. Basically what this does is it's the same, but it will not skip some sort, some of the debug things. That it would otherwise skip. But if this all worked fine, then it was the first tutorial. Be ready for the next one. It's going to be uploaded soon. And in that we're going to talk about probably just functions, how they work, and the different types of variables that you have. Maybe first, yeah, just first the variables, and then we're going to talk about functions in another one. Basically we're going to get you up to speed really quickly. Goodbye. Hello everybody. In this tutorial, I'm going to explain to you the different types of variables that you have in C++. On the top here, you have the different types of variables. They're full name if they have one, so even isn't a relative full name. A brief explanation of what the variable is and the size of each variable. And then in here we have a few extra modifiers that you can add before each different variable. And down here I have created a little example of how to use them. After this we're going to go to Visual Studio so we can see it actually in action. But now I'm going to go step-by-step through all of these different things and explain what they mean. First of all, the integer that you write as int. Basically it's a round number, so it can never have any values behind a comma, and it's always the size of 16 bits, unless you give it a special modifier, but we're going to go there in a second. The second thing is a float. A float is also really important, just like an integer. You're going to use it a lot, and it's basically a floating point value. A number with always numbers behind a comma, even if it's zeros, it always has numbers behind a comma. And it has the size of 32 bits. Then we have a boolean, which you type as a bool, and it's basically a true or false statement. So it's either one or zero. It's either yes or no. So it has the size of exactly one bit. Then we have a character. A character is basically just a single character, so anything that you can type on a computer. So this is a character, but this is also a character. This is a character. And even the tilde is a character. They're all just characters, and it has the size of 8 bits, since all of the ASCII things can fit in there. Then with a string, and the string is basically just a lot of different characters behind of each other. So basically you can, words or sentences, are all strings. And the size of it is unknown, because it's 8 bits multiplied by the amount of characters that you have in a string. And then we have a double. What a double is, is basically a double of a float. So it doubled a float. So it's twice the size of a float. So a float is 32 bits, and a double is 64 bits. The big difference between a float and a double is that the precision of a float is seven characters behind a comma, and the precision of a double is 14 or 15. I think 15 characters behind a comma. So it has a lot more position. That's why it's twice the size, because it needs a lot more position. Now we have the different types of keywords. So we have signed. Basically what signed means is something like a different variable can be both positive and negative. Every variable is standard signed. So you basically never type in signed. Unless you really want to specify, and just really want people to know that it can be both negative and positive. Then you have unsigned. And what unsigned means that a value can only be positive. And what that gives you is an extra bit that was normally used to specify whether value was positive or negative. So basically the amount of the highest number that you can store in it is doubled. Because you have an extra bit available. Then we have a long. And what a long is, it's basically doubles the value of whatever variable you gave it. So a double integer would be 32 bits. And a double float would be 64 bits. And a double double, sorry, not double, a long. A long end would be 32 bits. A long float would be 64 bits. And a long double would be when 28 bits. So never do a long double. It's a lot of bits. And then we also have the long, long keyword. And basically what it does is it makes it a long, so it doubles it, and it makes it a long again. So it doubles that value. So an integer would then be, so a long, long integer would be 64 bits, for instance. And now the declaration of a variable. How do you declare a variable in C++? Well, you first specify the type. So in float, bool, char, string or double. Then the name. So whatever name you want your variable to be. And then we can also specify the value if you want to. If we don't specify a value, so we just, yeah, just give it a type and add a name and then we end it off with a semicolon because that's how you end that line in C++. Then it will be just a random value. So it's always useful to do type name is equal to zero at least. So we can also specify a value and we can also type a modifier in front of it. So for instance, signed, unsigned, long or long, long. Here are a few examples. So you have an integer and I just called it my first int and here I did not specify a value. This is completely legit in C++. This works, but we can also type it as int. My second int is equal to 20. This is exactly the same, but now you already gave it a value. And the third thing that you can do is give it a special modifier. So here I typed unsigned int my third int. And this is also fully legit. So now it's an integer that can be like double the size and normally be because it's unsigned. So it can only be positive. So let's now jump over to Visual Studio and make a project where we're going to type some code. Okay, so we're now in Visual Studio. We're going to create a new project either via the new project and start or file new project. Then it's going to load the window and we're going to specifically select win32 console application. This is important. Just select that one. And then we're going to give it a name. So I'm just going to call it tutorial one. Then we're going to go to the application wizard. We're going to hit next. And then we hit empty project. This is important. Otherwise you're going to get a few random headers that you can't delete. And it's just a pain. Just press empty project and everything will be fine. Then we press finish. Then it's going to create the project for us. And then we're going to make a file. And we can start coding. So now I created the project. Now we need to add a C++ file, so a file that we can write our code in. And that needs to be in the source file folder. So we right mouse button on source files, add new item. Then we specify C++ file, so Cpp. If you can see it, make sure that you selected visual C++ right here. And then underneath here we can give it a name. So one of the files that is always handy to have, like give it exactly this name, is main. So you know that that's where a code starts. We can call differently. You can start your code from whatever file you want. But it's always handy to have a file called main. So other programmers know that that's the file where the code starts. Okay, then we press add and it will create a file for us. Now it's opened. And then we can start with our main loop, or well with our main function. So the one function that will always be called at the beginning of a program is int main, so the main function. Functions will be explained in a further episode, like exactly how they work and how to declare them and all. But this is just one thing that we need right now in order to write code. Okay, so let's now create one of each type. So we're going to create an integer, float, a bool, a character, a string and a double. So let's start with int. So we create an int with the name myFirstInt. Like that's the name that I'm going to give you. You can call whatever you want and it would just work fine. And we're going to specify it as phi. And we end with a semicolon. This is really important, otherwise a line is not closed and it will give you errors. So now we're going to create a float, myFirstFloat. And we're going to give it a value of 0.5 since it's a float, so we can give it numbers behind comma or point in this case. But because it's a float, we need to specify an F behind it. This is really important, otherwise it's going to be a double. And that means that it's going to take twice the amount of memory. And a conversion is not necessary because you can also just specify an F behind it and it's all fine. Okay, the next one is a boolean. So we're going to create a bool, myFirstBool. And we're going to give it either a value of true or false. But you can also specify 0 or 1, but there are special keywords that are basically equal to 0 or 1. This is true and false, literally just true or false. So let's specify this true. And then the next one is a char. So we're going to create a char, myFirstChar. And then we set it equal to, yeah, a character. But the way that you specify a character is with the single quotation marks. This is really important, the single quotation marks. So in there, we can specify a character. So let's do the T and then we end with the semicolon. Okay, the next thing, the string. The string you can't just, it would just type in string. Nothing would happen, like it would not find anything. It would give us quickly a line underneath it. Why is that? That's because a string is not standard, but most programmers see it as something standard. You need to include a special file that contains a string. So at the top of a program, what we want to do is to pound sign or a hashtag include. And then there are two different types of include. You can include with the double quotation marks. That will include something that is inside of the project itself. So one of the header files. But if you want to access something that's already written by Microsoft, for instance, then we need to use the smaller than bigger than. And as you can see, a whole list will appear with different things. Like a lot of different things you're probably never going to use. But the one thing that we're going to use is called string. So basically just string. And that will include strings for us. But wait a second. Just type string. Still nothing will happen. Why is that? That's because the string is part of the standard namespace. One of the things that's created for us to separate some different things from the main program. Just in case you want to use something else. It's also called the same name because it's a common name. But then it will give a conflict if it was just always accessible. So they've created something called namespaces. I won't explain namespaces in a later tutorial. But the one name space that we're going to use is called STD. Remember that name? It's really important. STD. And then in order to use a namespace, we need to do the double dots. Like whatever they're called. I forgot the name. And now if we type in string, you can see that we can find a string. So we can set it equal to... Oh, we need to give it a name. My first string. And then we can set it equal to whatever. But how do we specify a string of characters? Because if we would just type something, it would give us a squiggly line. And if we would use the single quotation marks. Well, they give us a squiggly line because a single quotation mark is only supposed to be one character. So in order to specify a string, we use the double quotation marks. So now we can type in whatever you want. So let's type in my first string. And then we end up with a semicolon. It's really important. Okay, but there's another way to get to string. There is a way that we can just type in string. My second string. And we set it equal to my second string for now. There's another way that this will work. This is possible to work. What we can do is we can say, hey, there's this one namespace that I'm going to use a lot. So I want to make it so I can just access things without typing it out over time. The way that we do that is we type using namespace because we want to use a namespace. And then the namespace that we want. So STD and then with the semicolon. So what this does is it will make it possible that we can just type in string. You see, no screw the lines under any string anymore. But we can still access everything using the namespace. So right now both of these strings are valid. Okay, third thing, a double. So we just type in double. Then my first double. And we can set it's equal to the same thing as a float. So let's put it at 1.8. But the one important thing is you don't specify the F at the end right now. A double is always just going to be whatever value without an F at the end. Because it's a double. It's something different than a float even though it looks like a float. It's internally different than a float. Okay, and these are all the different types of variables that are in C++. Unless you also want to... Let's also do the one with the keyword. So let's do a long, long. So we type in long, long again, and then int. My first huge int. So we can set it equal to a huge number and it will still work. This might be too big, but I don't know actually since it's 64 bits. That's a lot. So this is also work. So now we can run the program using either F5 or the green arrow at the top. And, well, nothing will happen since we haven't done anything with the variables yet. But it want to give us an error if I'm correct. Yeah, it will just close. So the one thing that we can do right now is we can print out all our different things. So in order to print out things, we need to include yet another file. So you do the hashtag include or pound include, and then we include iostream. iostream is a file that you basically always want to include because it's a really commonly used file. So now we can once again call the std namespace, which is not actually necessary anymore because we used using namespace std. So now we can use cout. It stands for console out since we're every time running a console. With this function, we can print out something to the console. The way to use this is we pushed something into the cout. So we pushed something into the console and we do that with a double quotation, a double lower than. So now we can push in my first int and then it will display the int. And then we can do also a cout for my first float and it will print it out too. But now if you just run a program, as you will see, or probably not see because, yeah, it closed instantly. The one way around that is using ctrl f5 instead of just f5 or just a green arrow. So if you press ctrl f5, then it will keep it open. But wait a second, it says 50.5, but my first int was 5 and my first float was 0.5. So why does it say 50.5? That's because we didn't specify that I needed to put an enter or space or something at the end. So the thing that we're going to do now is use another thing that STD includes and that is endall. So we're going to put something else into the cout to the console. It's something called endall which stands for endline. So we're going to do it with both the int and the float. And then we're going to run it again using ctrl f5. And as you will see right now, it says 5 and 0.5. So we're going to do the same for all the different things. So I'm just going to use ctrl c, ctrl v in order to copy it a few times and then just modify the names. Okay, I'm going to need another one. So if you now run the program with ctrl f5, it will show us exactly everything that we've created. So 5, 0.5, 1, because it's true. 1 is true, 0 is false. T, because we specified the character as T. My first string, my second string, 1.8 and then press any key to continue. That shows it because we press ctrl f5. Okay, this was it for the tutorial. In the next tutorial, we're going to learn how to do different statements. For instance, an if statement. Or a for loop. So for, and we're going to learn what we have to put in there. A while loop and do a while loop. I'm going to learn you all these different things in the next tutorial. So thanks for watching and see you next time. Hello everybody in this tutorial. I'm going to explain to you the different types of statements that you have in C++. So let's dive right into it. The first type of statement that you have is called an if statement. Basically in here, you specify something. So I've created a little variable called i. And let's say i is an integer. And only if the value of i is less than 5, we're going to do stuff. Then we have an else statement. So let's say that we have this if statement and i was 7. So it was not less than 5. Then the else statement will be called if we specified that. So then I will do this stuff. But let's say we also want to check then if i is less than 7. Because the special case is if i is less than 7. But it's bigger than 5 but less than 7. Then we can call an else if statement. Then we can specify some other values. So right now I said else if i is bigger or equal than 5. I'm going to talk about these different ways to check too. Then we do stuff. Then we also have a for loop. So basically we create some sort of value. So now I've created another integer. I shouldn't call it i. Maybe I should call it j. And basically it does whatever you specified in here. How many times you specified it should do it. So basically j started 0. And then every time that this loop is finished j will be incremented by 1. So it will do stuff 5 times. And then we also have a while loop. And basically while this statement is true it will do stuff. But if this statement is not true then it will not do this stuff. Then we also have a do while loop. And basically what it is it is exactly a while loop. But it's a little bit different because it will first run the code. And then check if it should continue. Unlike the while loop it will first check if it is true. And then do this stuff. So basically it's a reversed order. It will first do this stuff. And then check if it should continue. Instead of first check if it should continue. And then do this stuff. And then we also have a switch statement. A switch statement basically is you should give it a value. So in this case an integer i. And then you have different cases. So in this case if i is equal to 5 then it will do stuff. And then stop doing stuff. Continue with the rest of the code. That was a really really quick explanation of all different types that you have. So let's now put them in code. So let's go to Visual Studio. And let's create a new project. So new project. I'm going to call it this tutorial 2. And make sure that it's a Win32 console application. That's really important. Press ok. Hit next. Make sure that it's an empty project. Also really important. Hit finish. Ok now that the project is created we need to create our main file. So on source create a new file, a cpp file called main. So one thing that we need to include are the includes that we just last time. So once again what an include is is a hashtag include or pound include. Then either smaller than bigger than or double quotation marks depending on what type of file you want to include. So that we smaller than bigger than. And then we type in iostream and we do hashtag include string. Then we specify our main loop again. So in main don't worry. Ok now that we created our main file we want to do two includes that we also did last episode. So we want to do pound include or hashtag include. Either smaller than or bigger than or double quotation marks. Then depending on what type of include we want right now we want something that's external of the project. So we use a smaller than bigger than. Then we type iostream. And then we also do a hashtag or pound include string. Oh not stack string. Because we also want to use strings in this example. For printing stuff out to the console. Ok so let's now create our main loop, our main function. So that's we need to specify the return value first. So in this case an integer and a name which is main. And then it doesn't take any parameters. Don't you worry next episode we're going to talk all about functions. And I'm going to explain to you exactly what to do. And how to make them. And what type what you can pass into it and all. So basically just going to be a full explanation about how functions work. But now first. First you want to start with what was the first one? An if statement. We want to start with an if statement. So let's first create a value. So let's create an integer. Specified with int. Call it myValue. Let's give it a value of 4. Then we can do an if statement. So if myValue. Now we have different types of, we have different types of checks that we can do. So I'm going to specify them here in comments. You create a comment in Visual Studio with C++ using a double forward slash I think it's called. That will be green so you know that it's a comment. By the way if my colors are a little bit different than you that's true. Because I modified my colors to be a little more explanatory to me. So don't worry if it's different. It's just fine. So we have a smaller than. Basically what a smaller than is. If value is less than. So we have let's say we have a value x first. After that I value y. So if x is less than y then it will be true. And then we also have bigger than. So if x is bigger than y. If value is bigger than. Then it will return true. So only if x is bigger than y. So in this case if x was 4 and y was 7 then it will return false. And also if x is 7 and y is 7 it will return false because they're exactly the same. X is not bigger. That's the next thing that we're going to do. X is equal to y. Yes it's not just a single is. Since I would set x to y it's a double is. That would check if it's equal to. If value is equal then. So only if x is equal to y then it will return true. Then we can do the statement. Then we also can combine these different things. So x is smaller than or equal. So both values are true. It can be either smaller than or equal to y. If value is equal or less then. And then we can do the same for bigger than. So this will be true if let's say the x is bigger than or equal to y. That means that it will return true if x and y are both 7 for instance. And x and y like x is 8 and y is 7. Is equal or bigger than. That are the different types of modifiers that you have. So now we can do my value is bigger than let's say 5. So only if my value is bigger than 5. Let's do in this case 3. Then it will enter the statement. By the way we use the curly braces to specify different scopes. And things that you created in a scope are only accessible in that scope or lower scope. So if I create an integer called my second value. And set it equal to something. Then I won't be able to access my second value out here. Because it's already out of scope. Because the scope ends here. After this everything that's inside of the scope does not exist anymore. Except if you specified it outside of that scope. So as you can see we can still access my value. That's because we created in this scope. And in here we can also access my value. Because it's created in a higher scope. But we cannot access my second value out here. Okay. So if my value is bigger than 3. Then we do an stdc output. We're going to specify using name space std. Remember that name from last episode? That's the name space that's globally used for standard things. So what we do now is we make it so that we can just access that name space without writing a name space every time. So now we can do a console out, cout. We push in my value was bigger than 3. And then we push in an end line. So it will give us an enter. And let's specify up here a cout testing if my value is bigger than 3. And then an other end line. So now if you join this program with ctrl-f5, remember ctrl-f5. Because otherwise it will just run a program and instantly close it at the end. So now you can see that it will say testing if my value is bigger than 3. So that's great. So let's now do the second thing. An else statement. So we simply just type in else. And then do the brackets. So let's do an else cout. My value was less than 3. And then an end line. So now if we change either the value of my value or the thing that we're checking against, it will change the way that the statement works. So now if I say my value is equal to 1 and we run the program again, it will say my value was less than 3. Because yeah, my value is 1 and not 3. So now let's add the third statement. An else if statement. So let's do that in between here. Since an else if statement always needs to be after an if statement. It can't be after an else statement. It needs to be after an if statement. Else if my value is equal to 3. Remember it is equal to. That's a double is. Not a single is. It's a fault. That's a fault that I've made a lot of times. And it will give no error. Because it's just valid. You just set my value to 3. But that means that it will always return true. Because you just set the value to 3. That's legit. It works. And then we just always do whatever you want it to do. Only when the value is equal to something. So that's something really important. Don't forget that it's a double is. So let's see out now. Console out. Center console out if you forgot. My value is equal to 3. And then an end line. Because otherwise we won't have an enter at the end. So now if you set my value to 3. And run the program again. It will say my value is equal to 3. And if we change it to something else. So let's say 2 again. So it's not going to be in this if statement. And it's not going to be in the else if statement. We now run it. You will see that it will still. Call this function is else statement. It will still say my value was less than 3. Oh, I misspelled that by the way. But what we can do is we can chain else if statements. We can type another else if statement. And then this we can check for instance. If my value is equal to 2. And if my value is equal to 2. See out. My value is equal to 2. End line. Our value is right now equal to 2. But what you will see right now is that it will call that function. So basically what we can do is we can chain a lot of different statements. But there's an easy way to do that. If we want to check each number individually. What we can do is use a switch statement. What a switch statement is. It should type and switch. Then the rounded braces. In that you type the value that you want to check. So in this case my value. And then you use the curly braces to specify. Okay, this is the scope in which you're going to check things. Like here I'm going to specify all my different values that I want to check against. So let's for instance say let's add a case. A case is something that you want to check against. So we add a case called 2. And then we use it double dots to specify. Now I opened it. This is the case. And in here we can type whatever you want. So see how my value was 2. And then an end line. We can do this for and then we need to specify a break. It's really important. Otherwise it will continue to code. Since what you can do is say for instance you want both 1 and 2. To do exactly the same. Instead of copying the code. So instead of having to copy. In this case the C out. You can just type in case 1. So now both case 1 and case 2. We'll call this C out. My value was 2. So if we change my value to 1 right now. What you will see it will say my value was 2. Right here. My value was 2. Because we did not specify a break. But if I would now type a break here. And I would run it again. Then you will say it doesn't say it anymore. And what if we want something else. Like do we have to specify it for every single number. If we only want to have two special cases. So we now have my value was equal to 1. And my value was equal to 2. But what if we, I just wanted to say with everything else. My value is not equal to 1 or 2. What then? Then we can use a default. So we just type in default. And then double, the double dots. And then we input the break of course. Otherwise it will run the code. I'll continue running the code. And that's not what we want. We want to stop after we, like after we done what we need to do. And now we can type. See out my value was not equal to 1 or 2. Then in line. So basically what this will do is it will call this default function. Whenever none of these cases are met. So if my value is not equal to 1 and not equal to 2. You can have up to as many as you want. You can have up to like a thousand different cases if you want to. But when it's not equal to one of a thousand different cases. It will call a default statement. So let's say my value is equal to 0. And we run this code. Then it will say. My value was not equal to 1 or 2. That's true. Okay. Next thing. So I've already done the switch statement because it felt like a good moment. Okay. So the next thing is a for loop. Let's do a for loop. So the way that you declare a for loop. Is for. And then you use the soft. The rounded. Braces. And then you type some sort of value. So let's just use an int. Let's give it a name. So let's call it index and such. Use for a normal name to call it either called index or I. I is short for index. You said it said it equal to starting value. So let's say 0. And then. Then you specify do this while this is true. So this is basically a while loop. A different version of a while loop. I will show you exactly the same that you can do the same thing with a while loop. In just a second. So now you will specify the statement just like we do in an is statement. Well I is less than let's say five. Then we end that part. And then we do I plus plus since we want to increment I by one. I plus plus you can by the way use plus plus on. Every single. Oh this from the last tutorial every single different variable. The only thing that. In this case. So it's equal to it's the same as doing I is equal to I plus one. Or I plus equals one. What you can do with counting and C plus plus is you can. Specify whatever you want to add. Or remove or divide by. Before it is equal to. So what we can do is I divide equals one. So that means that this is exactly the same as doing I is equal to I divide by one. Or plus equals one is equal. Is it is the same as I is equal to I plus one. So what we do is we do I plus plus since that's the easiest way to do it. Then we use the curly braces. And then in here we're going to let's say see out. The current index is. And then we're going to push the index in. So we're going to push an I. And then we're also going to push in an end line. You can push as many different things into a console out as you want. So what this code will do. Is it will. Oh let's move it over. It will run this. Program that will run this will do whatever is between the brackets. Well I is less than five. But as you can see it never it stops at four. That's because he specified I is less than five and not I is less or equal than five. So if we do that and we first close it. And then we run it again. Now you will see that will also include the five so. Current index is zero one two three four five. Okay the next thing is a while loop. So a while loop is a really. Is the thing that you shouldn't use if you don't have to only use it when you have to. If you can use a for loop or if statements a switch whatever. Try to not use while loops unless you really need to for instance for game loop. So let's say well. Let's make let's make something that looks like a for loop because otherwise I'm not going to never going to exit. So we're going to create an integer called index. We set it equal to zero and while. Index is less than 10. Do whatever is in between the next curly braces. So whatever is in this scope. Then the first thing we're going to specify or actually the last thing that we're going to specify this while loop. We're going to type it first is index. Plus plus so increment index by one by one. And we're also going to do the sea out. My current index is an index. But let's add just an end line here just so we know that we started the while loop and we're not in the for loop anymore. So we're just going to do a sea out console out and then an end line. Yes you can just do that. You don't have to specify any kind of string or integer or float whatever. You can just do sea out and line. So what this will do it will call this function. It will do whatever is in between the curly braces while index is less than 10 in this case. So basically do it nine times because index will increment every time and after nine times index is not less than 10 anymore. But we could also do it well true. This is a really really bad statement. It should never do and you shouldn't run this on your computer right now at home while you're testing it. Because what this will do is it's an infinite loop. Like as you can see it never stops. It constantly keeps incrementing the current index and printing that out. Eventually this will break because you're out of values. So let's just exit it right now. Okay let's refer to the previous thing that we had. So index is less than 10 and let's now do a do well loop. So we're going to move this well down here. So you first have the scope and then we have to well and now we only have to specify do in front of it. So this means that it will do whatever is inside of this scope. Well index is less than 10 and now we do need to specify a semicolon at the end. If it moves it by the way it doesn't matter it's fine. So now it will first do this whatever is in this scope and then check what it should do. So we should see something funny right now. Oh no actually it doesn't. Well at least this is also a valid way to specify a different loop. A different well loop. This is an old way to do it. I would just use well in the scope but you can also do it this way if you really want to. And we're going to get the switch statement. So we're basically through it all. So these are the different types of statements that we have. Let's have a little recap. We have an if statement that will only run whenever a certain criteria is met. Then we have an else if statement that will also only run when a certain criteria is met. And then we have an else statement and it will run whenever some criteria isn't met. Or a lot of different criterias because you have a lot of different else if statements aren't met. And then we have a switch and this is basically the same as a chain of if else if statements and an else. So this we could basically make a switch out of that but we have to specify each individual number in this case. We can too. My value is bigger than 1. Then I will give like sprinkler lines underneath the bigger than. We can only do with individual numbers. And then we have a for loop. For loop will basically have a value in the beginning that will constantly be incremented. It's basically a while loop like this is the same as a for loop. But a for loop is a nicer way of writing it. Since now you need to create a different variable and like it's basically going to be specified on at least two lines. And it's it's not as clear as this. Since also this index will be always accessible in the main scope like in the same scope as my value is accessible. Well this I this I can't access this I out here. I doesn't exist. But I does exist in here. So what I does is it's basically a while loop. And it also has the value to specify to use only inside of the loop that you're working in. So basically only inside of the for loop. And if a while loop and it's yeah basically do all this code while something while some criteria aren't met. And one thing that's also really handy to know is we can have a Boolean. So let's do it underneath here. So let's have a Boolean called yeah my bool for now and set it equal to true. There's one one extra case that I haven't done in here. But I also want to learn to you guys. And that's let's have an if statement. So if my bool then console out my bool was true. And then line like now I think that you might now be wondering like why did you not specify anything after it? Like why doesn't it have to be equal to something? If you don't specify anything at least with bools like with integers is a little bit different. Just also specify always specify something with booleans. If you don't specify anything then it will run if it's true. And if you put an explanation mark in front of it that will give you the inverse of what the bool is. So then it will be in this case if it's not true then it will run the statement. And what you can also do is like have a second bool my second bool and set it equal to true to false. And what we can have is so if my first bool is true and then you can also add different things. So if my we can add functionality that needs multiple things to be true. We can do a double end. And basically what that will mean is if this statement is true and this statement is true do the statement. So if my bool is true and my second bool is not true then run the statement. So as you can see this will now run just fine and see out whatever is specified so my bool is true. But what we can also do is an or statement. So if this is true or that is true. So let's do an or. An or is with double straight lines whatever I may call it. So let's now do my bool is also not true. So if my bool is not true or my second bool is not true. What you can see is my bool is equal to true so this would return false if we only had that. But my second bool is false. So that's correct. So if you now run this code you will see that this console out is called like it called my second bool was true. My bool was true. So that's basically it. We can chain up as many as you want and we can just chain up as many as we want and everything will just be fine. So thanks for watching. In the next episode I'm going to learn you all about functions. So yeah, be ready. Hello everybody in this tutorial I'm going to explain to you how functions work in C++. So first of all I've created a little document explaining how you declare a function but the different types of returned values are and a little example. And after this we're going to go into the code and actually make some ourselves. So first of all the declaration, how do you make a function? You first specify the return type. I'm going to talk about the different return types soon. Then you specify a name like what name you want to give it. And then between the soft brackets you specify all of the different attributes necessary inside of the function. And then of course you end with a semicolon if you're doing a forward declaration that I'm going to talk about soon. Or you don't do that but you use the curly braces in order to create a scope in which the function will be the function. So like in this example between these curly braces is the function. Everything outside of it is not part of the function. Okay the different return types. Well the basic return types that you have is for instance void, integer, float, boolean, double or a class which we're going to talk about next episode. Or different kind of things like arrays which we're also going to talk about soon. And this is a little example. I've created a function that returns a boolean so either true or false. And it's called is number six. So is this number equal to six and you give it a number. And then I have created a little is statement that checks if the number is equal to six. And if that's true then it will return true. So the return type since we've specified boole we can return true or false. And so in the else I'm going to return false. If this was an integer so return let's say return number plus five. Then we would have to specify them for in this case we would do return number plus five. So whatever return type you specified you need to return that to using the return keyword. Just like we had in our main function I think we had that in the previous tutorials. There'll be an I would return zero. I might have forgotten that but that's something that you should always do. Give it return type the main function. But why do we use functions in C++? Well for instance if we have a lot of different things like with a lot of different enemies that all need to do the same thing like we all need to check if they're colliding with something. Then it's way easier to just make one function that checks collision and then pass in the enemies in the environment for instance. Then to like hard code the collision checks for every single one in your main functions. Is that going to be a lot of code that is all just redundant and we can just make a function for it and then a for loop or for instance. So that's one of the reasons why we're always using functions. The other reason is if we're using classes. Since classes are highly dependent on functions. Everything inside of a class is accessed via functions or at least other things if you want to do something inside of a class you have to do it with a function. And more of that will be explained in the next episode. So let's now dive into the code. So I've created a little new project in which I'm going to explain to you the different types of functions. So first we need to do the return zero so we know that there's no error. Always give it a return value probably for got list episode. So let's now create a function in order to access a function inside of our main loop. We need to specify it above the main loop. So let's create a function that greets somebody. So let's create a void since it doesn't need to return anything void. Great person. And then in here I want to specify let's say I want to give it a string like the name of the person. So we create an std string or we can do using namespace std. So we don't have to specify std anymore. So we give it a string. Let's give it a name called yeah just name since it's a person's name. And then we use the curly braces in order to open a function. And then in here we're going to use the console out. Let's do a high and then a name. Then an explanation mark. And then an end line. By the way someone in the comments asked me if you could use another function called printf. So I'm also going to give you guys a brief explanation on how printf works. So you have printf. It's a function. Standard in C++ you don't need to include anything I think. Maybe I stream but I don't think that you have to include anything. And then between the soft braces since this is a function so we have to give something inside of the soft braces. We can specify whatever you want but there is one fancy thing when it comes to printf. So for instance we can do print number. Print a number. But in printf we can just do like the push in a number. That one work. We can also can to plus a number or actually it will work now. But the fancy thing that you can do with printf is you can give it a comma. So specify it as a second parameter. And then inside of here we can use a percent. And then whatever type it is. In this type it's an integer so we use an i. You also have an f and I think you also have a b and a c for character. So in this case it's an i so we use an i. And so now it will replace the percent i with whatever we specified. And what we can actually do right now is still type after this. So now it will just replace this percent i with the four in this case. So it's a pretty fancy function but I like the c out so the console out a little bit more since it's easier to use. So you can do it with printf if you want to. That's no problem. Okay so now we've created our function. So let's now greet someone. We call the function by typing in a name, greet person. And then we specify a name. So it's a string so we use the double quotation marks. And let's give it a name. Let's think of a name. I've got link on the wall so let's say link. And then if we run a program with control f5 it's the first one that's building it so it always takes a little bit longer. Then you will see that it says hi link. Okay so we have a function so let's now create another one. Let's say we only want to greet specific people and otherwise not create a person. So what we can do is we can create a new function. Let's give it a bool. Make it a bool return value. Like one to greet. And then once again a string name. So now between the curly braces we're going to have I think a switch. Let's make a switch. Or can you not have a switch with no you can't have a switch at strings. That's because it's a clash you can't have a switch. So we need to use an if statement and else if statements. So if name is equal to let's say link because we want to greet link. Then we return true. Else let's just create an else for now. Return false. So what we now can do is we can create an if statement with inside of that one to greet. And then as parameters link. So if you now move greet person into here. We will only greet the person if the name is specified as link. So as you can see now it's as high link. Let's change this to Bob. We don't want to greet Bob. Bob is a bad person. I'm sorry if anyone of any of my viewers is called Bob. It was just an example. But as you can see now it doesn't greet Bob. But let's say Bob is also nice. So we're going to add an else if statement here. And that his name is equal to Bob. Then we'll also return to true. So now if we call this function again it will also greet Bob. But hey what is this? I want my main function to be a little bit at the top. I don't want if I have a lot of different functions to scroll all the way down just to get to my main function. There is a way around that. What we can do is we can forward declare a function. So what we're going to do first is we're going to take this entire function. We're going to copy it. Or yeah I just construct a contract. And then we're going to paste it down here. But in order to make this work what we need to do is we need to copy the first part. So the bool want to create a string name. So whatever the function is, whatever turns and what attribute it has. We need to copy that and we need to paste that up here with a semicolon at the end. So now and only now we can use this function properly. Otherwise it might give us errors and it might be weird. It might do weird stuff. So use the forward declaration if you are using a function for instance in your main file. So we're going to do the same with greetPerson. And now we can run the program again. And you'll see it will still work. So we're going to greetBob. Okay. So now we forward declared functions. We've created functions. Let's now create a function that depends on a function. Yeah. So let's create a function for instance. Avoid called autogreed. And it takes a string name. What this function will do is first actually we have to copy it. So we can create it down here. What this function will do it will call an if statement with a greet with one to greet. With a name. And then if it's true it will create a person. So basically what this function does is a wrapper for this if statement. Such functions are going to be really handy when we're going to work with classes and we're going to do stuff like give enemies health. But we only want it to be able to see the health and not to be able to modify the health outside of the class. Then we can use some sort of function, a sort of wrapper that returns the value of the health. A copy of the value of the health so it doesn't return the health itself. So it's not modifiable. Okay. We're now going to our main function and call autogreed on Bob. We're also going to call autogreed on Link. And let's also call autogreed on Jack. Not Jack. Okay. If we now run this program you will see that it will greet Bob and Link but not Jack. Because we specified that we only want to greet Bob and Link. This is my tutorial about functions. I hope you learned something about it. And next episode we're going to talk about classes. So be ready for that. Hello everybody. In this episode I'm going to talk to you about classes in C++. Why do we have classes in C++? Well because C++ is an object oriented language. So what you have to do is object oriented programming which stands for OOP. And the way that you do object oriented programming is by creating objects. And the way you do that is using classes. So how do we define a class? How do we declare one? We first specify that it's a class and then we give it a name. And then using curly braces we specify that everything inside of this is inside of a class. Two things that a class always has, whether you've specifically said that, whether you specifically want to change it or not, is a constructor and a destructor. A constructor will be called whenever a version of the class, an instance of the class is created. So this function will always be called whenever the object is created. And a destructor will always be called when an object gets out of scope or gets deleted. So then you can do some cleanup code to delete some variables that you need to delete or reset some things. One thing that a class is going to have is member variables. Those are variables that differ per instance of the class. So for instance we have four different enemies and they all have their own version of health. It's all called health, the variable is all called health and it's all the same type but the values can be different. Then we have the different type of specifications that you can do to change how the variables and functions can be accessed. We have the private keyword, we have the public keyword and we have the protected keyword. Private means that only and only these functions and variables can be called inside of this class. So only this class is accessed. Public means that everywhere that this class is created and everything that has access to the class can also get access to this variable or function. So basically our main functions that we want to call from our main program should be public. But our variables like health and money or whatever, our lives, all need to be private because we don't want our main program to change it. Then we also have the protected keyword. I'm not going to go into detail on the protected keyword just yet. I'm going to do it in a later episode when I'm going to talk about inheritance. So when we're going to talk about having a class that depends on another class. I know it sounds crazy but it's going to be something. Okay, so here's a little example of how to create a class. So I specified first that I wanted to be a class with the class keyword. Then I gave it a name, my class. Then inside the curly braces is the scope of the class. So everything that's specified between these curly braces is going to be a part of the class. Then I started with the public keyword. This is really important. Otherwise we cannot create an instance of the class since the constructor is private. So only this class can create this class and things are going to be messed up. So I've added a public keyword which we need to end with a double dot. Then I've created a constructor. The way that you know it's a constructor is because it is the exact name of the class without anything in front of it. So no specifier return type. And the destructor is exactly the same as the constructor but with a tilde in front of it. If you don't know what a tilde is, it's underneath the escape key next to the one. And you need to access it using a shift. Then I've created a little function called avoid function so it doesn't return anything called do stuff. And then I've created some private variables. So I specified the private keyword and then I specified the variables that I want to have. So let's say I've created an int called important value because we needed something important and a boolean should do stuff. Okay, let's now get into the code and actually make a class. So let's head over to Visual Studio. Oh, I still need to create my main file. So let's do that quickly. Main.cpp, check include, higher stream, oh god. And string. We're always going to need those. Then we're going to also use using namespace std. And then avoid, now an integer, main, then return zero. Okay, so this is our main file. Nothing important is going to happen yet. The one thing that we're going to do is we're going to press right click on the project. So as you can see here in the solution explorer, it might be on that side for you. I always move it to the left side. I find that more useful but you can just grab the top thing and then drag it around. So it's probably on this side for you right now, but I just dragged it over to this side. Okay, so what we're now going to do is we're going to press right mouse button on tutorial 04 in my case, but whatever your project's name is. And then we're going to go to add class. We're going to select the C++ class and now we can give it a name. So let's create an animal. So then we type in animal or our class name. You can make whatever class you want, but I'm going to make an animal. And as you can see, we can specify some other things here like a base class. That's for inheritance. We're going to talk about that later. Access, public. You can select whether everyone should be able to create an animal or an animal should be able to create an animal or only inherited classes of animals should be able to create an animal. So we're just going to select public since we just want to create an animal everywhere if you want to. The cpp file and the h file are all just fine. We don't need a virtual destructor and it doesn't need to be inline. We're going to talk about this later in a later episode. We're first going to just talk about basic classes. Then we had finished and now we've created a class. So as you can see, we have an animal.h file, which contains a class, then the curly braces, public, and then the constructor and the destructor. But we also have a cpp file. Why do we also have the cpp file? Well, when it comes to classes, it's really important that you create the functions outside of the header file. A header file should only be the .h sent for header. A header file should only be used to specify things. So for instance, specify the different functions that you have, the different variables that you have, and the actual code unless it can't be inside of the cpp file because of special reasons. They were probably going to get to in a later episode. You need to write the code itself inside of the cpp file. Okay, so now we have our animal class. So let's create an instance first in our main function. So let's create an instance first in our main function. But if we just type animal on here, we're not going to see it. Why do we not see it? Do you remember that I said there were two different types of includes? We're now going to need the second type. So we're going to do a pound include, and now we're going to use the double quotation marks. And as you can see, we can see animal.h there. So what we're going to do is we're going to type in animal.h or click on it if you want to. And then we're going to include the animal class. So now we should be able to type in animal and create an animal. So now we've created an animal. But this animal does not contain anything unless a destructor, and we don't want to call it a destructor yet. By the way, destructors will automatically be called whenever something gets out of scope. So when it comes to the main function, it will be automatically called whenever the main function ends, whenever it returns zero or whatever error code we have. Okay, so let's now create a function, a public function. It's going to be void. And let's call it make sound. We don't need to specify anything yet. It doesn't need any attributes, because we just want to make sound. It doesn't need anything specific for that. And let's also make a private. So we're going to specify the private keyword, string, called sound. As you can see, string is not available, even though we've included string in iStream in here, and even that using namespace std. But we haven't included them in this header. These includes are only going to work in something that is basically the file itself, unless you've basically only in the file itself for now. So we need to do the includes again. So we need to include string, and we need to include iStream, which you want to be on top, because it's the longer one. And then we also need to specify using namespace std. So now we have a string called sound, and we have a void make sound. But why does it have a weird green squiggly line underneath it? That's because it isn't declared in here. The constructor and the destructor are declared, but the sound isn't declared. So what we can do is either go in here and type animal double dots in a make sound. We can either do it like this, or we need to specify the type, so it's a void. This works just fine. Or what we can do is we can right mouse button on the squiggly line thingy. Then press quick actions and refractorings, and then create definition of make sound in animal.cpp. And as you can see now, is there is a declaration now, the same as we just type. But if this weird thing comes, like this weird blue thing, that means that you're inspecting a different file from within a file, then either just press escape to close it, or if we go to peak definition again, or we can press the promote to document thingy to open the document itself, and now it's also gone. Okay, so now we've created a function. We're just going to use a simple console and then sound. And then we're going to use an end line. But our sound is currently specified to nothing, like it doesn't contain anything. So we're going to go to the constructor since this will be called first. And what we're going to do in the constructor is we're going to say sound is equal to whatever sound we want to have. So let's create a shape. So it's going to be a... can I do that thing on my computer? No, so just a meh. Then the cow would work better. So we've created a sound. And now from within our main function we can call animal.sound. Make sound. But as you can see, we cannot find animal.sound. The sound is invisible. We cannot access it through the dot which we normally use to find things inside of classes. But we can find it inside of the class itself. So if you run this function right now, this program right now, you will see that it will say moo. Oh, moo. Oh, I said moo. It needs to be moo. Sorry. Moo. It's a cow. A cow doesn't say moo. It's a moo. Okay. So we have our cow and it says moo. So what you can also do is... This is going to be a preparation for the next tutorial in which we're going to explain inheritance. Is we're going to also add a string called name. And we're going to do the same in the constructor. We're going to do name is equal to cow. And in make sound we're going to add... Wrong one. We're going to add a few things. So we're going to add name. And then double dots and a space. So it will say the name of the animal. Then it will do a double dots and a space and then the sound. So if you now run the program again, you will see that it's a cow moo. So this is basically an explanation of classes. Let's go over it again. So in order to create a class we go to the project itself. Right mouse button on it. Add class. C++ class. We're going to specify the name. Nothing else is important right now. You only need to type in a name. Press finish. Then it will create a class for you. You need to do includes again. So if you need the ios stream or string, you need to include them again. And then you have the public keywords. And that will specify whether something should be accessible outside of the class. With the private keyword. That means that it will only be accessible inside of this class. And you have also an extra keyword called protected. And it will mean that it will only be visible in internet classes. What we can do inside of classes is we can make functions like we did in the last episode. And we can make variables like we learned in the first episode. And one thing that's really important in classes is just like you had with forward declaring functions inside of your main function. So we would declare a function here. And then type the rest down here. We need to do the same with classes. But we need to declare the functions inside of the header file. And then type the code inside of the cpp file. And that's basically it. So thanks for watching and I'll see you next time. This basically means that you're using one base class. Now you derive a different class from it. Which basically contains all of the data from the base class. But adds more specific things to it or change some things around. For instance, let's say you want to have a lot of different enemies. But they all have the same basic functions. Then what we can do is we can create a base class. For instance, enemy base. And then make all of the other enemies inherit from it. So how do we declare inheritance in C++? What we do is we type in class. The name of the child class that we want to create. So the class that's going to be inheriting from another class. Double dots. The public keyword. And then the name of the class that we want to inherit from. I've created this little example. So I've created my base class. So just simply class base class. With public constructor and destructor. And I've created another class called child class. With the double dots, the public and the base class. So as you can see, we're literally just using base class here. Since that's the name of the class. So now we can use whatever is inside of base class. But now we're going to go to code. And I'm going to show you what I can do using the animal example of the previous episode. So here's our main function. We've created our animal and we've called the make sound function. Well, let's say we want to make another animal. Which has a different name and a different sound. Since right now I've set name to animal and sound to no particular sound. So first of all we need to create a new class. How we do that is we go to the project. That's the solution. It's really important that you go to the project. Right mouse button. And then class wizard. That will open up. Then we'll press add class. The class name. Let's create a mouse. And then here in base class we type in animal. Since that's the name of our base class. Make sure that any capital letters are of the letters in the same size. So everything that's a capital letter needs to be capital letter. Everything that isn't needs to be a not capital letter. So base class is going to be animal. And then we create fish. Let's also for instance create a cat. Let's create a mouse and a cat. As you can see I just forgot to put in the base class. So one thing that we then can do is we can define it by ourselves. And we can just define it the same way that we did right here. So class, child class, double dots, public, the base class. So now we have our cat and it needs to inherit from animal. So then we use the double dots, public. And then the name of the base class. So animal. As you can see right now you have a squiggly line underneath animal. How can we fix that? What we need to do is we need to include the file. So we need to include animal. And as you'll see right now that will fix it. And when you create inherited class from the class wizard it will automatically include animal for you. I'm just going to do a little formatting so it will be a little bit nicer. So what we can do right now is we can go to the constructor for mouse class. And in there we can change the name and the sound that the animal makes. But one thing that we need to make sure that the animal has. And that is that other things are nicer to private. That we do want to change this out of a child class. So for instance the mouse that needs to be set to protected. That means that we still can't access it from outside of the class. But that all of the child classes can also modify it. So this needs to be changed from private to protected. So now when we go to the mouse at CPP we can change the name to mouse. And the sound to let's say peep. So now if you run the program again but instead of using an animal we use a mouse. The mouse just got animal. So if you run it as you will see it will now say mouse peep. Even though in animal the other CPP who specified name is equal to animal and sound is equal to no particular sound. We modified it inside of the mouse constructor. Let's do it also for a cat. So the cat the name will be cat. And the sound will be meow. So now what we can do is we can also include cat. The cat will see. And also create a cat. Just a cat called cat. And then we can also call the cat make sound. And if you now run it as you will see it will say mouse peep cat meow. So basically what we're doing right now is we're cutting a function from the animal class while using the variables that we changed inside of the constructors of the cat and the mouse class. That's what inheritance can do. It can make it so you can easily modify values while still using the same functions that you were using with the base class. Thanks for watching and in the next episode I'm going to explain to you virtual functions. So be prepared. Hello everybody in this tutorial I'm going to explain to you virtual functions. So what is a virtual function? Well let's say that we have inheritance. So we have a base class for instance an animal and we have a function that we want one of our child classes to modify because a child class might behave differently. So let's say we have an animal with a move function. The basic move function will just slowly move something around but a cat and a mouse that will both move around really quickly. So what we can do is we can use variables for that and modify the variables but what might be easier to do is just override a function. So how do we override a function? What we do is we specify the virtual keyword in front of a function. So all we have is the virtual, the return type, the function name and then between the self brackets all the attributes that we need. So as an example we have a virtual void called update with the delta time and the only thing that specified that it was virtual so that we can modify that's the virtual keyword. Then we also have something different, something called a pure virtual function. What is a pure virtual function? A pure virtual function is a function that needs to be overwritten by a child class. The class cannot work without it being overwritten. So basically the base class that you have, you cannot create it because it has a pure virtual function so it has a function without any definition you need to create a definition external. How do we declare a pure virtual function? Well the same way that we just declare a virtual function only with and is equal to zero at the end. So let's now dive into the code and actually create some virtual functions for yourself. So here I am again with the cat and mouse example from last episode and let's now make the make sound function virtual. So the first thing that we're going to do is we're going to go to the animal.h file and we're going to specify virtual in front of the make sound function. Virtual void make sound. Okay, what we're now going to do inside of the animal.cpp is just copy this and remove it. So a basic animal will not make any sound it will not do anything if you call a function. So let's now go to the cat.h for instance and then under the public keyword we're going to specify a void called make sound with no parameters and what we're going to do is we're going to type overwrite. This is important this will specify that we're going to overwrite the function that we had and then end it with a semicolon. Then we're going to create a declaration and simply just paste it in here and then we're going to do the same with mouse. So we're going to type void make sound no parameters overwrite and we're going to create the definition too and we're just going to paste it in here. So what we're going to do right now is in the main.cpp file it's once again include animal and now we can just create an animal so let's just change this back to animal and then also create a mouse and then also call the mouse that makes sound function. So now if you run the program actually we'll see is we're just going to see the cat and mouse so we're just going to see mouse, peep, cat, meow we're not going to see anything from the animal that's because in the animal.cpp file we specified that make sound doesn't do anything to further show you that this actually works we're going to modify the mouse function so let's say we want the mouse function to also end with an explanation mark because we want the mouse to be really really really aggressive so as you can see right now it's this mouse, peep, explanation mark and cat, meow, so that shows us that it will call the correct function based on what class it is because we made the original one a virtual void so let's now make a pure virtual function how we do that is we specify it is equal to 0 at the end so now we need to delete the declaration of animal that makes sound because it's a pure virtual function so there can't be no definition inside of the base class so if you now go back to main.cpp you will see that animal has a squiggly line on anything and I will say object of EPSR class type animal is not allowed function animal make sound is a pure virtual function so what this means is we cannot create a base animal anymore we need to create an animal of a specific type so if you just remove anything that has the animal in it right now and if we run it again which we'll see it will still just call the functions that we've created earlier so the mouse, peep, with an explanation mark at the end and the cat, meow so what we've run it right now is what a virtual function is so how we can override a specific function and we've learned what a pure virtual function is so how we can create a function needs to be overwritten this is really handy if you want to have for instance different animals or different NPCs that all behave differently on let's say for instance walking that all walk differently but you don't want anybody to be able to just create an animal a pure animal or a pure NPC they need to be an NPC of a specific type then we can use pure virtual functions so we can specify that we need specific behavior that's different from all the rest for this specific class that was it thanks for watching and in our next episode we'll explain to you what arrays are so be prepared hello everybody in this tutorial we'll explain to you what arrays are first of all why do we need arrays arrays are really important when it comes to for instance having a map since you want to store that map you want to store every different tile that you have so what we then can do is we can create a 2D array which is an array which contains arrays for instance integers which contains the type of tile that it is what we can also do and what we're going to do for this example is have an array of friends we want all of our friends names we want to be able to just check the array and see all of the different friends that we have so how do we declare an array we first have the variable type just like we do when we normally declare a variable now if the name just like we do when we normally create a variable then we use the square brackets with inside of that a size and if we do this and then use m with a semicolon then we'll have an array with none of the values already specified so we'll just be random values so what we can do is we can have the variable type then a name the square brackets the size again so exactly the same as we had before but now what we're going to do is we're going to use the curly braces and in there we're going to specify all of the different parameters based on the variable type so for integers you're just going to type integers for flows you're going to type integers with dots and f's at the end for doubles we're going to do yeah without the f basically for characters we're going to use the sync notation marks and for strings we're going to use the double condition marks so basically that so here's a little example so let's create a string of friends so we have a string friends which contains five friends since we said we'd buy the size of five now we're going to create another one also called friends with the size of five but now we're going to specify the names so Alissa, Jacob, Josh, Daniel and Brittany but how do we access something inside of an array we're going to use the same square brackets again so we type in a name and then we use the square brackets and then the index make sure though that your index is never bigger than the size of the array since that will resolve an unexpected behavior and one thing also to note is that counting always starts at zero and c++ so zero, one, two, three, four and that's basically all of the indexes that you can have for an array of five so it's not one, two, three, four, five like you would guess it would be, you know, it's zero, one, two, three, four so as an example here just access the name of a friend and here I simply print out the name of the friend so let's now dive into the code and see it in action okay now at our official studio we can create an array so let's create an array of strings so you type in string, it's already specified using namespace as city, create a name call it friends and then we're going to specify a size, let's say five so now we want to give it a value so let's use the square brackets and then in here we're just going to give it some empty parameters so we know that we have exactly five of them and they don't know what to semicolon okay so now we're going to give it a value so let's use the same that I used in the example so if Alissa, Jacob, Josh, Daniel, and Brittany okay now that we've specified all the names of our friends what we can do is we can make a little for loop to print out of the different names of the friends so we have let's create a for loop i, i stands for index it's equal to zero, i is less than five because we have five friends in our array a semicolon again i plus plus so i plus equals one and then in here we're going to do c out, push in there friends with the index of i, so with the index of index and then we're going to also push in an end line so now if you run this program with control of five so it breaks at the end as you can see it will say Alissa, Jacob, Josh, Daniel, and Brittany so using the zero one, two, three, four, we exit all of our different friends, our friend at zero is Alissa, friend at one is Jacob friend at two is Josh friend at three is Daniel friend at four is Brittany so let's now talk about the 2D arrays that we had what is a 2D array? well it's basically the same so let's now use an integer so in encode our map so we're going to create a map for a level for instance and then in order to make it a 2D array we're first going to specify the first size of the array and then we're going to specify the second size of the array so now we have a 2D array how do we initialize it? what are two ways that you can do? we can either use the square brackets of technique but how do we do that for a 2D array? but what we do is we use square brackets for each of the array things so for each array inside of the array so basically I've created five arrays inside of the array so what we can do now is specify let's make the first one 0 1, 2, 3, 4 second one we're going to have 5, 6, 7, 8, 9 let's start with 0 again but it's already coming to 5 so let's do 0 1, 2, 3, 4 again so in here have 5, 6, 7, 8, 9 again then in here let's just have some random values 4, 6, 3, 8, 9 and space them out evenly one of the easier ways to note this when you're using a 2D array is to actually make it 2D so what we can do is we can simply break the line and then use tabs to space it out evenly and do this for every single part of the 2D array so now you can actually see that it's a 2D array so how do you print everything inside of a 2D array well that's actually pretty easy to do what we're going to need is two for loops so we're going to need a for loop which contains an integer called y and why do we need y first and not x I think it was x, y right that's correct that's how we know that when you're just saying x, y but when it comes to 2D arrays if you just look at it what you will see is that you will first have the different arrays that go like that since you go like down scopes so the first scope is the scope of these curly braces and as you will see everything inside of here is noted down the 0th parameter, the 1st parameter, the 2nd parameter the 3rd parameter and the 4th parameter and then the 2nd part the 2nd array is inside of these braces so the 0th parameter, the 1st parameter the 2nd parameter, the 3rd parameter and the 4th parameter and so forth so we first need to access the y coordinate so we're going to print y is equal to 0 y is less than 5 because there is less than 5 to 5 size 5 y plus plus so now we're going to create another for loop for print x is equal to 0 x is less than 5 x plus plus so now in here we're just going to do a simple cout map y x notice that I don't do an end line here because I want to do the cout end line out here because otherwise it would end the line after every single number so 0 and 1, 2, 3 and 4 would all be on this line but we want them to all be evenly spaced out like we want them all to be in a row because they are in a row in an array so what we do is we do the integer and the end line on different lines so we first do all of the integers that we have here and now we do the end line but one thing that can make it look nicer is also push in a simple space so if you now run the program again as you will see we'll now have all of our different values printed out that's basically all that I can explain to you about arrays so thanks for watching and in the next episode I'm going to talk about vectors, a special type of array so be prepared hello everybody in this tutorial I'm going to explain to you vectors in C++ so what is a vector? right now when we've used arrays they all have been of aesthetic size we all specified the size of the array but what if we don't know what the size of the array is going to be for instance we don't know how many different animals we're going to have in our game we don't know how many different enemies we're going to have in our game we can't just use an array because that would specify a size so it will always be an X amount of enemies so what we can do is we can use a vector an array without a specified size we can always change that how do we declare a vector? what we type in is vector then we use a smaller than then the variable type then a bigger than and then the name of the vector so basically we're declaring a variable with vector in front of it and the square brackets around the variable type so in this example I'm going to use friends I've created a vector of strings called friends but how do we add something to a vector? well we use this little function that a vector gives us called push back and then we specify between the softraces what we want to push back and how do we access something inside of a vector? well we simply do it the same way as we do it in an array so let's now dive into the code and see what happens okay now that we're in our main.cpp file we need to include one thing first we need to do how to include and then we need to include a vector otherwise we're not able to use vectors to do it for this extent so let's now create a vector of friends so a vector of strings with our names so we're going to type in vector normally we'd have to do sd vector but I've used using namespace sd to get rid of the sd namespace we're going to create a vector of string see that I also included string and then we call it friends so let's now add a few friends so what we're going to do is friends.pushback let's do jack friends.pushback dated and let's do one more friends.pushback that pushback any okay so now we've specified over different friends so let's now print them out to the console so we're just going to create a little for look so for int i which stands for index is equal to zero i is less than and this is a little bit different in vectors we don't have to specify a number yet we don't have to type in three because with three different friends we can use something very specific that a vector has friends.size so the vector name.size that will give you the number so in this case three but if we added one more friend it would be four and then i plus plus so just basically make sure that you will loop over every element inside of the vector without having to specify or hard code a number so what we now can do is to console out friends.i and then end line so if we now run the program as you will see it will say Jack David and Annie which are our friends we specified well let's add one more friend one friend that we sort of have but isn't really a friend but that needs to be on the official list this is going to be an official list and then we're going to like remove the friend and give an actual list of our friends so let's create a friend friends of pushback let's call him Tim so now if we run the program as you will see it will say Jack David Annie and Tim there's one more little function that we can do what if we don't want to use a specific friend anymore what if it isn't a friend anymore all we can do is call friends.pop back what this will do it will remove the last friend that we specified so basically right now Tim is going to be the last one since he was last added to the vector so Tim will be removed so if we copy this and do the form up again at the end if we now run the program which you will see it will say Jack David Annie Tim and then Jack David Annie what if we don't want to remove someone at the end of the vector so we want to leave Tim but we want to remove someone that's more in the middle for instance David what we don't need to call is a function called swap so just type in swap and then what you need is you need two different operators what we're going to do is we're going to call friends and then we're going to specify the index of David so this is going to be one remember that you always start counting at zero not one and then we're going to use friends and have back basically what this does is it will swap the values but why do we use dot back and not a specific index for this well what that back does it basically always returns the last element in the vector so we'll basically return Tim right now now that we've done that we can simply just call friends dot pop back so if you remove this friends of pop back and simply keep this swap friends one and friends of back and then called pop back which we'll see is now we have a vector which still contains Tim but it contains Tim at the second place but it's a David used to be that's because we swapped David and Tim and they removed David so that's basically everything I have to say about vectors right now in the next tutorial we're going to talk about pointers