C++ Tutorial for Beginners - Full Course

C++ Tutorial for Beginners - Full Course

SUBTITLE'S INFO:

Language: English

Type: Human

Number of phrases: 2817

Number of words: 48851

Number of symbols: 214401

DOWNLOAD SUBTITLES:

DOWNLOAD AUDIO AND VIDEO:

SUBTITLES:

Subtitles prepared by human
00:00
Hey, welcome to draft Academy. My name is Mike in this course, I'm going to be teaching you guys everything you need to know to get started writing c++, c++ is one of the most popular programming languages around. And for good reason. It's an awesome programming language, it's actually a language that is closely related to the C programming language. In fact, c++ is essentially just like the next level of the C programming language. So if you're already familiar with the C programming language, then you're gonna have no problem picking up c++. But even if you're not c++ is a great first programming language to learn. And in this course, I'm going to teach you guys all of the core concepts. For c++, we're gonna start off just with the basics, we're going to, you know, get everything set up, we'll install c++, we'll get you guys set up with a text editor, and I'll show you guys how to write your first program, then we're going to get a little bit more advanced. So we'll start looking at things like variables. And we'll work with data, we'll even write a couple of different programs and some little games. And then we're really going to get into the thick of it, we're going to learn all sorts of different programming structures,
01:01
things like loops, and if statements and classes and objects, it's going to be awesome, we're going to cover all of the core concepts, not just in c++, but all the core concepts for programming in general. So the knowledge you learn in this course will actually be able to carry over to potentially other programming languages as well. I'm really excited to be able to bring you guys tutorials on c++, it's an awesome language, it's super fun to develop in. And it's a good first language to learn if you're just getting into programming. So stick around for this course, you can kind of work through the videos at your own pace. And hopefully you guys can learn something about this awesome language c++. In this tutorial, I'm going to show you guys how to get everything set up to start working with c++. So in order to start working with c++, and started writing programs, and doing all that fun stuff, we actually have to do a little bit of setup, we're actually going to need two things on our computer. The first thing we're going to need is a text editor. And we're basically just going to use this text editor in order to write our c++ programs, any text editor
02:04
is going to work. Generally you don't want to use a text editor like Microsoft Word or Google Docs, you're gonna want to use just like a basic text editor, it could be something like Notepad, I'm going to show you guys a special text editor called an ID, which stands for integrated development environment. And we can use that text editor. It's basically just a an environment that's really awesome for writing c++ code. And then we're also going to need in addition to a text editor, we're going to need a program called a compiler, specifically a c++ compiler. And this is a program that will basically take the c++ code that we write, and it will translate it or transform it into a language that the computer is going to be able to understand. So as long as we have a text editor, and we have our c++ compiler, then we're ready to start programming in c++. So I'm going to show you guys how to get all that set up. First thing we're gonna do is head over to our browser, and I'm over here on this website called codeblocks. It's just www.codeblocks.org. And code blocks is what's called an ID. So it's an integrated development environment, it's essentially
03:07
just a special environment, special text editor that can be used to write a c++ code. So this is going to be exactly what we need. I'm just gonna click downloads over here. And you'll see there's a few options one says download the binary release, download the source code, we're going to click download the binary release. And this is going to bring us to this page, you can see there's a link over here for Windows XP, Vista, seven, eight, etc. There's also links for Linux and Mac, let's click on this windows option. And over here, you'll see there's a bunch of these different options. So what we're gonna do is we're going to download this one over here that says codeblocks 1601 min GW setup. And actually, what's cool about this is it's going to include not only the codeblocks text editor, but this is also going to include that c++ compiler that I was talking about. So we can download both of those things in this one download for code blocks. So this is the easiest way to do this as a beginner. So let's go ahead and download this, I'm just going to come over here and click on this
04:07
link for SourceForge. And this should start downloading the file onto our computer. When codeblocks has finished downloading, let's go ahead and open that up. So I'm just going to head over to my downloads folder. And we can open that guy up. You can see over here it says codeblocks 1601 m i n GW setup. Let's open this, and this should open up into an installer window. So let's click through this installer window, and we can just select all the default options. And they should start installing code blocks on our computer. So once code blocks has finished downloading, then we have everything we need on our computer to start writing in c++. And you can see over here I just opened up code blocks and the next tutorial we're going to talk about how to set up your first code blocks project and get started writing c++. In this tutorial, I'm going to show you guys how to get everything set up. To start working with and start writing programs in c++, in order to set this up, we're actually going to need two different things. The first thing we're going to need
05:09
is a text editor where we can write our c++ programs. And really, for c++, you can use any text editor that you want. So a lot of people might use like something like TextEdit, or maybe a different text editor. In our case, we're going to be using something called an ID, which stands for integrated development environment. And there's an ID, which is called codeblocks. That is specially designed for writing c++ program. So I'm gonna show you guys how to download that, we're also going to need one more thing, which is going to be a c++ compiler. And basically, this is just a program that takes our c++ code that we write, and it translates it into a language that the computer can understand. So when we want to run our programs, we want to, you know, execute the programs with the computer, we can use this compiler to translate the c++ that we write into computer code. So the first thing I want to do is show you guys how to get that compiler. And it's possible that if you're on Mac, you might already have it. But we want to do is go up here to this search bar. And we're just going to type in Terminal. And we're going to click Enter.
06:13
And this should open up the terminal. This is basically just a window or a program that we can use to interact with the computer using text commands. So down here, we want to check to see if we already have the c++ compiler installed. So you just want to type in GCC hyphen, V. And just click enter. If you have GCC, which is the C compiler we're going to be using installed and all this stuff should come up with like a version number and, and everything. If this doesn't come up. In other words, if you know it says that you don't have it, all you need to do to get this is just say Xcode, select hyphen, hyphen, install, and this is going to go off and install everything that we need for c++. So I'm just gonna click Enter. And I already have these installed. So these are like command line tools, you can see I already have them installed. But if you don't already have them installed, then this will basically just prompt you to install them. Once that installer is done running, then you can just check to make sure that you got GCC. So you can just say GCC
07:17
hyphen, V. And make sure that you have it. So once we have this c++ compiler installed, and we've run this Xcode, select Install command. Now what we want to do is get a text editor. So like I said, we're going to be using a special text editor called codeblocks. But really, you can use any text editor that you want. So I'm going to go down here into my browser, and I'm over here on this website codeblocks.org. And this is the website where we can download this program code blocks, I'm just gonna click downloads. And over here, there's a couple options, you want to click on the option that says download the binary release. So I'm going to click this. And you'll see there's this little like list here, just click on Mac OSX. And this will bring us down here. So it's basically just a zip file that contains the codeblocks application. Over here, we can download it from SourceForge, I'm just gonna click this. And this should redirect us to SourceForge and download the file for us. When codeblocks is done downloading, I'm just going to go over to my downloads folder, and we'll see what we got. So we just got the zip folder, I'm just going to double
08:20
click it. And you'll see over here we get the code blocks application. So what you should do is take this and drag it over to your Applications folder. That way OSX knows that this is an application we want to use, then you have everything that you need to start writing c++. So in the next tutorial, we're going to set up a project in code blocks, we're going to set up our first c++ file, and then we're going to run it and we'll just get everything set up and ready to go. In this tutorial, I'm gonna show you guys how to get your first c++ project up and running in code blocks. So over here, I just opened up code blocks. And this is sort of like the welcome screen that we get when we first open it. And see over here, there's a couple options. One says Create new projects. And that's actually what we're going to do. So before we're gonna start writing our c++, I'm just gonna show you guys how to get a c++ project up and running and ready to go in code blocks. So let's click Create new project. And this should open up this little window over here. So you'll see
09:21
there's all these different options. And these are essentially just different types of c++ applications that we can create. We're going to just do the bare basics. So you want to come over here and click console application. And then just click Go. And we're just going to click Next. And over here, you'll see we can select between a couple different languages, we want to click c plus plus. So I'm going to click Next. And we're just going to give this project a title. So I'm just going to call it draft. And you'll see down here I'm storing it inside of my desktop if you want to change it, you can come over here and you can put it wherever you want. Let's click Next. And you'll see over here we just have a bunch of options. You can just leave these as the Default, and we're going to click Finish. Once we click finish, then our c++ projects should have been created. So over here, in our little Tree Viewer, you'll see that we have our c++ projects. And down here inside of the sources folder, we have this file main dot cpp, and CPP is a file extension that stands for c++. So I'm just going to right click this and I'm just going to open it.
10:22
And you'll see over here we have a bunch of code that's already been included in this file. So this is essentially just like the most basic c++ file that you can write. I mean, this is what we call like a Hello World program. So you'll see down here, it's just printing out hello world onto the screen. And don't really worry too much about what this stuff up here is, really, let's just focus on this line down here, this line is actually going to print something out for us. So if I was to come over here, and I'm actually going to come up here in my editor, and I'm going to click on this build and run option, when I click this, a window should pop up. And it should basically just say, like, Hello World, you'll see down here, this little black window popped up and it says hello world. So let's just go over some basic terminology. Generally, whenever we're going to run a program in c++, we're going to build the program. And then we're going to run the program. Building the program basically means that we're taking all this code over here,
11:22
and we're converting it down into a language that the computer can understand. So the first thing we always have to do is build the file, then what we want to do is run the file, which basically means we're telling our computer to execute all of the instructions that we wrote in our program. And there's a bunch of different ways we can do that here in code blocks, you can click this little cog wheel up here, and that'll build your program. And then you can click this play button over here, that's going to run your program. But a lot of times, if you just wrote some new code, and you want to test it out, you can just click this build and run option. And for the most part in this course, we're always just going to be whenever I say we're running the program, I'm just going to click this button, and we'll build it and run it at the same time. So like I said, this is a very basic c++ file. In next tutorial, we're going to talk about like, what all this stuff is, we're going to look at writing some of our own instructions, maybe modifying these instructions. And we'll kind of dive a little bit deeper into you know, just sort of the bare basics. But for now, we have everything set up. So we have our c++ project set up, we have our first
12:22
c++ file called main dot cpp, and we're ready to start programming. In this tutorial, I'm going to talk to you guys about the basics of writing a c++ program, we're just going to talk generally about what programs are and how we can write them. And we're going to write a little program, which is going to print out a shape onto the screens, this is going to be pretty fun. Down here, I just have this basic program. And when I first created my c++ project, in the last tutorial, we sort of got this basic program. And this was kind of given to us. And this is, you know, essentially just a very simple c++ program. So I'm going to walk you guys through essentially what we have here, we're gonna talk about a couple different things, and then we'll start writing some code. So up here, we have these two lines of code. This is like hashtag include iostream. And this one says using named scape STD. Essentially, what this does is it's kind of like configuration options for our c++ file. As a beginner, like, you know, don't really worry too much about what
13:27
this stuff is or what it's doing. We're going to talk more about all this stuff later in the course. But for now, just know that you need to have it there in order for us to write our programs. Down here we have this line, it says int main. And there's an open and close parentheses. This is actually what's called a function in c++, again, don't worry too much about what a function is, I'm just kind of throwing out these words. So you kind of familiar with hearing them. But essentially, what this is, is, it's a container for us to put the code inside of our c++ programs. So this function called main is a very special function because any lines of code that we put inside of here, in other words, any lines of code that we put in between these open and closed curly brackets, is actually going to get executed when we run our program. So down here, I just have this thing it says return zero. And again, don't worry too much about what this is, just know that you need to have this in this main function. Over here, though, we have an actual line of code. So
14:27
this says c out. And then it says hello world, then it says end L and this is a basic line of code in our program. So again, any lines of code that we put in between these curly brackets for this main function is going to get executed when we run our program. So if I was to come up here and run my program, and I'm just going to click this build and run option right here, you'll see when the program runs, it prints out hello world. And you'll see this little window that opened up here. This is what we would call The console. And the console is basically just a little window that will output information. So whenever we run our c++ code, this console window is going to open up. And sometimes we can tell c++ that we want to print things out onto this console window. So if you ever hear me referring to the console, I'm just referring to this window right here. Now let's get down and start talking about programming. So again, any of the code inside this main function is going to get executed. And here, we just have one line of code. So what I can actually
15:30
do is I can copy this and down below here, I can say something else. So I could say just like my name, Mike. And now when I run this program, you'll see it's going to print out both of these things. So here on the first line, it prints out hello world. And then over here on the second line, it prints out Mike. And this is just sort of like basically how we could print something out to the console. You'll see over here it says c out and that stands for console out. And then over here, you'll see we have this little line, it says end L and this stands for end line. And basically, what this means is we're ending the line where we're going to print out text. So you'll notice that we printed out hello world. And then on the next line, we printed out Mike. So using these little print statements, I'm actually going to show you guys how we can write a little program that's going to draw a shape out onto the screen. So I'm actually going to go ahead and get rid of this text. And I'm just going to copy this guy here a couple times. So we'll make like four of these. And I'm going to print out a triangle onto the screen. So I'm just going to print a forward
16:31
slash, and then we'll do a space and a forward slash. And I'm just going to keep doing this. And you'll see we're kind of drawing this little diagonal shape upwards. Now I'm going to use vertical bars, and we're going to go all the way down. So I'm just going to keep going like this. And down here, why don't we put some underscores, and then we'll put the last vertical bar, you'll see over here, I'm actually writing out a bunch of instructions. And each of these instructions is telling c++ that we want to print out a different line onto the console. So now when I run my program, you'll see we're actually printing out this little triangle. This is a very basic program. But you'll see just by using those four simple instructions, we were able to actually draw something out onto the screen. So let's talk about this. Essentially, what's happening when we click that run button is c++ is going and it's looking inside of our program. And the first thing it's going to do is it's going to look for this main function right here. So it's going to look for this block of code. And that's basically just what a function is, it's going to look for this
17:34
block of code called main and inside of these open and closed curly brackets, it's going to execute all of the instructions inside of there. And basically, when we're writing a program, all we're doing is we're just telling the computer a bunch of instructions. So we're giving the computer a bunch of instructions that we want it to carry out. So imagine that, for example, in real life, like you were using a recipe or something, a recipe is a lot like a program, a recipe has a list of instructions. And if you follow those instructions correctly, then you end up with like something delicious. That's basically what a program is a program is just a collection of instructions that we're giving to the computer. And we can give the computer simple instructions like this. And we can do something simple, like draw out a shape. But as we go through this course, and we learn more and more complex instructions, and we learn how to use them together in unison with each other, we're going to be able to tell the computer to do a bunch of complex things. So the next thing I want to talk to you guys about is the order that these instructions get executed. So just like if you were following
18:38
the recipe, and you would start with the first instruction, and you'd go down to the last instruction, the computer is going to execute these instructions in order. So it's going to start with this instruction. And basically here we're just telling the computer over telling c++ that we want to print this line of text out onto the screen. So c++ is going to do this. And then once it's done with that, it's going to move on to this instruction, it's going to print this out to the screen onto the screen, etc. It's going to keep doing that. So actually, let me show you guys if I was to get rid of this line, and I was to put it up here. Now you'll see when we run our program that we're going to print out a funky looking shape. So instead of this base being down at the bottom here, we're going to print it out on the top. And that's because we change the order of the instructions. So again, all a program is it's just a set of instructions that we give to the computer. That's it, it's very simple, the more complex instructions that we can give the computer and the more ways that we're able to combine those instructions in different ways, the
19:40
more complex Our programs are going to be. So as we go through this course, I'm going to be teaching you guys all sorts of more complex instructions. We're going to learn different ways to do different things and will use real world examples to build little applications. And you'll end up learning a bunch along the way. And this tutorial, I'm going to talk to you guys about using variables in c++. A lot of times when we're writing programs in c++, we're going to be dealing with all different types of data and information. And a lot of times when we're dealing with that data and information, it can be kind of hard to keep track of. and a variable is basically just a container where we can store different pieces of information or different data values in our programs. And it makes it a lot easier for us to manage and maintain and use that data. So I'm going to show you guys an example. And we'll show you guys basically how variables are useful and how we can use them in c++. So down here, I have a very basic program that I've written out, you'll see
20:41
down here, I'm basically just printing out a bunch of lines of text, it says, There once was a man named George, he was 70 years old, he liked the name George, but did not like being 70. So this is basically like my little story here. And you'll see I can run my program. And this is a valid program in c++, it basically just prints out the story. And we have all of our information. So you know, this is a pretty nice c++ program. It serves its purposes. But let's say that I'm looking at my story. And I'm thinking, Hmm, maybe I want to change the character's name, right. So maybe I don't like the name, George, maybe I want to change it to a different name. So what I could do is I could go through and I can manually change it in each line of code. So I could come here where it says George, and I could change it. Let's say we want to change the character's name to john, right, so I can change it to john. I'll keep looking through the story down here. It also says George, so I'll change it to john. And there we go. We've changed the character's name, we've officially updated it. Let's say maybe now I'm thinking to myself, I think I want to make the character a little
21:42
bit younger, instead of 70. Why don't we make john 35. So I can do the same thing, I can go in and manually change the value of 70 to 35. So we can come over here, and we can say he was 70, which changes to 35. And we'll say did not like being 17. Okay, so we'll change that to 35 as well. So now we've officially updated our store, we've changed the character's name, and we've changed the character's age. Here's the problem though. In order to make those changes, I had to manually go through and individually edit each one of the places where the characters name showed up or the characters age showed up. And imagine that instead of having a story that was only four lines long, I had a story that was like hundreds of lines. And we mentioned the character's name hundreds of times, and we mentioned their age hundreds of times, well, all of a sudden, it becomes a lot more difficult for me to manage those pieces of information. So it's difficult for me to manage the character's name and the character's age. Right, if I had a story that was hundreds of lines long,
22:45
and we mentioned the character's name 100 times having to you know, go and modify and update that name would be a very tedious task, because I'd have to go through and essentially just do it manually. This is where something like variables come in. A lot of times in our programs, we're going to have different pieces of information, different data values that we want to keep track of, and we want to be able to manage. So what we can do is we can take those pieces of information, and we can put them inside of containers called variables. And variable. Like I said, it's just a container where we can store a piece of data. And we'll make it a lot easier for us to use and manage that piece of data in our programs. So I'm gonna show you guys how we could create a variable that can store the characters name and the character's age. And you'll see why this can be useful in something like this. So over here, I'm going to create a couple of variables. When we create a variable in c++, we actually have to tell c++ a couple things. The first thing we have to tell c++ is what type of information we want to store inside of the variable. Now, in the next video,
23:49
I'm going to talk to you guys all about the different types of data that we can use in c++. But for now, I'm just gonna show you guys two types of data. The first type of data that we can store inside of a variable is called a string. And that basically means it's a string of characters. So it's like plain text. So this down here, where it's saying There once was a man named john, this is a string, right? It's plain text in our program, a lot of times we're going to be dealing with strings. So I'm going to create a string variable. In other words, I'm going to create a container that can store a string value. So I'm just going to say string. And that's the first thing that we have to tell c++. The next thing we have to do is give this container give this variable a name. So what we want to do is give this a descriptive name, which will basically tell us what is inside of the variable. So I'm just going to call this character name just like that. And what I can do now is I can give this a value so I could say character name is equal to and now we can type in the character's name so I could say, john, just like that.
24:53
Alright, so once we've created this character name value variable now this value, john, this string value is now stored inside of this character named variable. The next thing we're going to do is create another variable to store the character's age. In addition to storing data in the form of a string, we can also store numbers. Now age is a whole number. So what I can do is I can store it inside of something called an integer. And an integer is basically just a whole number. So I can just say int, and I can just call this character age. Now I want to show you guys another thing that we can do. So up here, what we did is we said string character name, and we set it equal to a value right away. But what I could also do is put a semicolon here. And also I do want to point out, whenever we're writing lines of code in c++, every time you finish writing a line of code, you want to put in this semi colon. I'm not sure if I mentioned that in the last video. But the semicolon basically tells c that we're done with that line of code. So this separates
25:57
one line of code from another. So you need to always make sure you put these semicolons. But with a variable, what I could do is I could say int character name, I could do the same thing for string. And then I could go on to a new line. And I could give this a value. So I could say like, character age is equal to and now I'm just going to type in a number. So we said that john was going to be equal to 35. And you'll notice when we use numbers, we don't have to surround these with quotation marks, we just can type out the number. So now we have two variables, and I showed you two different ways that we can create them. And what we can do is we can use these variables inside of our story. And you'll see how this makes it a lot easier for us to maintain this program. So what we want to do is we want to replace every instance of the characters name and the characters age with the variable. So instead of just printing out john here, I want to refer to this variable. And the way that we need to do that is we basically need to include this variable. So over here, I'm printing out this string of text, I'm printing out a bunch of
27:01
plain text, right. But let's say instead of just printing out john here as plain text, I want to instead print out the value that was stored inside of the character name variable, what I can do is I can just get rid of john. And I can say less than sign less than sign. And what this is basically going to tell c++ is that we want to take the value that we're going to type out here, so I'm just going to type out character name. And it's basically telling c++ that we want to take this value, and we want to insert it right here, inside of this line of text. So when I go ahead and run my program, now, you'll see that we're still printing out there once was a man named john. Except now I didn't actually type out john, all I did was include this variable right here. And I basically just said that we want to put the variable right in there. So that's how we can include a variable inside of one of these print statements. So over here, I'm going to do the same thing. So I'm
28:02
just going to say less than less than character name. And now this is going to insert the character name. In other words, it's going to insert the value stored inside of the character name variable. at this position, we can do the same for the age. So over here, we have the characters age, I'm going to get rid of that. And I'm going to say less than less than, and now, character age. And so the value inside of the character age variable is going to get placed right in there. And we have one more place where we have the characters age. So over here, I'm going to get rid of this. And now this is going to be special. So you'll see over here, we want to insert the value inside of the character age variable, right in between all of this text. So I want to put it right here, what I can do is I can make two quotation marks. And essentially what this is doing is it's saying this is going to be its own string of text. And this is going to be its own string of text. And I can make to less than signs, I can type out the character age variable. And then I'm
29:03
going to make two more or less than signs. And this is essentially just going to string all of these together. So it's gonna say, print out this text, then print out the value inside of the character age variable, then print out this text. So I've now replaced every instance of the characters age, and every instance of the character's name with those variables. Let's run our program and see what happens. Over here, you'll see we have the same exact story as we did before, there was a man named john, he was 35 years old john 35. So without having to manually do anything, we were able to include those values. And now what's cool about variables is if I wanted to update the character's name, or update the character's age, all I have to do is change it up here in one spot. So if I wanted to change the character's name to Tom, and I wanted to make Tom let's say 50 years old, I only have to modify the values that Getting stored in the variables in, they'll automatically update down in our story. So now when I run my program, you'll see it's
30:07
using the name Tom. And it's saying that he's 50 years old. So that's kind of an awesome way that we can use these variables. Another cool thing that these variables allow us to do is modify the value. So let's say that halfway through our story, I wanted to change the character's name. So halfway through, I wanted to make the name be a different name, all I have to do is say, character name. And I can actually assign this a different value. So I could give this the value of Mike. And again, I'm going to need a semicolon here at the end of this line of code. And now you'll see halfway through the story, the character's name is going to change. So it says There once was a man named Tom. And down here, it's using the name, Mike. So not only was I able to just use those variables to insert these values, but I can actually modify those variables at different places in my program. So that's pretty awesome. Now, this is just sort of like the bare basics of variables, variables are containers, they allow us to maintain and keep track of the data and the values
31:11
in our programs a lot better. And they also give us the advantage of only having to assign a value once so I can assign a value once up here. And then I can use it and refer to it in different places down here. I can also modify those values in different places in my programs. So in this tutorial, we talked about storing values as strings of text, and as integers, which are whole numbers. In the next tutorial, I'm going to show you guys all of the different types of information and the different types of variables that we can create in our programs. In this tutorial, I want to talk to you guys about the different data types in c++. a data type basically just means a type of data or a type of information that we can use and work with inside of our c++ programs. So there's all different types of information, we can store like text, different types of numbers, decimal numbers, true false values, all sorts of stuff. And I'm gonna kind of walk you guys through what all of those are and how we can use them. So the easiest way for me to demonstrate this is
32:14
just going to be to create a bunch of different variables. So any of the different types of data that we can work with in c++, we can actually store inside of variables. So I'm just going to create a couple of different variables and kind of show you guys exactly how this is going to work. So the first data type I want to talk to you guys about is called a character. And a character basically allows us to represent one single character. So the way we can create a character variable is just by saying CH, ar. And now I want to give this a name. So let's just call it like grade or something. And whenever we create a character, we're going to use these single quotation marks. So I can store any like regular character that I would want inside of here. So you know, essentially any character that you can think of, you could store in here, and then we're going to include this semicolon. So that's basically like the character data type. And in addition to just storing one single character, there's going to be a lot of situations where we're going to want to store like more than one. And what we can actually do is use something called a string. And a string is basically
33:18
just a string of characters. So it's, instead of just being one single character, it's like a bunch of different characters. So this would be like, essentially just plain text that we would see in a program. So I could just say string. And we could just call this like phrase. And when I create a string, I can use these double quotation marks. So I can say, like, draft Academy or something. And now we're going to be able to store instead of just one character, a bunch of different characters. So there's different situations where you might want to use either like a char or a string. But for the most part, I think strings are probably a little bit more commonly used than chars. So instead of just plain text, we can also store and work with numbers. So essentially, there's two different types of numbers are two basic types of numbers. There's whole numbers, and then there's decimal numbers. Some people will also call those floating point numbers, a whole number is like a counting number. So think like 12345, right? There's just whole number of solid numbers, there's no decimal points, right? Then we have decimal numbers, so it'd be like
34:21
0.5, or 1.267, or 10. Point 11. You know, basically a number that has a decimal point after it. When we work with these different numbers, c++ is actually going to distinguish between them. So the first type a number we can work with is an integer. So I could just say int, and this could be like an age or something. So whenever we're creating a number, we can just type out the number so I could say like 15. And you'll notice I don't need any quotation marks. I don't need anything special surrounding this, I can just type out the number. In addition to positive numbers. You can also use negative numbers, but anytime we're using an integer, you can't have a decimal points, he can't do anything with decimals, these are just going to be solid whole numbers. If you want to work with a decimal, you have two basic options. So there's two data types that represent decimals in our programs. The first is called float. And this basically just stands for like floating point number. The second one is called double. Now you'll hear different people talk about these different
35:25
data types, the main difference is just how many decimal points they can store. So a double can store more decimal points than a float. So if you need a number to be very specific, as far as like how many decimal points you can take it to, then you definitely want to use a double. And I would say for the most part, as a beginner, just only worried about doubles, floats will be used more in specific circumstances. But you know, as you're just learning this language, really just worry about double. So we can just say double like GPA. And I can set this equal to like 4.5, or like 2.3. Basically, I can make this any decimal number that I wanted. Keep in mind, you could also do like 2.0. So it doesn't have to be like a different number decimal. And again, you can make these negative, that's no problem. So ants are going to be what we're going to use for whole numbers, for the most part doubles are going to be what we're going to use for decimal numbers. So that covers text and numbers. And just with those two data types. With text, we can use characters
36:25
and strings and an instant doubles. You can represent like just about any type of information in your programs. But c++ is awesome. So they're actually going to give us another data type, which is called a Boolean. And a Boolean is maybe not as intuitive as, like text and numbers. A Boolean is actually what we would call a true false value. So when we're writing our programs, there's actually going to be a lot of situations and circumstances where we want to represent true or false data. And a Boolean is just a special word for true or false. Right. So I could say B o L stands for Boolean, and we could create a variable like is male, right, so this variable is male is going to store a true or a false value inside of it. And this will basically tell us whether or not someone is male. So in my case, I could say true, because I am a male, right. So you'll see how this can kind of come in handy for different things like, we could say something is true, or we could say something is false. And that allows us to represent a certain type of information, I could also say false
37:27
over here. And that's going to be the opposite. So these are going to come in handy a lot these true false values. And we can use Boolean to represent them. So for the most part, these are the basic data types. Now there's a couple other data types that we could also get into. But I think 99% of the time, as a beginner, as somebody who's learning c++, you know, don't concern yourself with anything that you don't have to so Boolean doubles, which are just decimal numbers, and which are whole numbers, strings, which is plain text, and chars, which are just single characters. That's what we're going to be working with. So as we go forward in the course, we're going to be working with all this different type of information. Now, I want to point out one more things, I'm actually just going to make a little print statement here. And you know, this is the basic statement. So if I wanted to, I could print out any one of these variables, like if I could print out a grade, for example. And this is going to go ahead and print that out onto the screen. You see over here, we're
38:29
just printing out a, but we don't have to store this information inside a variable. So if I wanted, I could just type in a string down here, right, I don't have to store in a variable. And this is what we would call a constant. So I don't need to store it inside of a variable, I could also you know, type out like false. Or I can type out a number like 4.5. Or I could type out an integer. Or I could type out a character like, you don't have to put these things inside of variables, a lot of times you can just use like straight up like that. And this is what we would call a constant. But a lot of times you're going to want to store information in variables because it's really useful. And obviously when you do that, you have to declare the data type and you have to tell c++ what type of data you want to work with. In this tutorial, I'm going to talk to you guys about the ins and outs of working with strings in c++. And the c++ programming language, one of the most common types of data that you're going to be working with are going to be strings and
39:30
strings are basically just plain text. So any type of plain text we want to represent or work with in our program is going to be considered a string. So down here, you'll see I have this little program set up basically just printing out a couple lines. So the way I can create a string is just by using an open and closed quotation marks like this. So I can basically type out whatever I want. If I wanted to, I could type out draft Academy. You'll see now I'm printing out draft Academy and Hello. If I was to run my program, you'll see that we Drop Academy and Hello. First thing I want to show you guys is doing has to do with this see our line of code over here. So you'll notice here it says end l right here. And basically when we put end l right there, it tells c++ that we want to print a new line after we print whatever is in here. So if I was to get rid of this, and I just said, See our draft Academy. Now, what you'll see is that Hello is going to get printed on the same line as draft Academy, because we didn't put an L. So we didn't put a new
40:31
line. If I wanted to, though, I could actually manually come in here and specify a new line by saying a backslash n. So backslash n basically means that we want to print out a new line. So this inside of a string represents the new line character. So now when I run this program, you'll see that Hello gets printed out on a new line. So that's how we can use that backslash n inside of our string in order to do essentially the same thing as and L is doing. And if I wanted, I could put this like in between these two words, and it would put them on a new line. So in addition to just printing out a string, like we did over here, I could also store a string inside of a variable. So in order to create a string variable, I can just say string, we'll give it a name. So we could just call this phrase, and I could set it equal to whatever I want. So let's set it equal to draft Academy. And then I can do exactly what I did down here. So here, I can just print out phrase. And now it'll be printing out draft Academy onto the screen, just like that. So in addition
41:31
to just printing out strings, and kind of working with them that way, storing them inside of variables, we can also use what's called string functions. Now, a function is something that we're going to get into more later in the course, we're going to write our own functions. But for now, just know that a function is basically like a little block of code that we can call, which will perform a specific task for us. So these functions do all sorts of things. And there's a lot of functions that we can use with these strings. And they're called string functions. So these functions will either like modify the string, or they'll give us information about the strings. So I'm going to show you guys a couple. So the first string function that I want to show you guys is the length function. And all I have to do to use this is I can just come down here. And I'm actually just going to print out the result of using these functions. So I'm going to put it down here, I'm going to say phrase dot length. And I can make an open and close parentheses. And generally when we're calling a function and see, we're going to use this dot, and then we're going to type the name of the function. So this is the length function. And we're going to
42:33
make an open and close parentheses like that. So now if I was to run this program, you'll see we're getting 15. So this is basically just telling us how many characters are inside of this phrase, strings, how many characters are inside draft Academy. Now, if I wanted to, I could actually access individual characters inside of here. So let's say that I wanted to access just this G. So I wanted to print out the first character in this string, we can make an open and closed square brackets just like this. And inside of here, I can put zero and zero is going to refer to this first character in the string. So now when I run my program, you'll see we're just printing out this capital G. If I wanted to access this R, for example, I could say two, and two is going to refer to this R. So now when I run my program, we're printing out our as you can see over here. So if you haven't caught on yet, whenever we're indexing a string, we're starting at zero. So if I was to assign index positions to each one of these characters in the string, I would say that g is an index
43:37
position zero, I is an index position one, R is an index position two, a three, four, etc. So whenever c++ is indexing a string, it always starts indexing it at zero, so it'll start counting basically 01234. Even though g is the first character in draf Academy, it's technically at index position zero. So whenever we're using something like this, we're referring to like a specific character, we want to refer to the index position, which is going to start at zero. Another thing I could do is I could actually, like modify a specific character in a string. So I could say like phrase, and I can refer to an individual character inside of this string. So we could say like phrase zero, I could assign this a new value. So I can assign this the value of a new character. So you'll notice I'm using these single quotes, so I could say like, B. And so now, instead of saying, gee, Ira FFP, it's going to say, Be IRA FFV because I'm essentially changing one of the characters
44:37
in this string. So now when we print this out, you'll see it says burapha Academy instead of draft Academy, so that can be kind of handy just to modify a specific character inside of a string. We could also find out information about this string. So I showed you guys how we can find out the length of the string But imagine if we wanted to find out whether or not a specific string or a specific character was inside of this string, I could say phrase dot find. And I can make an open and close parentheses. And actually, inside of these open and close parentheses, I'm going to give this find function a couple of different pieces of information. So I'm going to give it some information for it to perform its task with. And these are called parameter. So anytime I give a function, different pieces of information, we call it passing parameters. Or you also hear people call them arguments. So passing arguments, or passing parameters, basically, it's just information that we give to this function. So this fine function, I need to give a couple pieces of information, the first thing I can do is give it a string or a character that I wanted to find. So let's say I want
45:41
it to find Academy. So basically, I want to check to see if the word Academy is inside of this phrase string up here. The next argument, I want to give this an X parameter is going to be an integer. And it's going to be the index where I want to start looking. So if I wanted to just check to see if it was in the string, I could say zero, if I wanted to check to see if Academy occurred after the third index, then I could say three, let's just say zero. So now, this is actually going to give us back in number and it'll tell me at what index position inside of this string Academy occurs. So now I'm going to run my program, you'll see over here, it's giving us an eight back. That's because academies starts at index position 012345678. So Academy starts at index position eight. And that's why it gave that back to us. So I could do this with anything I could say, like FFP, or something. And now this will tell me where FF II starts inside of this string. So it starts at index position four. So that can be pretty useful. And it's a really useful
46:44
way to find the figure out of different things, or different strings show up in this string you're working with. There's one more string function that I want to show you guys, which is called substring. So it's just phrase.su. b str. And this is also going to take two parameter. So we're going to give this two pieces of information. The first piece of information we're going to give this is a starting index. Now substring allows us to take, you know just part of this string, so I could specify that I want to take like all the characters after this a or I can specify that I wanted to take like, from index position zero from index position one all the way to the end, basically, it'll allow me to take like just a subsection of this string. So the first parameter, I want to give this as a starting index, so I could say like eight. And so basically, this is going to start grabbing a new string at index position eight, then I can give this a link. And this is basically going to tell substring, how many characters I want to grab. So if I said like three, this is basically going to start at index position eight. So it's going to start up here at this
47:47
a, and it's going to grab three characters. So it's going to grab this A, this c and this a. So now when I print this out, you'll see we get a CA just like that. And that's basically what we do. So this is the starting index, and this is the link. And one of the cool things we could do is we could actually take this and store this substring in another string. So I could say like, string, phrase sub, just like that. And then I could come down here and I could say phrase sub is equal to, and then what we just had before, so phrase dot substring. And now if we print it out phrase sub, it'll print out ACA. So I'm essentially storing the value of the substring in another string. So that can be pretty useful. And that's really the basics of working with strings. I mean, obviously, there's certain things that I left out, you know, I could spend an hour just talking about all the different things we could do with strings. But I think that kind of covers the basics kind of shows you all the basic things that you can do, we looked at some basic functions, we talked
48:50
about grabbing individual characters, we talked about string indexes. So I think for now, that should be a good introduction into working with strings in c++. In this tutorial, I want to talk to you guys about working with numbers in C. Now whenever you're writing programs, and C, one of the most common types of data that you're going to be dealing with are going to be numbers. So these can be things like whole numbers or decimal numbers. Basically, I'm gonna give you guys a full overview of how to work with numbers, we'll talk about the basics, we'll look at how we can use different mathematical functions in order to do different mathematical operations with our numbers. So this is gonna be a pretty cool tutorial. Now, down here, I'm just going to talk to you guys about the basics. There's really two types of numbers and see that we deal with whole numbers and decimal numbers. And whole numbers are basically referred to as integers. And decimal numbers can be referred to as two things either floats or doubles. And essentially, the only thing you need to know about that as a beginner is that doubles allow you to store more specific decimal points.
49:56
So the double you can store you know, potentially more decimal points and you In a float, and there's more differences. But if you want to get more into that you can kind of look it up. But the basics of using numbers as you just type them out. So if I wanted to, for example, print out the number 14, you see I have this C out here, I can just type it in and we can print it out, we can work with it. In addition to positive numbers, we could use negative numbers, if I wanted, I can make this a decimal. So really, numbers are very simple, you just kind of type out the number. But we can also do things like math. So for example, I could say like five plus seven. And in addition to printing out five and seven, this is actually going to do this math operation. So this will actually print out the result of five plus seven. So you can see we get 12 over here. So we can use addition, we can also use subtraction, we could use division, which is going to be this forward slash, and we can use multiplication, which is going to be this Asterix, so if I was to multiply these two numbers, now you'll see over here we get 35. So those are the four basic math, you know operations. So that's going to work really well. Also, one other
51:00
thing I want to show you guys, which is called the modulus operator, and the modulus operator will basically give us the remainder of dividing two numbers. So if I said like 10, and then I made this percent sign, and actually this is read 10 mod, and then I said three, so we would read this 10 mod three, what this is going to do is it's going to take 10 divided by three, and then it's going to give us the remainder, so this module is operator will give us the remainder, so 10 divided by three is going to be three with a remainder of one. So now we just we should just get one. And you can see over here we get one. So sometimes that modules operator can come in handy, you can also represent order of operations in C. So C is going to adhere to like the normal or reparations, like p DMS, I think it is Please Excuse My Dear Aunt Sally, basically like multiplication and division are going to come before addition and subtraction. So if I said for example, like four plus five times 10, this is going to do five times 10 first, so it's going to be 50 plus four, so
52:06
we get 54. But if I wanted to do the addition, first, I could just put parentheses around this. So now it's going to do five plus four, nine times 10. So now we should get 90. And you can see we do so if you need to separate order of operations. I mean, it's essentially just following basic math order of operations rules. But you could represent those like that. So in addition to doing all that stuff, and just printing out numbers, we can store numbers inside of variables. So I can make an N, we'll just call it like w nom for whole number. And we'll just make this five. I could also use like a double. And this will be like de nom for decimal number. And this would be like 5.5, right? I mean, now we're storing these numbers inside of variables. If I wanted, I could just, you know, print them out. Naturally, like we did down here, I want to show you guys one cool thing we can do though, which is incrementing, a number that's stored in a variable. So I could say like w num. And I could say plus plus. And what this is going to do is it's going to add one on to W num. So now when we print out w num instead of just being five, it's going to be six because
53:09
we're adding one to it. So you can see we get six. And that's a shorthand that'll come in handy a lot. There's a lot of situations where you want to increment a value. You could also do like minus minus, and that will subtract one from it. You could also do like w plus equals, and we could say like 80. And so what this is going to do is it's going to take w nom and it's going to add 80 to it. So now we should get 85. And you see we do, you can do plus equals multiplication equals minus equals, I think you can do division equals, and all of that will it's just basically shorthand so you don't have to type out all that stuff. So now that we kind of looked at all the different operators, let's talk about how decimal numbers and integers work together. So here's a little experiment. Let's say I came down here and I added 5.5 plus nine, so I'm adding a decimal number. And I'm adding it to a integer number, right, so let's see what happens over here, you'll see that we're going to get a decimal number back. So anytime we're doing math between a decimal number like a double or a float,
54:12
and an integer, a whole number, we're always going to get a decimal number back. So it's always going to give us the decimal back. But it's important to note though, if I did math with two integers, so for example, let me show you guys if I was to say like 10 divided by three, and these are both integers. Keep in mind, I'm actually going to get an integer number back. So we're going to get like three back because that's technically the answer. But here's the thing. This isn't actually the answer. Really what it is, is it's three with a remainder of one. But because we did the math with two integers, we're going to get an integer value back. If I was to make one of these a decimal number or even if I made both of them a decimal, a decimal number. Now we're going to get back The actual like full answer. So it's going to be three with three repeated just like that. So you can see if we do math between just two integers, we're always getting an integer back, even if that's not like fully the correct answer. And different circumstances, you're going
55:15
to want to do that in other circumstances you're not, but just keep that in mind. So that's kind of how integers and decimal numbers interact with each other. So now what I want to do is show you guys how we can use different math operations. So it generally just with math, there's all sorts of different, like, I guess, operations, you can do like square root, you can take a number to a power, we can round a number. And in C, there's actually these things called functions, which can do all that stuff. For us. A function is basically just a collection of code that we can call that will perform a specific task. And we're going to talk more about functions later. But for now, I'm going to show you some basic math functions that we can use. In order to use these math functions, I actually have to do something called importing them. And essentially, when we import something, we're basically going we're telling c++ that we need to go out and grab code from other files. So up here, you can see we're using this include statement, and we're grabbing something called iostream, I'm going to add another line here, we're just going to put a hashtag, we're basically
56:16
just going to copy this guy up here. And we can open and closed greater than less than sign. And in here, I just want to type in C math. And basically, what this is going to do is it's going to tell our c++ program that we want to use some math functions, that's kind of all you need to know at this point, just know that you need to put this up here in order to follow along with what I'm going to be doing. So down here, we can now use a bunch of different math functions, essentially just math operations. So for example, I could say like POW. And what this will do is it'll take two arguments. So in here, I could pass two numbers, like I could pass a two, and I could pass a five. And what this will do is it'll take two raised to the power of five, and it's just going to print that out. So when I run my program, you'll see we're getting 32. So 32 is two raised to the fifth power. And you can kind of do that with any number. So I could also say like three raised to the third power, so three cubed, and now we should get 27, which we do. So that POW function
57:19
can be pretty useful. There's another one square root Sq RT essentially doing the opposite. So we can say like square root 36. And now this is going to give us the square root of 36 back, which is going to be six. So that can be pretty useful. And inside of these functions, we you can put a decimal numbers too. So I can put both integers like whole numbers and also decimal numbers. There's another one which is called round, which is going to round a number. So if I put like 4.3 inside of here, this will return the rounded number. So you see, we just get four and this will follow normal rounding rules. So if I change this to 4.6, now we should get five back. There's a couple other these functions, which will similarly like round decimal numbers, I can say CIO, and what this will do is it'll automatically just round the number up. So even if this was like 4.1, this will round the number up to the next highest whole number. So you'll see here we get five, you can do the opposite, which is floor. So if I just say floor here, if I put a 4.8 in here, normally,
58:25
it's supposed to round up, but now it's going to round down because we're using that seal or that floor function. So those can be pretty useful. And there's one more I want to show you guys, which is called f max. And f Max is going to take two numbers. So I can pass in like a three and a 10. And this will tell me which one's bigger. So this will return back to us the bigger of the two numbers. So now when I run this program, you'll see it's giving us a 10 back because 10 was the bigger number and a lot of circumstances, in C, you're going to have two numbers, you might not know which one is bigger. So this can be really useful to tell us. And you could also use f min, which will do the opposite. So this will tell us what the smallest number that we passed in was. And you can see we get three over here. So those are some basic math functions. There's a lot more if you just go online and search c++ math functions, like you'll find a huge listing of all them that you can use. There's things to do like sine, cosine, tangent, you can do like logarithmic stuff you can do, you can use like exponentials all different stuff like that can be really
59:29
useful. But that's kind of the basics of how you can use that. And you know, really, numbers are extremely useful. And that's kind of been like a broad overview of sort of all the stuff you can do with them. In this tutorial, I'm gonna show you guys how to get input from a user in c++. A lot of times in our programs, we're going to be working with all different types of information. But one of the most important types of information is going to be information that the user inputs. So a lot of times in our programs where Want to allow the user to input information. And then we're going to want to use that information in order to do different things. So in this tutorial, we'll just give a broad overview of how that's done, we'll talk about how to get different types of information. And you know, you'll kind of learn everything you need to learn. So down here, whenever we're getting information from the user, the first thing we always want to do is store that information somewhere. So whenever I'm asking the user to give me a piece of information, if they give me that information, and I don't put it anywhere, like it's kind of useless, right,
01:00:31
so we usually what we want to do is create a variable. And I'm actually going to write a little program that will allow the user to enter in their age. So I'm going to create a integer just called age. And I'm not going to give it a value. So I'm just going to put a semicolon there, we're essentially declaring the integer. So we're telling c++ that we want to use this integer, but we're not giving it a value, we're going to let the user give this variable a value. The next thing I want to do is I want to prompt the user to enter in something right. So if I don't give them a prompt, if I don't tell them what information I want, then they're not going to know what they should input. So I'm just going to say C out. And I'll just print out a little prompt, so we'll just say, enter your age. And we're basically just prompting them to enter in their age. Alright, so once we've created the variable, and once we've prompted them to enter in the information, now what we can do is we can actually get the information from them. So the way that we get information is actually the opposite of the way that we print out information. So instead of saying
01:01:31
c out, I'm just gonna say C n, and I'm gonna make two greater than signs. So when we use C out, we use these lesson signs, when we use C, and we use greater than signs, and that's really important. So over here, I'm just going to type in the name of the variable that I want to store what they enter inside of. So I'm just going to put age here, because I want to store whatever they input into this age variable. So we're assuming that they're going to enter in an integer number, and I'm going to store that integer number inside of this integer that we created. Now, what we can do is we can just print out something. So we'll print out a little message to them, basically saying like you are, and then we'll say like age, or we can say like, years. Old. Cool. So now we're basically just asking them to enter in their name, we're taking whatever they input, and we're storing it inside of this age variable, we can do that using the C in command here. And then we're just printing
01:02:32
out You are the age years old. So let's go ahead and run our program. And we'll see how we did. So I'm going to build and run. And you'll see here it says enter your age. So we can just put like 30. And now when I click enter, it's going to take that integer that we entered in 30, it's going to store it inside of the age variable, it's going to print out the prompt. So when I click enter, you'll see it says you are 30 years old. So that's awesome. So we can do that and we can get an integer, I could also get a double. So if I made this a double, it would be the same thing. So here, I can run the program. And I can enter in like 4.5. So maybe someone's like four and a half years old. And here they're entering in 4.5. So it's gonna work the same exact way, you could also do the same thing for characters. So if I made this a sharp, here, it'll be the same. So we'll be able to get a character from them as well. So now when I run the program, I could just enter in like a G, for example, whatever, obviously, that's not the age, but it does the same thing.
01:03:33
So it's able to take in that character, and it's able to use it. Now, here's the thing, that's how we can get characters in numbers. But if we want to get a string of text, we're actually going to do something different. So if I wanted to get a string, I'm actually not going to use this C in command. So I'm gonna show you guys how we can get a string. So instead of entering in the age, why don't we have them enter in their name. So over here, I'm going to make a string, and I'm just going to call it name, instead of using C and I'm going to use another command, which is called get line. And get line will basically allow us to get an entire line of text. So instead of just getting like one number or one character, we're going to get like the entire line that the user enters. And in here, we need to pass this a couple of different parameters. The first is going to be c n. And c n is basically just that little like command prompt. So like whenever we enter stuff into that command prompt, that's basically what this is saying. And then we want to enter in the name of the variable where we want to store the line of text that we get. So
01:04:34
I'm just going to store inside of name. So over here, we can just say hello to the user. So I could say hello. And it's going to be name. So now we'll be able to get the name from the user. So we're basically get the entire line. So for example, I can come over here I can say like john smith, and now when I click enter, it says hello, john smith. So that's how we can get strings of text. And that's really the difference is if I wanted to get like an integer and store it inside of an integer variable, if I wanted to get a char and store it inside of a char variable, I have to use C in. But if I want to just get like a string of text, then I can use this get line function, and they're both going to be useful. So that's kind of an overview of how we can get input from the user. And you can get as many values as you want. And so you know, you can essentially like copy this whole line, paste it a bunch of times below there, and get, you know a bunch of different pieces of information and use them. In this tutorial, I'm going to show you guys
01:05:36
how to build a basic calculator in c++. Essentially, we're going to build a calculator where the user can enter in two numbers. And then we'll add those numbers together and will spit out the answer. So this is going to be kind of cool. And it'll show you guys a little bit more about getting input from users. So down here, the first thing we want to do before we do anything is create two variables, where we can store the two numbers that we want to add together. So I'm going to create an integer. And I'm just going to call it num one. And I'm not going to give this a value because we're actually going to let the user give this variable a value, I'm going to create another one called num two. And same thing, actually, let me show you guys something cool, we can do it, we're creating two variables like this. And they're the same data type, I can actually put them on the same line. So I could say num, one, comma, num, two. And a lot of people find this to be really useful. So for example, I could do like as many as I wanted. In our case, we're just going to have two numbers, though. So now that we've declared our two number variables,
01:06:36
the next thing we want to do is prompt the user and get some information. So I'm going to say C out. And the first thing we'll do is prompt them for the first number. So I'll say enter first number. And now we want to actually get the first number from the user. So I'm going to say cn, and over here, I'm just going to specify num one. So we're going to store the number that they enter inside of this num, one variable, I can actually just copy this. And we're going to do the same thing down here. So I'm just going to say enter second number. And now instead of num, one, it's going to be num two. So at this point in our program, we should have both of those numbers, we should have gotten both of those numbers from the user, the last thing we want to do is just print out the answer. So we can actually just say, C out. And I'm just going to add number one and num two together. So we should be printing out the result of adding those two numbers together. So let's go ahead and run this program. And we'll see how we did. So I'm going to build
01:07:39
and run. And actually Whoops, I put the wrong direction for these arrows here. So this is probably a really common mistake with C and we want to make to greater than signs not to less than signs. So that's something that could easily trip up a new user. And it tripped me up. So when we're using C and we're using these two greater than signs when we're using C out we're using these two less than sign. So that's my mistake. But I guess that does kind of highlight a common mistake for beginners in c++. And even for someone like me who's programmed in c++ a lot before. Alright, so now let's run our program. And it says enter first number, so enter in a five, enter second number entered and a 10. And now when I click enter, it should add both these numbers together. So it does and we get 15. So that's pretty cool. Another thing we could do is instead of using integers, we could also just use doubles. And that's as easy as just changing these variable declarations. So now when we run the program, we can work with double. So I could enter in like a four, I could also enter in like a 9.8. And now we'll be able to add those guys together. So that's kind
01:08:42
of cool. And really, that's a basic calculator. So essentially, we're declaring the variables up here, we're using C out. And with C out remember, we're using these less than signs, then we're using cn with the greater than signs. And this line is basically getting the use the input from the user. And it's storing it inside of this variable. We're doing that twice. And because we store these as doubles or before integers, we're able to add them together and print out the answer. So that is the basics of building a calculator. Actually, later in the course, I'm going to show you guys how we can build an even cooler calculator that will be able to add, subtract, multiply and divide, and the user will get to decide in order to build something like that. We're gonna have to learn some more stuff. So stick around, and we'll learn how to do something like that later in the course. In this tutorial, I'm gonna show you guys how to build a little mad libs game in c++. So a Mad Libs is basically a little game where you enter in a bunch of random words could be like nouns, verbs, adjectives, and then all those words get taken and get sprinkled
01:09:45
into a story. And generally since you entered in random words, the story ends up being pretty funny. So let me show you guys over here in my browser, I just have an example of a Madlib you know, essentially there's this story and then you would sprinkle in all those random words that the person entered into the story. So we're going to build something like this in our c++ program. Down here, I have my little program set up. It's just this little poem, it says Roses are red, violets are blue. I love you. So this is a, you know, sort of a classic poem. But I think this could be a lot better if we turn it into a Madlib. So let's say instead of saying Roses are red, why don't we let the user enter in a color, instead of saying violets are blue, we'll let them enter in a plural noun. And instead of saying, I love you, let's say I love celebrity, so some random celebrity they can enter in. So basically, we're going to ask the user to enter in all these different values, we'll store them in variables, and then we can print them out inside of our story. So let's get
01:10:47
started, this should be kind of cool. The first thing we want to do is actually create variables for all these things. So I'm just going to say string. And we'll create a variable for the color for the plural noun and for the celebrity. Alright, so now we've basically told c++ that we're going to use all these variables, but we need to give them values. And we're actually going to let the user give them values when they enter in all those things. The first thing we want to do when we want to get input from the user is we're just gonna say C out and we're gonna print out a prompt, so I'm basically just gonna say, enter a color. So we're gonna have them enter in a color, now we want to actually get the color that they input, so I'm just going to say, Get line. And in here, we're just gonna say ci, N. And that's basically just standing for like that console input. So whenever we like, input text into the console, so it's gonna get whatever gets inputted into there. And then we're gonna store it inside of this color variable. So this will get the line of text that they enter. And I'm actually just going to copy this, and we'll do the same thing
01:11:49
for all the other values. So we're gonna say enter a plural noun. And we'll store that inside of the plural noun variable. And then down here, we're going to do the same thing for the celebrity. So we'll store that in the celebrity variable. Alright, so we're declaring the very most, we're printing out our prompts. And then we're getting the input from the user using this get line function. Last thing we need to do is modify this part of the program. So instead of saying roses, our color, we're actually going to put the color that they entered into the story, same thing for this plural noun. So I'm just going to say, plural noun, and you can see how that gets placed in between these lessons. And then I love celebrity, we're going to actually put in the celebrity variable. Cool. Alright, so now our program should be set up, everything's wired up, we're getting values into all these
01:12:50
variables, and we're printing them out, down here in our story. So let's run this program and we should be able to play our madlibs enter a color, why don't we enter in magenta? Enter a plural noun, let's do microwaves. And enter a celebrity. Why don't we do Tom Hanks. So now when I click enter, all of the values are going to show up in our story. So it says roses are magenta, microwaves are blue. I love Tom Hanks. Awesome. So our Madlib worked, and we are ready to go. So that's essentially how you can build a Madlib I mean, it's it's as easy as getting different inputs from the user and printing them out inside the story. But this is a very simple madlibs. I mean, I'm sure you guys can see that you could ask the user for like 20 different words and print them out into some huge long story. And it's going to work out the same way. So this is an awesome way to create our Mad Libs game. In this tutorial, I want to talk to you guys about using arrays in c++. A lot
01:13:53
of times when we're writing programs in c++, we're going to be dealing with large amounts of data. And one way that we can keep track of and manage that data is by using something called a variable. But the one thing about variables is that generally you can only store one value inside of a variable. So if I create like a character valid variable, I can only store one character in there. If I create an integer variable, I can only store one integer in there. A lot of times though, in c++, we're gonna be dealing with huge amounts of data. And that's where arrays can come in array is basically a container a lot like a variable. But unlike a variable arrays can hold multiple data values. So an array could hold like a list of you know, 1000s, or millions of pieces of information. And generally, we could put, you know, pieces of information that are related to each other in some way inside of the same array. So I'm going to show you guys how we can create arrays and how we can work with them. You create an array, a lot like you create a normal variable. So
01:14:55
the first thing we have to do is tell c++ of what type of data We want to store inside of the array. So I'm just gonna make an array of integers. So I can just say int. And again, just like a variable, we're going to give this a name. So I could say like lucky nums, right, and this would be an array of lucky numbers. Now, here's where this gets different from a variable. When we're creating an array, we always want to put an open and close square brackets after the name. So after lucky nums, we're gonna put this open and close square bracket. And that basically tells c++, okay, they want to create an array, so they want to be able to store multiple pieces of information inside of this container. Now, I'm just going to say equals, and one, the easiest way to create an array is just to assign it some default information right off the bat. And I can just make an open and close curly bracket and inside of here, I can just start typing out the information that I want to store. So let's say I want to store like a list of numbers, I could say like for a 1516 2342, right, so I'm storing all of these numbers inside of this single container. So unlike
01:16:02
a variable where we can only store one number, now I'm sorting a whole list of numbers. And we will refer to these as elements in the list. So four would be the first element in the list. And then we would delineate the next element using this comma. So now we have the second element, 815 16, excetera. So these are all going to be elements inside of our array. Now down here, I'm going to show you guys how we can access individual elements. So I'm just gonna say C out. And let's say that I wanted to access one of these particular elements, let's say I wanted to access like this first element here inside the array, because what good is the array, if we can't access the information inside of it, right, I can just say, Lucky nums. And I can make an open and close square bracket. Now inside of this open and close square bracket, I can put the index of the element inside the array that I want to access. So if I want to access this first element in the array, I just need to put an index in here, that's going to be zero. So now when I run my program, you'll
01:17:02
see we're printing out four, we're printing out that first element in the array, if I wanted to get access to this 15, it's going to be 012, it's going to be at index position two inside of this array. So now whenever my program, we get that 15. So if you haven't caught on by now when we index these arrays, we start with index positions zero. So I would say that four is an index position. 08 is an index position. 115 is an expedition 216, three excetera. So we would always say that the first element in the array is actually an index position zero. And if you're familiar with how strings are indexed in c++, it's the same exact thing. So that's how we can access an individual element in the array, we can also modify an element in the array. So I can say like, Lucky nums. And like, let's say I wanted to change the first element, I could say lucky num, zero, and I could give this a different value. So I could set this equal to 19. For example, Now down here, when
01:18:05
I print out lucky numbers, zero, I'm actually going to be printing out and actually typo whoops, I'm actually going to be printing out 19. So you can see we're able to modify one of the indexes inside of that array. Another thing we can do with these arrays is I can give them a size. So normally, if I just create the array like this, or just say, Lucky nums and an empty square brackets, the array is only going to be able to store the elements that I declare over here. But a lot of times when you're making these arrays, you might not know what all the elements should be. So over here, I could put in a number like I could say, like 20. And essentially what I'm doing here is I'm telling c++, that I want to be able to store 20 elements inside of this lucky nums array. So over here, I don't have 20 elements, yet I only have elements 012345. So what I can actually do is I can add more elements into here. So I could say like lucky numbers six or lucky numbers 10.
01:19:06
And I could give this a value. So I could give lucky numbers 10 the value of 100. And now down here, if we printed out lucky numbers 10 it's going to be printing out 100. As you can see, another thing you can do is just not give this any info right up front so I can get rid of all of these. And I can just put a semicolon here. And then down here, I can give all these different values. So I can say lucky number zero is equal to 100. And now I can you know essentially just assign all the elements after we declare it so like I said, a lot of times you might not know exactly what's going to go in the array when you declare it and so you can just basically tell, see how many items you want to hold in there and then you can just you know, fill the the array up as you go. But that's really the basics of arrays and arrays are very simple. So this is a very simple way To store multiple pieces of information. In this tutorial, I want to talk to you guys about using functions
01:20:09
in c++. A function is basically just a little collection of code that performs a specific task. So a lot of times in c++, when you're writing much code out, you have code that's designed to do a certain thing. So you might have like four or five lines of code that's supposed to do something. And a function is basically a container where you can put that code, and then you can reuse it throughout your entire program. So I'm going to show you guys how we can create a function in this tutorial. So down here in my program, you'll see I have this little block of code here, it says int main, there's an open and closed parentheses. And then there's these open and closed curly brackets. This is actually a function. This is a little block of code. And this block of code performs a specific task. And the purpose of this main function is it's the function that gets executed when we run our program. So any code that we put inside of this main function is going to get executed when our program runs, I'm gonna show you guys how we can create another function.
01:21:12
So up here above this main function, I'm going to create another function. And the task that this function will be performing is it's going to say hi to the user. So whenever we're creating a function in c++, we need to give c++ a couple pieces of information, the first piece of information we need to give is called a return type. Now whenever we create these functions, a lot of times the functions will go off, they'll perform a specific task, and then they'll return a value back to the caller. And we're actually going to talk more about returns and return types in the next video. But for the purposes of this video, you can kind of just follow along. And I'm just going to go ahead and put void here. And when we put void here, it basically means that this function is not going to return anything. So this is kind of like the most basic type of function. And after we put void, now we need to give this function a name. Remember, a function is a block of code that performs a task. So generally, when we're naming a function, you're going to want to name it according to the task that is performing or according to the purpose of the function.
01:22:14
So I'm just going to call mine Say hi, because our function is going to say hi to the user. Now I'm going to make an open and closed parentheses. And after this, I'm going to make an open and closed curly bracket. Any code that I put inside of this open and close curly bracket is going to be considered inside of the function. So inside of here, why don't we just say like C out, and we'll just print out like Hello, user. And that's basically all we'll do. So this is a very simple function, I just have one line of code in here, I can have as many lines as I want. This is a simple function. But I can have, you know, a dozen lines or 100 lines, if I wanted to, you can put as many lines of code in the function as you want. Now, let's run our program. And we'll see what happens. So I'm going to go ahead and run my program. And you'll see over here, nothing's getting printed out. So hello, user isn't actually getting printed out when we run our program. Here's the problem, when we want to execute the code that's inside of these functions. In other words, when we
01:23:17
want to execute a function, we have to do something called calling it. So if I want this code inside of here to be executed, I have to call the function. Now remember, the code inside this main function gets executed by default, right, so no matter what this is going to get executed. So inside of here, I can call this Say hi function, I can just type out say hi. And I can type an open close parentheses. And when I do this, when I type this out, this tells c++ that I want to execute all of the code that's inside of this, say hi function. So when c++ sees this, it's going to jump up to this Say hi function is going to execute all the code inside of there, and then it's going to come back down. So let's go ahead and run our program. And we'll see what happens. So you can see over here, now we're printing out Hello, user. And real quick, real quick, I just want to show you guys the flow of the so if I said like C out over here, and I said top. And then I did the same thing over here, and I said bottom. When I run my program, you'll see that we're printing out
01:24:21
top, hello, user and then bottom. And actually, this probably would have been better if I put new lines in there. But the point is that when this program executes, c++ is going to execute this line of code, it's going to see that we want to call this Say hi function. And it's actually going to leave this main function, it's going to jump up here to the Say hi function, it'll execute all of the code inside of here. Then when it runs out of code to execute inside of the say, Hi function is going to jump back to the main function and execute this line. So that's basically like the flow of what's happening. So this is a very basic function, but we can make this more complex and one thing you can do With these functions, you can actually give them information called parameters. So this Say hi function, I can give this a piece of information. And the Say hi function can use that piece of information to, you know, performance task differently or better or whatever. So these are called parameters. And if I want to specify that this Say hi function should be given a piece of information, I can just come over here and specify what piece of information
01:25:25
it should take. So, in our case, instead of saying hello user, why don't we say hello, hello to someone specific. So up here, I could say like string name. And now this functions going to accept one parameter a name. And down here, instead of saying hello, user, we can just say hello name. Now, whenever I call this, say hi function now, because it's specifying that it needs to take a parameter, I need to pass it a parameter, I need to pass it a value. So in here, I can just pass it like Mike. And now it's going to print out Hello, Mike, because the value Mike is going to get stored inside of this name variable. Alright, let's go ahead and run our program. And now you'll see it's printing out Hello, Mike. So that's kind of cool. And you can take any piece of information as a parameter, and you can also take multiple parameters. So why don't we specify another parameter like age, so now the caller is going to have to pass in their name and their age. So now
01:26:27
we can say hello name, will say like you are. And then we'll just say how old they are. So we'll say like age. So now we're passing in two pieces of information inside of this function. And down here, when I call the function, I have to give it two pieces of information. So now I could just say, like, Mike, and let's say that I'm 60. So now, when we call this function, it's going to be able to take in both of those pieces of information, and use them to perform the task differently. And what's cool about these functions is I can call this as many times as I want. In other words, I can reuse all of the code that's up here. So I can come down here, I can copy this and why don't we do this a bunch. So we'll say like, Tom is going to be 45. And let's say Steve, is going to be 19. And so now, I'm actually going to run this code three different times. And actually, over here, I'm just going to put a and line so that gets printed out on new lines. Alright, let's run
01:27:31
the program. And you'll see it says hello, Mike, you're 60 times 45 and Steve's 19. So functions are great, because we can reuse the code that we write inside the function. So basically, like I wrote this function one time, and I can reuse it as many times as I want inside of my program. So anytime you have code that's going to be reused a lot, that's a good candidate for a function. Alright, so I want to show you guys one more thing, you'll notice that I'm creating this function up here above my main function, right. But if I was to take this and move it down here, so for example, I moved it below the main function. Now when I run my program, we're going to get an error, you can see we're getting a little red block here. This is the problem, when we create this function below the main function, it doesn't actually know about it. So like all this code up here, is getting executed. So c++ is trying to execute the Say hi function, but it has no idea like what that is because we created it down here. So what we can do is we can actually create what's
01:28:33
called a function stub. So up here, we're basically just going to write out the functions signature, and we're going to tell c++ about it. So you guys remember, like, if I created a variable, like if I created an int, and we just call it num, I don't have to give this a value right away, I could then come down here and say, like num is equal to four or something. This is basically what we're gonna do with this function, we're gonna essentially just declare the function and then somewhere else in our program, we can define it. So over here, I'm just gonna say, void, say hi. And it needs a string name, and it needs an int age. So now, when I create this little function stub up here, or this function signature, I'm basically declaring the function and I can give it a value down here. And now c++ is going to be able to call it because it's going to have some information about it. So when I run my program, now, we're able to call it No problem. So that's kind of just how we can do that with functions. And really, you can create as many functions as you want.
01:29:35
In fact, a good program will have lots and lots and lots of functions. And the next tutorial, I'm going to show you guys how we can actually get information back from functions using the return keyword. In this tutorial, I want to talk to you guys about returns in c++. So when I'm talking about returns, I'm talking about returns from functions. So in c++, we can write functions which are basically like little containers that store a bunch of code that performs a specific task. And with those functions, we can call them and we can also pass them information. So I can give them all sorts of like parameters and different information that they can use to perform their task better. But in addition to giving functions, information functions can actually give us back some information. So when I call a function, not only can I give it parameters, but that function can give information back to me. Now I'm going to show you guys how we can use this and how we can use the return keyword in c++. So why don't we create a function up here, and I actually want to create a function
01:30:39
that's going to cube a number. So when I cube a number, I'm basically taking it to the third power. So if I was to say like, two cubed, it's basically just two raised to the third power, or it's just going to be two times two times two, right? That's what cubing a number does. So why don't we create a c++ function that's going to cube a number. Now when we create a function in c++, the first thing that we have to do is declare something called a return type. And a return type basically tells c++ what type of value or what data type, this function is going to return. Now in the last tutorial, if you're following along with the course, we just put void here. And void basically meant that our function wasn't going to return any information. In this tutorial, though, we definitely want to return some information. So you can put any of the generic c++ types here, you can put like int, double, you put character, you put string, essentially, any of those values can go here. In our case, we're going to cube a number. So why don't we just make it a double.
01:31:41
And I'm going to say double. And now we need to give this a name. So I'm just going to call it cube. And we're going to take one parameter into this function. So why don't we take a double, and we'll just call it num. Now, inside of this function, all we want to do is cube the number and then return the result that we get. So I'm actually going to create a double, I'm going to call it result. And I'm going to set this equal to the cube value of num, so I'm just gonna set it equal to num times num, times num. So this is basically me cubing num. So now result has inside of it, the result of cubing all these numbers, okay, down here, I want to return the value that's stored inside of results. So I can just say return result. And now this is going to tell c++ that we want to return this value stored inside result back to the caller. Now down here in my main function, I can actually call this function so I could say cube and I could pass as a double, let's say like 5.0.
01:32:43
Right? Now, actually, what's going to happen is when we call cube and we pass it this information, it's actually going to get a value back. So if I wanted, I could basically say like, double answer is equal to cube 5.0. And now the value that gets returned back from this cube function is going to get stored inside of this answer variable. So let me show you guys, I can say C out, and I'm just gonna print out answer. And so now when I run my program, what you'll see is, we're going to be printing out the result of cubing, five. And you'll see over here, we get 125. So five times five is 2525, times five 125. So we are able to cube the number. And you can see here, this is getting a value back. So I'm able to store the value that gets returned from this function inside of this variable. If I wanted, I could also cut out the middleman and I could just print out cube 5.0. So let's just paste this bad
01:33:47
boy down here. And now it's going to do the same exact thing. So we should get 125. Cool. And actually up here, if we wanted, instead of storing num times num times num inside of this result variable, I could instead just return these guys up here. So we again, we cut out the middleman. And we'll just return that and we should get the same exact answer. So there's also one more thing I want to talk to you guys about, which is this return statement, this returned keyword right here. This is a very special word in c++. And basically whenever we type out this return keyword, it's essentially telling c++ that we're done executing the code inside of this function. So if I was to put like a C out here, and I just printed out like hello. When I run my program Now, you'll notice that it's not printing out Hello, even though we're executing that cube function, even though we're executing all that code, it's never printing out Hello. That's because whenever we use this return
01:34:49
keyword, it's going to break us out of the function. So this line of code is actually never going to get executed because it's never going to get reached. When c++ sees this line is is going to break out and we'll head back down to the main method. So that's essentially what this is doing. And that's sort of the basics of using returns. So like I said, if you didn't want to return a double, you could return any type of date, I mean a string, you can even return something like an array. So really, you can return anything you want. And then that value will get stored over here. In this tutorial, I'm going to talk to you guys about if statements in c++, an if statement is basically a structure that we can use in our code, which will allow our program to respond to different situations. So when one situation occurs, we can do one thing. And when another situation occurs, we can do another thing, essentially, we're able to check different conditions. And when those conditions are true, we can do certain things. And when those conditions are false, we can do other things.
01:35:53
So I'm going to show you guys exactly how this works down here in our main function, I'm just going to create a couple of different things. So the first thing I'm going to do is create a variable, it's going to be a boolean variable. Remember, a boolean variable stores a true or a false value, so I'm going to create a Boolean called is male. And I'm going to give this a value initially of true. So we're basically creating this variable izmail, it's storing whether or not someone is male, and I'm giving it a value of true. Now I want to show you guys how we can use an if statement. And an if statement, like I said, allows us to respond to different situations. So what I'm going to do is I'm going to be able to write an if statement that will respond to the situation where the user is a male, and the situation where the user is not a male. So what I can do is I can actually say F, I'm going to make an open and close parentheses, and then I'm going to make an open and closed curly bracket. Now, inside of this open and close parentheses, I can specify a condition.
01:36:54
So I can actually check a condition. And if the condition that I put inside of those open and close parentheses is true, then we're going to execute the code inside of here. If it's false, then we're going to move on. So over here, we're essentially putting a true or a false value. And that's basically a condition right? It's a true or false value. So I'm gonna come down here, I'll say if and I'm just going to type out his mail. And essentially, what this is saying is if the value stored inside of his mail is true, in other words, if the value in here is true, then we're going to execute the code that's inside of these curly brackets. So I could say, like C out, and I'm just going to type out like you are a male. And now when I run my program, you guys, we'll see what happens. So run the program. And you'll see over here, it says you are a male. And that's because the Boolean is male was true. So the value that was inside of this condition, the value inside of the
01:37:56
parentheses, which was true, and we executed this code, if I made this false, now, you'll see that this code isn't going to get executed, so we're not going to be printing anything out. So because the value inside of this parentheses, because the condition was false, we're actually not going to execute this code. But let's say that if the person wasn't male, we also wanted to handle that situation, I can use another keyword in C called else so I can type out else just like that. And we're gonna make an open and closed curly bracket. Now, inside of this open and close curly bracket, I can put a message that's going to be displayed when this is false. So I'm going to show you this a C out. And I'll say, you are not male, right, so I'm responding to this situation. So now, even though is male is false, when I run my program, it's going to be able to respond to that. So it's just gonna say you are not male. Essentially, what's happening here is, my program is now smart enough to
01:39:00
respond to this variable. So if this variable is male is true, it can handle that. If it's not true, it can also handle that. And so basically, inside of these parentheses, I specified a condition in our case, it was a true or false value that was stored inside of this is male variable. So that's kind of cool. Now let's up the ante a little bit, I want to show you guys how we can make these more complex. This is a very simple example. But let's say I added in another Boolean, so I had another Boolean called is tall. And I'm going to set this equal to true and actually, I'm gonna set this equal to true as well. So now in addition to izmail, I also have is tall. So let's say that we wanted to check to see if the person was both male. And if they were tall, so down here, what I can do is I can use something called the AND operator I could say is male, and is tall. And basically what's happening now is c++ is saying If the person is male, and the person is tall, then
01:40:04
we're going to print this out. So I'll just say you are a tall male. So this code, this line of code down here, and any of the code inside of these open and closed curly brackets is only going to get executed when is male is true, and is tall is true. Essentially, this and operator is allowing me to check two separate conditions. And both of them have to be true, in order for this line to get printed out. So you'll see is male is true and is tall is also true. So now, when I run my program, you'll see that it says you are a tall male. Here's the thing, though, if I set one of these to false, so if I set his tall to false, this whole condition is no longer going to be true. So then we're going to go down here to this else block. And this should probably say, like, you are not male, or not tall, but we'll just leave it like that just for demonstration purposes. Alright, so now it says you are not male, right. And obviously, like I said, that message should be different. But the point is that this condition was false, because one of these
01:41:06
values up here was false. And that's how we can use this and operator, there's another operator called the OR operator. And that is similar to and except only one of these conditions has to be true, in order for the whole thing to be true. So even though his tall is set to false, and his male is set to true, this is still going to execute, so it's still gonna say you are a tall male, because one of them is true. So let's go ahead and run this program. Now, you'll see it says you are a tall male, so it executed the code that was inside this initial if block. And that's because one of them is true. If I set both of these to false now, because we're using the or now it's going to go down to you are not male. So you'll see down here it says you are not male. So that's basically how we can use and and how we can use or with the AND operator, we can put two conditions in both of them have to be true for the whole thing to be true with the OR operator, only
01:42:09
one of the conditions has to be true for the whole thing to be true. And they can both be true as well. And it'll be true. But if both of them are false, then it's going to be false. All right, let's make this even more complex. So so I want to show you guys, another thing we can do, I'm going to bring this back to and so I'm gonna say is male, and is tall. But let's say that we wanted to check to see if they were male, and they weren't tall. So if they were like a short male, well, I can actually check another condition. So I can use another keyword. In c++, it's called else if, and I'm going to make an open and close parentheses and open and close curly bracket. And I'm just going to bring this down to a new line. So else if is basically saying, if this condition up here is false, then we're going to come down here, and we're going to execute this condition. So if this condition appears false, instead of just going to else, we're going to come over here, and we're going to check another separate condition. So over here, I want to check to see if they're
01:43:09
male, and they're not tall, so they're not tall. So I can say, is male, just like before, so I'm saying elsif is male, and is tall. But remember, I want to check to see if they're not tall. And here, I want to introduce you guys to another operator, it's called the negation operator, it's basically an exclamation point. And whenever you put this exclamation point before a condition like is tall, it's going to actually negate that entire condition. So if is tall is true, and we put this negation operator over here, it's going to set it equal to false, and vice versa. So essentially, what I'm saying here is else if is male, and is not tall. That's basically what this is saying. So now, over here, we can just print out, you are a short male, right? Because if this code, in other words, if this condition is true, that means they're male, and they're not tall, so they're short male. And there's one more that we can check. So I'm going to say another LSF over here. And there's one
01:44:14
more possible scenario with these two variables up here, that's when they're tall, but they're not male. So I can say not, is male, and is tall. So this would be like, you know, it could be like a tall female or something. Right. And so down here, I'm going to go ahead and say C out, and I'll just say you are tall, but not male. Right. So essentially, I'm just printing that out onto the screen. So now we have all of these different situations covered. So over here in this if else block which is what we would call this and actually down here in this else block, we would want to change this. So this is going to be you are not male and not tall. So that's just you know semantically going to Correct. All right, so we're able to respond to every possible combination of these two variables up here. So let's try this out, I'm going to set both of these equal to true. And now I'm going to run my program. So they're both equal to true. So our program will be able to respond
01:45:16
to that situation, who see it says you are a tall male, awesome, that was able to respond to that, let's set his male equal to false. So now we have izmail false and is tall, true, our program, once again, is going to be able to handle that. So it's gonna say you are tall, but not male. So the program is smart enough now to be able to tell us what we are depending on the value of these variables. Let's try another one. So we'll set is male to true and is tall to false. And again, it's going to be able to respond. So it will say you are a short male. And then finally, we'll set them both false. And we should get the answer which is going to be you're not male, and not tall. Awesome. So we were able to figure this out just by using all this stuff. So let me walk you guys through this one more time, we have this if statement, and it's checking if they're male. And if they're tall. Now, these are both conditions, right? So I'm checking this is Gmail, this is a condition, essentially, it's a true or false value, right? And then I'm checking is tall. And if both
01:46:21
of these are true, then we'll execute the code inside of here. If this is false, then we'll come down here and we'll check this other condition. And we're going to keep doing that. And so we get down here, and we figured out okay, they're not male, and they're not tall. So that's the basics of if statements. And really, this is just scratching the surface of if statements, I talk to you guys about a lot of the different things we can do. But there's one more thing I want to talk to you guys about in the next tutorial, which are called comparisons. And down here, we're using these Boolean variables in order to get our conditions right, so I'm using these, and these are either going to be true or false, right. But we can also use something called comparisons. And with comparisons, we can check to see if different values relate to each other in different ways. So I could check to see if like a number is greater than another number or something. And we can use those comparisons as our conditions. And that's been pretty cool. So stick around for the next tutorial. And I'll talk to you guys more about these if statements. In this tutorial, I'm going to talk to you guys some more about if statements more specifically, we're going
01:47:23
to be looking at using comparisons inside of our if statement condition. So this would be kind of cool. In this tutorial, I want to create a function, so we're going to write a function, and it's going to basically tell us the max of two numbers. So this function will take as parameters, two numbers, and then will basically return whichever number is the largest. And this is a great example of how we can use comparisons with if statements. So over here, I have my main function. And up here, I'm going to create another function. And let's just say it's going to return an integer, and I'm just going to call it get max. And we'll make an open and close parentheses and open and close curly bracket. So inside of this function, we're going to take two parameters, one is going to be an integer, and we'll get another integer. So essentially, we're going to take two integers as parameters as input. And the goal will be to figure out which of the integers is the biggest, and return that back to the caller. First thing I want to do is just create a variable called result. And I'm not going to give this value right away. But eventually, at the end of
01:48:28
this function, we're going to return results. So whatever we do in here, we have to store whichever of these is bigger inside of this result variable. So this should be kind of interesting. Now, we need to figure out which is bigger. So we can use an if statement to do that. So I'm just going to say F, open and close parentheses and an open and close curly bracket. Now, inside of this parentheses, we need to put a condition. In other words, we need to put a true or false value, right? If the value is true, then we'll execute whatever's down here. If the value is false, then we'll just move on. Here's the thing though. In the last tutorial, when we were learning about if statements, we were using Boolean, right, and a Boolean is a true or false value. So it's really easy, right? You just throw it in there, and it's true, or its false. And you can just kind of do the if statement. But in a lot of cases, we're not going to have any Boolean information available to us. So it's not going to be as easy as just like throwing a Boolean in there. All the only information that we have are these two numbers. So how can we create like a true
01:49:31
or a false value using just those two numbers? Well, what we can do is we can use a comparison. So if I want to figure out which of these numbers is bigger, I can just compare them right so over here inside of this if condition I can basically just say if num one is greater than num two, if num one is greater than num two, then we can just set result equal to num one. And the reason I'm doing that is because we know for a fact That if this condition, if this comparison is true, the num one is the bigger. So you can see this comparison num, one greater than num two, it's actually going to get resolved down to a true or a false value. Right? num. One is either greater than num two, or it's not technically, this is going to be a boolean value, right? Once you evaluate the comparison, once we figured out if it's true or not, it's going to be a true or false value, right? So I can basically check to see if num one is greater than num two, and if it is, was that result equal to
01:50:32
num one. And we can just say else, and we'll set result equal to num two, right. And the reason we're saying this down here is because of this condition is false. If num one isn't bigger than num two, then we know that num two is the bigger of the two numbers, or in certain circumstances, the numbers might be equal, but it's still going to work anyway. So down here, we're going to return the result. So let's go ahead and test this function. I'm just gonna come down here. And why don't we print out the answer. So I'm just gonna say get max. And we'll throw like a two and a five in there, right? So pass in a two and a five. And now when we run this program, hopefully we should get that five back. So I'm gonna run the program. And Yep, over here, we get our five. So that's working perfectly. So this is basically how we can use comparisons inside of these if statement conditions, right? Technically, this comparison is going to get resolved down to a boolean value, it's going to get resolved down to a true or false, like either the comparisons true or it's not right.
01:51:37
And that's kind of interesting. So that's sort of the basics. And in addition to using greater than we could also use less than, we could use less than or equal to, we could use greater than or equal to, we can use equal to and equal to is a double equal. So when we use the equal sign down here, we're using it for assignment. So I'm assigning the variable result the value stored inside num one. When I use this double equals, it's a comparison operator. So I'm comparing the value of num one with the value of num two. And again, that's either going to be true or false, right? They're either going to have the same value or they're not. And then the last one we can use is a not equals and not equals does exactly what you think it does. This whole thing is going to be true if num one is not equal to num, two. So those are sort of the basics. Now let's make this a little bit more complex. So this get max function takes two integers. What if we had it take three, so let's say int, num, three. Now all the sudden, instead of just comparing two numbers, we have to
01:52:39
figure out which is bigger among the three numbers. And this is going to make our if statement a lot more complicated. So why don't we just start over. And I'll show you guys how we can do this. So inside of this comparison, I need to check to see if num one is bigger than num two, just like we did last time. But I also need to check and see if num one is bigger than num three. If no one's bigger than num two, and it's bigger than num three, then we know it's the biggest. So let's start there. I'm just gonna say if num one. And instead of just saying greater than I'm gonna use a greater than equals right, because there is that chance where they're all the same number in which case this will cover that. So we can say if num one is greater than or equal to num two. And so I'm going to use this and operator and this is going to allow me to check another condition. So I can say num one is greater than or equal to num three. So now if num one is greater than or equal to num two and is greater than or equal to num, three, than we know num, that one's the biggest. So I'm going to set result equal to num one. Now down here, we need to check
01:53:45
another condition. So I'm gonna say else if and we basically want to check to see if num two is greater than or equal to num one. And num two is greater than or equal to num three. And if this is the case, in other words, we're going to check this condition if this condition of here is false. So this condition is false. We know that num one's not the biggest, right. But if this condition is true down here, then we know number two is the biggest. So we'll say result is equal to num two. Now all we have to do is just say else. And down here we can just say result is equal to num three. Because if this condition is false, that means num one's not the biggest. If this condition is also false, that means num twos not the biggest. So if num one's not the biggest and num twos, not the biggest, then num three must be the biggest. So we're going to default to this value. Alright, so that is the if else block that we need. And you see I'm using a bunch of different stuff here. So not only
01:54:47
am I using this and operator, I'm also using these comparison operators and that's what these guys are called. So let's go ahead and test our little function out. So now instead of saying two parameters, we need to pass in three So in the past one to five and 10, and we should get 10 back, which we do, if we wanted, we could test out each of the positions. So for example, I could test out, we'll make this one the biggest now the second one. And we'll see if it works. And it does. And we'll make this one the biggest now, and we'll see if it works. And it does cool. And then also there was that condition where like two of them were the same. So let's try this were two of these guys are going to be the same. And we're on the program. And yeah, so it's still going to be able to return 200. So there is our max function. And hopefully this gives you guys a little introduction into using comparisons. Now we can compare all different types of data. So using these comparison operators, we can compare integers, we can also compare like doubles and floats, and we can compare
01:55:50
characters. So you can compare all those guys just by using all that stuff in there. In this tutorial, I'm going to show you guys how to build a four function calculator in c++. Now, if you've been following along with this course, you'll know that in the beginning of the course, we created a very simple calculator. And basically, that calculator was able to take in two numbers as input, and added them together and printed out the answer. But in this tutorial, now we've learned a little bit more in the c++ language, we're going to build a more complex calculator. So this calculator is going to allow the user to enter in two numbers, but it's also going to allow the user to specify which operation they want to perform. So they can specify that they want to add the numbers and multiply the numbers, divide them, multiply them, whatever. So it's essentially going to be a more powerful calculator. So down here, inside of my main function, I want to start writing the code for this. And the first thing I'm going to do is create a few variables. So I want to create a couple
01:56:52
variables. And why don't we just create these as and you could also make these doubles, if you wanted. We'll just call them num one and node two. And these are going to represent the two numbers that we want to perform the operation on. And then down here, we're also going to want to create a character. And I'm going to call it Opie for operators. So we're going to throw the two numbers here. And we're going to sort of the operator here. So it's either going to be plus or minus division, or multiplication. Alright, so now that we've created those two variables, we want to start writing the code to get the input from the user. The first thing that we want to do is print out a prompt, so I'm just going to say C out. And I'll basically just print out enter first number, so we'll prompt them to enter in the first number. And then down here, we're going to use C in and remember you want to these, these two greater than signs. And we want to store the user's input inside of num one, just like that. Okay, cool. So now that we got num, one, I'm actually just going to copy this code, and then down here, we can paste it two more times. So down here, we're
01:57:54
going to ask them for the operator. So I'll say enter, operator, and we're going to store the operator inside of that Opie variable just like that. And then finally, we're going to enter in the second number, and we'll store it inside of num two. So basically, what I'm doing is I'm prompting the user for all of the information that we need. So we're going to get that first number, we're going to get the operator and then we're going to get the second number. So they'd give me like five plus two, or six divided by seven or something like that. Alright, so now that we have all of that information, we have one more thing that we need to do, which is we need to figure out what the user wants to do. Right over here, we have the operator. So ideally, they would have entered in like a plus sign a minus sign, whatever, but we don't know what exactly they entered in like we don't we have no idea what what they were going to enter in. So we need to figure that out. And this is a perfect scenario for an if statement. So I'm going to use an if statement. And I'll show you guys how we can check this. So I'm just gonna say if. And we need to put a condition in here. So basically, what we want to do is want to figure out what that operator is.
01:58:58
So the first thing I'm gonna do is just check to see if the operator is equal to a plus sign. Right? So if the operator is equal to a plus sign, then we're basically just going to print out the result of adding the two numbers together. So why don't we come up here, and I'm going to create an integer called result. And then down here, we'll set result equal to num, one plus num, two. And we know that we want to add them because if this condition is true, then we're going to execute this code. So now let's do an ls F. And down here, I basically just want to check to see if it's a minus sign. So I'll say if Opie is equal to a minus sign, and if it is equal to a minus sign, then we can do exactly what we did up there. But we can subtract the two numbers. So I'll say num, one minus num, two. Alright, so we can do the same thing for multiplication and division. So I'm actually just going to copy this else if, and I'll paste it down here. And now we'll say okay, is equal to
02:00:01
forward slash, that means division. And then one more time, we're going to do this for multiplication. So use this Asterix, and then here we're going to multiply the two numbers. So now we're basically covering all of the operators. So they enter in a plus sign, and we got that covered minus sign, division sign and multiplication sign, right, we can cover all of those standard scenarios, but there's always the chance that the user entered in an invalid operator. So instead of entering in one of these, they just entered in some like, you know, random character, in which case, I'm going to use an else block over here. And this LS block is going to get executed whenever none of these conditions are true. So if none of the conditions are true, then we know they entered an invalid operator. So I'm just going to go ahead and print that out, I'm going to say C out. And we'll just print out invalid operator. Alright, so basically, by the end of this, if block, we should have a value stored inside of result. So down here, we'll just going to print it out. So I'll
02:01:01
just say C out, and we'll print out result. Alright, so let's see if our program works. I'm going to go ahead and run the program. And it says enter first numbers, why don't we insert a five? Enter operator, let's enter a plus sign and second number. Why don't we do 30. So now when I click enter, hopefully our if statements going to execute, we'll be able to figure out which operation I was trying to do, and then we'll be able to perform it correctly. So you see down here, we printed out 35. So it looks like everything worked. So why don't we try a couple other ones. Let's do like 50 divided by five. So down here, we get 10. Yes, it looks like that's working. Alright, let's check one more thing, let's check to see if we enter an invalid operator. So I'm gonna put like a five, we'll enter the operator, let's make it like a capital G, and then put a nine for the second number. And you'll see down here it prints out invalid operator. And then we're also getting this other little printout here. And actually, that's just because we didn't give results
02:02:04
we didn't give result in initial value. Alright, so that's basically our four function calculator, you guys can see how we can combine getting input from the user, then we can use these if statements to basically figure out what the user input and that can be extremely powerful. And this tutorial, I'm going to talk to you guys about using switch statements in c++. Now a switch statement is basically a special type of if statement, which allows us to check one particular value against a bunch of other values. So this is a very special use case, when we're trying to compare different things. So I'm gonna talk to you guys about what switch statements are. And we'll do an example which are kind of illustrate what they're doing. So in my program, In this tutorial, I actually want to create a little function, which is going to be able to convert a number into the day of a week. So basically, if the user passed in a zero to this function, I'd want to be able to print out like Sunday, or if
02:03:06
the user passed in a one, I'd want to be able to print out Monday, etc. So I want the function to be able to convert an integer number to a day of the week, just like that. So I'm gonna show you guys how we can do this inside of our c++ program. So I'm going to create my function, and we're basically just going to have it return a string. And I'm just going to call it get day of a week, just like that. And I'm going to make an open and close parentheses. And this is going to take one parameter, which is going to be an integer. And we'll just call it day num. So this is going to be the number of the day of the week. So like zero through six, basically. Alright, so inside of our little function, now, we need to figure out how we can do this. So depending on the day of the week, in other words, depending on the day num, that they give us, we want to be able to return a different value. So the first thing I'll do is I'm going to create a variable. And it's just going to be a string, and I'm just going to call it day name. So this will be like this will end up storing
02:04:08
the name of the day that they requested. And then down here to the end of this function, we're going to return it. So I'm just going to say return day name. So now we know what we're going to end up returning. So our goal inside of this function now is to populate this day name variable with the correct day of the week. So let's think about this. I mean, one way that we can build this function is by using an if statement. So I can say like, if day num is equal to zero, and if day num is equal to zero, then I can say day name is equal to Sunday, right? That works. And then I can just keep doing this for every day of the week. So I can say like, else if and again, I have to check to see if day num is equal to something else. So day num is equal to one and then down here we'll do the same exact thing. So we Ad name is equal to Monday, right? I can keep doing this for every single day of the week. Here's the problem, though, I'm gonna have to do this like seven times, right? Potentially eight times, if we want to check for an invalid input. So
02:05:13
I'm going to have to create eight of these different if else structures in order to figure this out. And that is extremely inefficient. And actually, this is a perfect circumstance for us to use something called a switch statement. Now a switch statement essentially takes what we're doing over here with these if else statements, and it makes it a lot easier. So one thing I want you guys to notice is in every single one of these conditions, in other words, in every single one of these, if conditions, we're checking to see if the same value, day num is equal to a different value. So in here, we're saying Dana is equal to zero. Here, we're saying Dana equal to one and the next one, it would be Dana equal to two, then Dana equal to three, all of those conditions are checking to see if daenam is equal to something else. And when you have a situation like that, it's a perfect candidate for a switch statement. So I'm going to show you guys how we can use a switch statement to make this a lot easier. So I'm just going to write out switch and open and close parentheses and an open and close curly brackets. So so far, it looks a lot like an if statement, up here in these
02:06:16
opening, close parentheses, I'm going to put in a value. And we're basically going to put the value in here that we want to compare to all those different values. So in my case, it's going to be daenam. Right, I'm comparing daenam to 0123456, excetera, right? daenam is the value that I want to compare to all the different values. Now inside of this switch statement, I can write cases. And basically I can say case, and then I can just put a value in here. So I can say zero and a colon. And then down here, I can basically type out what I want to happen. So I could say day name is equal to Sunday. Now essentially, what this is doing is it's saying in the case, that day, num is equal to zero, we want to set day name equal to Sunday. And then right below here, I'm just going to say break. And I'll explain what break does in a second. But I can basically make one of these cases for every single number. So I could say like, case one. So in the case, the day num is equal
02:07:18
to one, I'm going to set day name equal to Monday. And then again, I'm just going to say break. And then I can keep doing this for 23456, etc. Right. So what this break is doing down here, this break statement is essentially breaking us out of the switch statement. So if I don't put this break statement here, c++ is actually just going to keep looking through all these cases. So even if daenam is equal to zero, and it sets it equal to Sunday, it's still going to keep going and checking all these different values and less I say break right here. And generally, it's a good idea to say break unless you want to be able to check for more than one condition, we're basically saying in the case, the day num is equal to zero, we're going to do all the code down here. And I'm actually going to make one of these for each day of the week. So I'm going to go off and do that. And then we'll meet back here, and I'll kind of show you guys what I have. Alright, so I went ahead and made one of these little case blocks for every day of the week. So you can see down here we have zero through six. And they're all corresponding with the appropriate day.
02:08:24
So in case three, day name is Wednesday, etc, right, so everything seems to be working. Now there's one more thing that we have to do in this switch statement. So down here, there's always the chance that the user enters in an invalid day of the week. So in other words, if the user doesn't enter zero through six, then we want to be able to handle that. In other words, I want to be able to handle the situation where the number that they entered isn't in here. And inside of a switch statement, we have a special little block, which is called default. So we can just say default, a colon, and then down here, this code is going to get executed, if none of the cases up here are true. So if the number isn't equal to any of these numbers up here, then this code inside the default block will get executed. So down here, I could just say, day name is equal to invalid day number. Alright, so now we have our switch statement set up. And you can see how this is a lot cleaner than something like an if statement. So this is a lot clearer what's going on, we don't have to type out LSF like 1000 times, we don't need all those parentheses and all that stuff. Basically,
02:09:28
it's just really clear, it's really easy to visualize what's going on. Alright, so let's see if our get day the week function is working. I'm going to go in here and we're actually going to print out the value. So I'll say C out. And we'll call this so I'll say get day of week and we need to pass this an integer. So why don't we pass it zero. And hopefully we should get printed out here Monday. Or Sunday. Whoops. Yeah. So over here Sunday, so Sunday would be the first day of the week. Let's try. Let's see if we can get Monday. So I'm going to put one here instead of zero. And now we should get Monday. Yeah. Awesome. All right, let's try one more, we'll try six. So six is going to be Saturday. And let's see if it works. Cool. So Saturday worked as well. Alright, there's one more situation, which is where we pass in an invalid number. So if I pass in like a 10 here, now we'll be able to see that this isn't going to work. So it should tell us invalid day number. Awesome. So this is a pretty cool function. And it's working. And you can see how we can use these
02:10:32
case statements. In order to make this a lot easier. Now, here's the thing, I could have easily done this with an if statement, nothing is stopping me from doing that. But it's a lot easier to use something like a switch statement. And that's why c++ included it in the language. In this tutorial, I'm going to show you guys how to use while loops in c++. Now a while loop is basically just a programming structure that allows us to loop through a block of code, while a certain condition is true. So with this tutorial, we're starting into something called loops. And loops are extremely useful. And there's a lot of situations in our programs, specifically in c++, where we're going to want to be able to loop over code and do things a bunch of times. So I'm gonna introduce you guys to the most basic type of loop, which is a while loop. And we'll sort of see what it does and see how it works. Alright, so down here, the first thing I'm going to do before I create my while loop is I'm actually just going to create an integer. And I'm just going to call it index, and I'm
02:11:33
going to set it equal to one. So this isn't 100% necessary when you're creating while loops. We're just going to use this in our example in this tutorial. Now, when I want to create a while loop, I can basically just say, while I can make an open and close parentheses, and an open and closed, curly bracket, and this is sort of like the main structure, the main outline for a while loop. Now here's what's going to happen. Okay, over here, inside of these parentheses, I'm going to specify a condition. Now, if you remember, learning about if statements in c++, this is essentially what we're doing here. So in here, we're putting a condition just like we would an if statement. And as long as this condition is true, I'm going to loop over and continually execute all of the code that's inside of these curly brackets, as you'll see in a second, we're going to put a bunch of code inside of these curly brackets. And as long as the condition up here is true, we're going to keep looping through that code. And every single time we loop through that code, we're going to check
02:12:33
this condition. So let me just show you guys this, and we can kind of get a feel for what's going on. So up here in these parentheses, I can basically just write a condition. So what I want to do is I'm going to say while index is less than or equal to five, so this is now my condition, while index is less than or equal to five, I'm going to execute the code inside of this loop. So I'm going to execute the code inside of these curly brackets. So down here, what I want to do is I basically want to print out the value of index. So I'm just going to say, C out index. And then I'm also going to say and so we print out a new line. And then down here, I'm going to increment the index, so I'm just gonna say index plus plus. So you remember index plus, plus just add one two, index essentially. Alright, so what I'm doing is I'm specifying my condition, while index is less than or equal to five, I want to execute all of this code. So I'm going to run my program. And maybe you can sort of see already what this is going to do. But if not, we're going to run it and
02:13:35
we're going to take a look at what's happening. So you see over here, when I ran my program, we basically got 12345. Here's what's happening over here, I created this variable called index, I set it equal to one and I basically told c++ that while index is less than or equal to five, I want to keep looping over this code. So basically, what happened was the first time we went through, we checked this condition, so we check to see if index is less than or equal to five. Since that was true. In other words, since index was one on the first iteration of the loop, we executed this code, so we printed out one, and then we incremented index, so index was now equal to two, then we came all the way back up here and we check this condition again. So we check to see if index is less than or equal to five. In this case, index is equal to so the conditions true index is less than or equal to five. So we're going to execute all of the code inside of here again, so we printed out two, and then we incremented to up to three, and then we came back up, we
02:14:39
checked the condition again, every single time we go through the loop, we check that condition. So before we execute the loop, we execute that condition, we go back and check it again and execute the code again. Eventually, we got to the point where index was equal to six. So we came up here to check this condition and six is not less than or equal to five. So Then we just came down, we broke out of the while loop, and everyone was happy. So that's essentially what we did. And that's what a loop is basically you specify a condition, as long as that condition is true, then you're good to go. And we can pretty much just keep looping through all the code in there. And there are tons of situations where using loops is gonna come in handy. And in the next tutorial, we're actually going to build a little game where we can use a while loop. But before we do that, I want to talk to you guys about a couple more things with these loops. The first thing you need to watch out for when you're using these loops is something called an infinite loop. an infinite loop is a situation where this condition up here never becomes false. So in certain circumstances,
02:15:42
like the condition that you specify up here won't become false ever. And then your loop will just continually loop through. So for example, if I was to get rid of this line of code right here, where we're incrementing index, now the index is always going to be one. And so this condition up here is never going to be false. So when I run my code, you'll see over here that we're just continually printing out one, you can see the screen keeps flashing, really what's happening is we're just looping, looping, looping, looping through that while loop, and it's never going to stop me I can leave this on for years. And it's never going to stop, right because that condition is never going to be false. So you always want to make sure that any conditions that you write with these, while loops at some point are going to become false. Now, there are certain circumstances where you're going to want a while loop. But I would say for the most part, while loops aren't going to be necessary. And they can actually be dangerous, especially if you're doing something where you're like creating variables or allocating memory, like you can end up crashing your program or even crashing your computer if you're not careful. So that's just a little
02:16:45
tip about infinite loops. There's one more type of loop that I can show you in this tutorial. And it's a different type of while loops, kind of like a different flavor of while loop. But before I show you that, I just want to show you a demonstration. So if I make this int index equal to six, when I run my program, the code inside of the while loop is never going to execute. And that's because before we loop through all of this code, we check this condition first. So before I'm allowed to execute any of this code, I check the condition up here in these parentheses. So I check to see if index is less than or equal to five, since index is starting out at six. We're never ever going to execute this code, right. So if I ran my program, you guys will see nothing gets printed out, right. So we didn't do anything. But there's another type of loop, which is called a do while loop. And I want to show you guys how we can make one, I'm just going to cut this and I'm going to paste it down here. And I need to include a semi colon. And then up here where that while used to be I'm just gonna say do and this is what's called a do while loop. And a while loop is
02:17:47
essentially just a normal while loop, but it's reversed. So instead of checking the condition, and then executing the code, we're going to execute the code, and then we're going to check the condition. So it's just sort of like the while loop reversed. So now even though index is equal to six, I'm still going to be able to execute this code first, then we're going to check the condition and it's not going to let me do it again. So let me show you guys what happens when I run this. And you'll see over here, we're able to print out that six. That's because with the do while loop, you execute the code in the loop before you check the condition. And there's a bunch of different situations where you can use while loops or do while loops. And to be honest with you everything that you can do with a do while loop you can do with a normal while loop. So do our loops aren't technically necessary. But in a lot of circumstances, they are useful. So that's why they're included in the language B I mean, that's a basic overview of loops. There's actually another type of loop that we can talk about in a future tutorial called for loops. But for now, you know, practice
02:18:49
with these wild loops, you can kind of see like how they work. In this tutorial, I'm going to show you how to build a little guessing game in c++. And this is going to be cool because we're actually going to be able to use some of the stuff that we've been learning up to this point in the course. So we'll use things like while loops, if statements, we'll use variables, we use all sorts of stuff in order to build this game. So essentially, how this is going to go is the user is going to try to guess a secret number, right? So I'm gonna have a secret number stored here in my program. And the user's job will be to try and guess what that number is. So the concept of the game is pretty simple. And as you'll see in a second, it's actually pretty simple to program something like this. But here's the one caveat is if the user doesn't get the guests right on the first try, I want to keep asking them keep prompting them to enter in another guest. So I'm going to show you guys how we can do this. So the first thing I want to do is create our secret number. So I'm just going to make an integer and I'm just going to call it secret num. I'm just
02:19:51
going to set this equal to I don't know, let's say seven. So seven is going to be our secret number. And then the next thing I want to do is create another integer which we're gonna call guests, and we're not going to give guests a value right up front. So now we have our secret number, and we have our guests. And we want to be able to keep asking the user to guess what the secret number is until they get it right. So whenever something needs to be done continually, that's usually a good sign that we want to use a while loop. So I'm going to go ahead and say, while when make an open and close parentheses, and I'm gonna make an open and close curly bracket. So inside of this while loop, we want to continually ask the user to enter in the number until they get it right. So what I want to do here is specify a condition. In other words, I want to keep looping Wow, the secret num is not equal to the guests. So as long as the guests is not equal to the secret nom, in other words, as long as the user hasn't guessed correctly, I want to keep going through this loop. Now, every time through the loop, I want to prompt the user to input a number.
02:20:55
So I'm just going to say C out. And we'll basically just say, enter guests, so they're going to enter the number that they want to guess. And then down here, we want to be able to get that answer, we want to store it in a variable. So I'm just gonna say C n, two greater than signs. And I'm just gonna put guests here. So basically, what we're telling see is that we want to take whatever the user enters, store it inside of the guest. So basically, every time we go through this loop, the user is going to be guessing, and then we're going to check their guests against the secret num. If they got the secret num, right, then we'll just break out of the loop. So down here, if they end up getting out of the loop, that means they got the guest right, so I can just print out, you win, so we'll give them a success message. Alright, so this is essentially all we need to build our guessing game. So I'm going to go ahead and run this program, and we'll see how we did. Alright, so it says enter guests, so I'm just going to enter in some numbers. So you can see I entered in a three, that wasn't the secret number, so I can keep entering in different guesses.
02:21:55
And no matter how many times I guess, it's gonna keep asking me to guess until I get it right. Now let's try to get the guests Right, so the secret number was a seven. So when I enter a seven in now, all the sudden the condition that was in that while loop is going to be false. So the secret num is going to be equal to the user's guess. And we'll break out of the while loop. And it's going to say you win. So that's essentially how we can build our little guessing game. Now I will say down here, I created this game using a while loop, right. But there's actually another way that we can build this using a do while loop. So your homework for this tutorial is to go off and see if you can do this using a do while loop instead of just a normal while loop. Alright, so here's the thing, this is actually a pretty fun game, and it works out we were able to program it really easily. But the game is a little bit unfair. It's unfair in the sense that the user gets essentially unlimited guesses. So they can guess as many times as they want. And honestly, that doesn't seem fair. So what we could do is we can impose some sort of a guess limit. So I can say,
02:22:59
hey, the user is only able to guess like three times and then they lose the game, right. So I'm going to go ahead and show you guys how we can actually program something like that, we're going to need to add in a couple extra variables, the first variable I'm going to add in is going to be called guest count. And this variable is basically just going to keep track of how many times the user has tried to guess the secret numbers. So every time the user guesses the secret number, we want to increment the guest count. And I'm actually going to set this equal to zero initially, because the user will have guessed zero times, then down here, every time the user guesses, I'm going to increment the guest count. So I'm just gonna say, guest count, plus plus. So now we're basically able to keep track of how many times the user has tried to guess the secret number. All right, we're gonna make another variable over here. And I basically just want to call this gas limit. And guess limit is going to specify how many times the user is able to guess until they lose. So
02:24:01
why don't we set this equal to 30. So we'll say that the user gets three guesses, they can't get the word and three guesses, then they're going to lose the game. Alright. And then one more thing, we want to create another variable, which is going to be a Boolean, and this is going to be called out of guesses. And I'm going to set this equal to false initially. So out of guesses will tell us whether or not the user has any guesses left. If out of guesses is false. That means the user has guesses left, right? Because they're not out of guesses. If it's true, it means they don't have any guesses left, because they ran out of guesses. Alright, so now that we have these variables, I want to show you guys how we can modify this a while loop. So down here, every time we go through this while loop, you'll see that we're letting the user guessed the number. But here's the problem. If the user doesn't have any guesses left, then we don't want them to be able to guess the number. Right? In other words, if they've reached the guessed limit, we don't want to give them another guess. So what I can do here is I can actually use an if statement and what I'm going to say is if guest count is less than guest limit. So if the guest count is
02:25:07
less than the guest limit, then I'm going to let them guess, right? Because if this condition is true up here, that means that they still have some guesses left, right? They haven't reached the guests limit. So then they're going to be able to guess what increment the guest count, etc. Otherwise, though, if the guest count isn't less than the guest limit, that means that they ran out of guesses, right, so what I want to do is I want to toggle this Boolean. So basically, I want to say out of guesses is equal to true. So I'm telling my program that they ran out of guesses. Alright, so once we've set this equal to true now, we're basically done inside of this while loop, right? So every time we go through, we're checking to see if they have any guesses left. If they do, then we let them guess. Otherwise, we tell our program that they're out of guesses. There's one more thing that we have to do to this while loop which is modify the condition. And up here in this while loop condition, we can use this and operator. And this and operator will allow me to put two conditions inside of there. So I could say while secret num is not equal
02:26:12
to guests, and we can check another condition. So what I want to do is I want to check to see if they're out of guesses. So I want to check to see if out of guesses. And I want to check to see if they're not out of guesses. So we're going to keep looping as long as they haven't guessed the secret word. And they're not out of guesses, right if one of those becomes false, so if they run out of guesses, or if they guessed the secret word, then we're going to break out of the loop. So that's essentially what's happening here. Alright, so one more thing down here below the while loop, we have this C out, and we're just printing out you win. But this isn't technically correct, because if the user runs out of guesses, then they're going to break out of the loop. But they didn't win. So we shouldn't tell them that they won, we should tell them that they lost. So what I'm going to do is make another if statement. And I'm going to check to see if out of guesses. So if out of guesses is equal to true, then I just want to print out a you lose message.
02:27:13
And otherwise, if it's not equal to true in other words, if they had guesses left, that means that they want so that means they guess the secret word. Alright, so let's go ahead and run this program. And we'll see how we did. Alright, so down here, it says enter a guest. So why don't we try to lose the game. So I'm just gonna say three, four, and two. So this is our last guest. If we don't get it on this try, that means that we lose. So you lose, right? So we lost the game. Alright, so looks like it's working. So now instead of being able to guess infinite times, we can only guess three times. Let's see if we can win the game now. So we're gonna guess like a three, a two. Now we're on our last guests, I'm gonna say seven. And when I enter this, it says you win. So we were able to win the game, as well. So this is sort of how we can use all of these different programming structures together. So you'll see down here, not only that, I use this while loop. And not only did I use all these variables, but I also use if and else in combination with
02:28:15
all that stuff. And really, this is what real programming is going to look like, you know, it's not just going to be like little simple examples, it's going to be a little bit messy just like this. Not that this is messy code. But we want to be able to use all of these different programming structures in combination. There's also one more thing I want to tell you guys, which is the way that I wrote this little algorithm, this little function here, we could have written this like a dozen different ways. So I could have programmed this game. And you know, I can think of like at least two or three other ways that I could have done this. And this is another lesson in programming is that generally there's going to be like five or six different ways to do the same thing. So I could have done this with a do while loop, I could have used different things I could have used like a break statement or something else. And so that's one of the cool things about being a programmer is it's going in and trying different ways to do the same thing. But that's basically how we can build this guessing game. Hopefully, you guys learned a little bit about seeing all those different structures like ifs and Wiles, and variables and all that stuff used together. In this
02:29:21
tutorial, I want to talk to you guys about for loops in c++. a for loop is a special type of loop, which actually allows us to keep track of how many times we've gone through the loop. So in a for loop, we can use something called an indexing variable. Basically, that indexing variable will allow us to keep track of how many times we've gone through the loop. And you can also use the indexing variable to do a bunch of other stuff too. So I'm going to show you guys how for loops work, we're going to look at why they're useful and in what situations you're going to want to use them. So over here, you'll see that I have this basic program set up and essentially I just have a while loop over here. So I have this a integer called index and I just set it equal to one. And then while index is less than or equal to five, I'm just printing out the value of index. And then I'm incrementing it. So I'm gonna go ahead and run this program, you guys can kind of see what's happening. So you'll see, we're basically just printing out 125. So this is a very simple while loop. And if you've been following along with this course, and you've you know, hopefully you're familiar with what while loops do at this point. But I want you guys to notice a couple
02:30:25
things. So this while loop is special, because every time we go through the loop, this variable index tells us what iteration of the loop we're on. So when I run this program, you'll see over here, like the first time we go through the loop, index represents one right index has the value one, the second time we go through the loop index has the value of 2/3, time has the value of three, etc. Right? So this index variable is essentially telling us what iteration of the loop we're currently on. Right. In other words, on every iteration of the loop, the value inside of this index variable is changing. So it's in our case, it's incrementing, by one, but the point is, is that we have this variable, this index variable, and it's essentially just keeping track of how many times we've gone through the loop. Now, in c++, and in just about every major programming language, this is a very, very powerful looping structure. In other words, there's a lot of situations where keeping track of you know how many times we've gone through the loop, or even just having a variable
02:31:29
that's going to change every time we go through the loop is very useful. And in fact, this is such a useful situation, that there's actually a special loop just to do something like this. So there's a special type of loop whose sole purpose is to provide us with a structure very similar to this, where we have this indexing variable that is going to change every time we go through the loop. And in a lot of circumstances, it's actually going to represent what iteration of the loop we're currently on. And that's called a for loop. So I'm going to show you guys how we can build a for loop. And essentially a for loop is just going to be exactly what we have up here, it's just going to be a lot more compact and a lot easier for us to use. So it's great if we're live, we can just say for making open and close parentheses and open and close curly bracket. So so far, it looks exactly like the while loop. Now the one difference with the for loop is instead of only having one thing up here in the parentheses. So in this while loop, we just have our looping condition write, a lot of people call this like a loop guard. Down here in this for loop parentheses, we're actually going to have
02:32:30
three different things that we want to include. So the first thing that we want to put inside of this for loop parentheses is a variable declaration variable instantiation. So up here, you'll notice that I'm creating this indexing variable called index, and I'm giving it a value of one. So I'm declaring the variable and I'm assigning it an initial value, right, I'm initializing the variable. inside of this for loop parentheses, we can do exactly that. So I could actually just copy this line, and then down here, I can just paste this in here. And instead of calling it index, why don't we just call it AI. So AI is gonna stand for index, we'll set it equal to one, then you'll notice I have this semicolon here, after I put this semi colon, we can actually put another line. So another thing that we have in this wild looping structure is our loop guard, right, this loop condition, it's essentially telling us when we should loop so if this condition up here is true, then we want to keep looping. So that's the next thing that we want to put inside of this for loop parentheses. So we're putting this variable declaration and initialization and then down here, we're
02:33:36
also going to put the looping condition. So this is going to tell the loop when it should stop and when it should go. Alright, so then what I'm also going to put another semicolon here. So you'll notice I have a semicolon here, after we create this variable. And I have another semicolon here, after we specify the looping guard. The next thing I want to do is put in a line of code that's going to get executed after each iteration of the loop. Up here in this while loop, you'll notice that we have this line of code over here, index plus plus, this line of code is going to get executed at the end of every single loop iteration. So the last thing that we're going to do, when we go through this while loop is increment the index variable, right. So that's essentially what we're going to put over here, we're going to put a line of code, which is going to get executed after every iteration of this loop. And in our case, we're just going to increment this variable i so I'm just going to say i plus plus, we're adding one tie. Now again, I can put essentially, any line of code here that I want, I mean, I can put like i minus minus, I could do,
02:34:39
you know, basically whatever I want, but in our case, we're just going to increment i just like we did up here with index. So now inside of this for loop body inside of these open and close curly brackets, we can just put whatever we want to do on each iteration of the loop. So that's going to be this line up here. So I can actually just paste this guy in right here. And now we have a completed for loop. So believe it or not, this loop down here is actually equivalent to this loop up here, you'll notice this took 1234 lines of code, whereas this only takes two lines of code. So like I said, this is such a common scenario where we want to have a variable like index, which is, you know, essentially changing every time we go through the loop and can allow us to keep track of things like the current loop iteration, such a common situation that there's a special loop called a for loop for that. And the for loop, the first thing we do is create the variable just like I did up there. So I'm initializing this variable. The next thing we do is specify
02:35:42
the looping guard the looping conditions, that's what I did here. And then we specify a line of code that's going to get executed after every iteration of the loop, which is going to be this i plus plus. And you'll notice that these are all separated with semi colons. And you'll notice also that I don't need a semicolon over here. So let's actually get rid of this while loop. And I'm going to go ahead and show you guys what happens when we run the for loop. And actually, I need to change these to eyes. So instead of this being index, I'm just gonna leave it as i and this is also going to be I, so we have int is equal to one, we're going to keep looping as long as i is less than or equal to five. And then at the end, we're just going to increment it. So we're gonna say i plus plus. And every time through the loop, we are printing out, I so let's go ahead and run this code. And you'll see we get the same exact output as we did with the while loop. So it's just still just 12345. So what's cool about these four loops is we can essentially, like keep track of a value every time we go through. And in our case, we're basically just keeping track of how many times we've gone through the loop, I want to show you guys how we can use these
02:36:46
for loops to actually iterate through the contents of an array. So let's say I created an array of integers, so we'll just call it like nums. And I'm just going to give this an initial value. So why don't we just say 12573. So I have this array of integers. And it just has a bunch of numbers in it. And if you recall, with integers, if we wanted to access a specific element inside of this list, I could just say like nums, and then put the index in here. So if I said num zero, this is going to give us access to this one, if I said nums, four, it's going to give us access to 01234, it's going to give us access to this three, what I can actually do is I can modify my for loop down here in order to actually loop through and print out all the contents of this array. So array indexes start at zero, so I'm going to start this I variable at zero. And what I'm going to do is I'm basically going to say i is less than the number of elements inside of the
02:37:48
array. So inside of this array, we have 12345 elements. So I'm going to say while i is less than five, we want to keep looping and then I'm just going to say i plus plus. And down here, instead of printing out, I could actually just print out nums ai. And what this is going to do is on the first iteration of the loop, it's going to print out nums zero, on the second iteration of the loop, it's going to print out nums one, on the third iteration of the loop, it's going to print out nums, two, etc. And it's going to do that all the way up to number four, which as we saw is going to be this three. So that's going to be the last element in the array. So we can actually use this for loop to loop through all the elements in the nums array. So let's go ahead and do that. And you'll see over here, we're printing out all the elements. So we're printing out 12573. So that is a very common situation, we can use for loops to iterate over the elements inside of an array. And it can be really useful. So play around with these for loops, these are extremely
02:38:52
useful. And one more time, I just want to go over the structure. So the first thing we're doing over here is we're creating a variable or we're initializing a variable, so I'm saying int is equal to zero. And then I'm specifying my looping guard. So my loop condition, I'm saying we're gonna keep looping as long as i is less than five. And then this is a line of code that will get run after every iteration of the loop. So every time we go through this loop, we'll come up here, run this line of code, and then we'll check the condition. Again, that is the basics of a for loop. This is a very useful loop, I would say this is almost even more widely used than a while loop and a lot of situations. So you definitely want to practice and play around with these four loops. In this tutorial, I'm gonna show you guys how to build an exponent function in c++. an exponent function is basically a function that will take a number to a specific power. So I could write a function that I could pass in two numbers, like if I passed in three and four. This would basically give me back three race to the fourth power. So I'm gonna show you guys how we can build this
02:39:57
and we'll actually get a chance to use four loops. So we can use a for loop in order to build this function, that's going to be pretty cool. So, up here, I'm going to create my exponent function. So we're going to have this use a integer, so I'm just gonna say int. And why don't we just call this power because it's taking a number to a specific power, and make an open and close parentheses and open and close curly bracket. Now our power function is going to take two arguments. So it's going to take a base number, and it's going to take a power number. And we're basically going to take the base number to the power of the power number. So I'm just going to say int, and then and power num. Now down here in our power function, we need to figure out how we can do this. So I basically need to take bass num to power num. And we can use something called a for loop in order to do that. So the first thing I want to do is just create an a variable, so I'm just going to call it result. And I'm just going to set this equal to one. And then down here, I'm
02:40:59
basically just going to return results. So essentially, our goal inside of this function is to get rid the result variable equal to the value of the base num raised to the power number, right, that's kind of our goal. Throughout this function, let's just say for the purposes of simplicity, and the purposes just for this tutorial, that we're only going to be able to handle positive number exponents. So we're just going to go ahead and assume that pounamu is going to be positive. And now let's just make it a lot simpler and kind of help me to drive home the point a little bit better, what we're going to need is a for loop, and I'm going to go ahead and write out this for loop. Now up here in the parentheses, we need to specify three things, the first thing I need to specify is a variable. So I'm just gonna say int i is equal to zero, I'll start this out at zero. Now what I need to do is specify a looping a condition or a loop guard. So I want to keep looping, as long as i is less than the power num. Essentially, what I'm saying here is I want to go through this
02:41:59
loop power num times. So as many times as the power num specifies I want to go through this loop. So if power num is three, I want to loop through three times, if panamas five, I want to loop through five times. That's basically what this is saying. And then over here, we can just specify something that we want to do after each iteration of the loop. So I'm just gonna say i plus plus. And basically we'll just be incrementing, the value of i every time we go through the loop. Alright, so down here inside of our for loop, we need to think about what we can do. So where are we already know that we're going to be looping through here, power num times, right? So what I think we should do is, we should multiply result times the base num, every time we go through this loop. So what I'm going to say is result is equal to result times base num. And the reason that this is going to work is because let's just kind of break this down, the first time we go through this loop, right? result is going to be equal to one. So the first time we go through this loop, we're
02:43:00
multiplying base num by one, so we just get based on the second time we go through the loop result is equal to based on right, in other words, result has the value of base num. So the second time we go through, we're essentially multiplying base num times based on based on squared, right, the third time we go through this loop result has the value of base num squared. So for molting multiplying base num squared times base num, that'll give us base num cubed. So that's kind of why this is going to work, right. So we're looping through this for loop power num times. And every time through the loop, we're multiplying result times the base num. And you can see this for loop is very useful, because it allows us to only loop through the loop a specified number of times, right? It makes it really easy for us to only loop through, you know, power num times essentially. Alright, so this is the basic function. And this looks pretty good to me. So again, we're only going to this is only going to be able to handle positive number, exponent, so positive number power
02:44:02
nums. Okay, so let's go ahead and call this. So I'm just going to say, actually, why don't we print out the result. So I'm going to see out power. And I'm going to pass in two numbers. So why don't we pass in two, and three, so we're going to go ahead and do two cubed. So now let's run this function. And you'll see we're getting eight. So two cubed is eight. Two times two is four, four times two is eight. Let's try another one. Why don't we do four squared. So now we should get 16. And you can see over there we get 16. So it looks like our little function is working. And basically the point of this tutorial was just to kind of demonstrate to you another way that we can use these for loops. So I'm using this for loop in order to essentially specify how many times I want to loop through something. And four leaves are really useful for that because we're keeping this count. So I is representing how many times we've gone through the loop. So on the third iteration of the loop I can tell us that we've gone through three times, essentially. And that's why this
02:45:04
is useful. And so you can see, it's actually really easy to do something like that it only takes up like two lines of code. So that's the basics of building something like a power function. And actually, this is kind of useful, because you can kind of see how a function like this might get put together. In this tutorial, I want to talk to you guys about two separate subjects in c++, the first subject we're going to talk about are going to be two dimensional arrays. And a two dimensional array is a situation where we have an array, where every single element inside the array is another array. So this is a pretty cool situation. And I want to talk to you guys about one other thing which are nested for loops. And a nested for loop is a situation where we have a for loop inside of another for loop. So this is gonna be kind of cool. And you'll see we can actually use these two subjects together two dimensional arrays, and nested for loops in order to build a kind of a cool program. So I'm gonna show you guys the basics of doing that stuff. First thing
02:46:05
I want to show you is a two dimensional array. So I'm going to go ahead and create an array of numbers. And I'm just going to call it number grid. And normally, when we create a regular array, we're just going to make an open and close square bracket. But since we're making a two dimensional array, I want to make another open close square bracket after this. Now remember, a two dimensional array is a situation where we have an array, where every element inside of the array is another array. And I'm going to show you guys how we can go ahead and set something like this up. So I'm going to give this an initial value. So I'm just gonna initialize this. And I'm basically just going to put a bunch of array elements inside of here. So the first thing we're going to do is create the first element in the array. Now normally, if I was creating an array, I could just kind of type out elements like this, right? You know, I can kind of type them out delineate them with a comma, and then we have our array. But with a two dimensional array, remember, each individual element is actually another array. So if I want to create one element inside of this
02:47:06
two dimensional array, I can basically go like this, right, so I can make another sort of mini array inside of here. And I'm actually just going to format this a little bit. And so now the first element in this number grid array is going to be an array with elements one and two. So number grid, first element is this array. And this array has two elements of its own. So I'm going to copy this, and we're going to make another element. So I'm going to put a comma here, because remember, we need a comma to delineate the different elements. And then down here, I'm just going to paste. So now we're going to do three and four. So we have 1234. And why don't we do one more, and we'll just do five, six. Alright, so essentially, what's happening here is we have our overall array, right, so these two curly brackets represent the number grid array. And inside of the number grid array, we have three elements. So this is the first element, this is the second element. And this is the
02:48:07
third element, all of which are arrays. And each one of these arrays has one, two elements inside of them. So whenever we're creating two dimensional arrays, we always want to specify the numbers over here. So inside of this first open and closed square bracket, we want to tell c++, how many total elements are in the number grid array. So in our case, we have three, because again, there's 123 elements. Then over here, we want to specify how many elements are inside of each array element. So you'll notice we have 121212, etc. So there's going to be two elements in each one of these arrays. And it's very important, especially when you're dealing with two dimensional arrays, that you specify these numbers, because otherwise you can get really confused, you know, and so by specifying Three, two, I know the basic layout, like I know that this array has three rows, like you can call these rows, and then it has two columns. So these would be like columns, essentially, we're building
02:49:11
a matrix. Alright, so that is a two dimensional array. And let's talk about how we can actually access some of these elements. So if I want to access some of these elements, I'm actually just gonna go ahead and print some of these out. So over here, I can just say number grid, and what I can do is I can make two open and closed square brackets, so I can go just like just like this. And inside of these open and closed square brackets, these first ones I want to specify the row. In other words, I want to specify the element inside of number grid that I want to access. So this would be the element at position zero, this would be the element at position one, and this would be the element at position two. So let's say we want to access this element right here. I can put zero here because it's inside of element zero. And then over here, I want to put the index of that element inside of the array, so it's going to be an index position 01. So zero again is corresponding to this overall element, and one is corresponding to this element inside of that array. Alright,
02:50:12
so now when we print this out, we should get a two. And you'll see over here, we get that two. Let's try one more, why don't we try to get this five over here. So five is going to be an element 012. So we're going to put a two over here, and it's going to be element zero, so we're just going to put a zero and now we should get that five, which we do. Alright, so that kind of shows you a little bit about how we can create a two dimensional array. And actually, this same concept can apply to like n dimensional arrays. So you could have a three or four or five dimensional array. And all these same concepts are going to apply. Obviously, the more dimensions you add, the more complex it becomes. But you can get pretty complex with arrays, just by using this technique. So that was the first thing I wanted to show you guys. And now I want to introduce another topic, which is a nested for loop. And nested for loop is a situation where we have a for loop inside of another for loop. And actually nested for loops can be extremely useful, because we
02:51:13
can use them to print out all of the elements inside of this two dimensional array. So basically, we could iterate through all the elements inside of here using a nested for loop, but I'm going to show you guys how that's going to work. So down here, I want to create a basic for loop. So I'm just gonna say for, and I'm just gonna say int i is equal to zero. Again, our goal here is to be able to loop through all of the elements in this array. And, you know, as I sort of create this for loop, just kind of stick with me, eventually, this is all gonna make sense. But what I want to do is I want to keep looping, while i is less than three. And I'm using three here, because I want this for loop to loop through all of these individual elements. So all of the arrays inside of the number grid array, and over here, I'm just gonna say i plus plus. So this for loop is responsible for iterating over all of these elements. So this first element, the second element in this third element, but remember, each of these elements is another array. So what I want to do is
02:52:17
for each of these arrays, I want to iterate over each of the elements inside of those arrays. So I can create another for loop to do that. So I can say, int, and I'm just going to call this J. And a lot of times when we're using nested for loops, people will name these ij, that's kind of a convention, I'm gonna set this equal to zero, and I'm going to keep looping as long as j is less than two. And remember, two is a how many elements are inside each of these arrays, right? So this loop over here is responsible for iterating through all of these elements, and this one down here is responsible for iterating through all of these elements. So I'm gonna say j less than two, and then I'm just gonna say j plus plus. So now we have our basic structure setup. For every iteration of this I loop, this j loop is going to fully iterate. So it's going to go through all the way. And down here, I'm just going to see out. And I'm basically just going to print out all these different elements. So I'm going to print out number grid, i j, and you guys will see in a second,
02:53:19
basically how this is going to work. And then real quick down here, I'm just going to see out a new line. So I'm just going to say C out and L and that'll print kind of a new line. So let's go ahead and run this. And then I'll give you guys more of an explanation as far as what it's doing. So when I run this, you'll notice that we're actually printing out all of these elements. So we're printing out 123456. So I'm actually able to go through and iterate over every single element inside of this two dimensional array. And basically what's happening is, every time we go through this top loop up here, this AI loop, we're fully iterating through the J loop. So this I loop is responsible for iterating over each individual element. So it's responsible for iterating over this, this and then this, this j loop is responsible for iterating over each of the elements inside of those elements, which are arrays. So the J loop is going to loop over this and this. So the first time we go through this loop is going to be equal to zero and j is going to be equal to zero.
02:54:23
So we're going to print this out, then we're going to go through this j loop again, I is still going to be equal to zero, but j is going to be equal to one. So we're going to print this element out. And then AI is going to be equal to one. So it's going to be one zero, we'll print this out and then it's going to be one one. So we'll print this out. And that's essentially what's happening here. So nested for loops and even like nested while loops, I mean, any sort of nested loop can be very useful. And it can be useful for looping through like I said, complex structures like that. And this tutorial on talk to you guys By using comments in c++, or comments are a little special blocks of code, which aren't actually going to get executed or rendered by c++. So a lot of times in your programs, you're going to want to, you know, basically write things that are meant for human beings. So whenever we're writing programs, like whenever we're writing code or instructions, those instructions are meant for the computer. So I can tell the computer to print something
02:55:24
out, or I can tell the computer to loop over a block of code or something like that. But a lot of times, we're going to want to write out messages just for human beings, so either for yourself or for someone else. And we can do that using something called a comment. And I'm gonna show you guys what comments are essentially, a comment is just a, you know, a little line of code or a block of code that c++ is going to ignore. And therefore we can use it to write little messages for ourselves. So in order to create a comment in c++, there's actually a couple of ways we can do it. The first way is just to make two forward slashes. So when I make two forward slashes, you'll notice that when I start typing, all of this text is actually colored different than the text over here. That's because this is a comment. So any text that I put after these two forward slashes is going to be considered a comment. And it's not actually going to get rendered by c++. So even though I have a bunch of nonsense up here, I can still run my program, and it's still going to be able to execute. So you'll see I can run the program. And there's no errors, it's just printing
02:56:24
out comments are fun. So this is a really great way to kind of write a little note, a lot of people will use this to write notes to themselves, or maybe write notes to other developers. You can also use this to like describe a line of code. So I could like describe this line of code over here, I can say like, print out text, right? Obviously, you know, you probably wouldn't need to describe this line. But if you had like a complex function, or you know, complex loop or something like that a comment can come in handy. You can also put these comments like after a line of code, so I can just cut this out and paste it down here. And you'll see that everything that comes after these two forward slashes is going to be considered a comment. But I can still put code over here. And again, you can use this to like describe what the line of code is doing or something like that. The one downside to this type of comment, though, is that it only works on one single line. So if I come down here to the new line, all of a sudden, this goes back to normal text. So this is no longer a comment. If you want to have a long block of comments, though,
02:57:26
you can use something called an opening and closing tags. So if I make a forward slash, and an Asterix, then everything that comes after this, even if it's on a different line is going to be considered a comments, you can see all of this stuff is now a comment. But I can close this off by saying an Asterix and another forward slash. But now everything that's in between these two tags is going to be considered a comment. And these two types of comments are very common. And you know, pretty much either one you use is going to be correct. And it's going to work out. So that's basically how the you know the two types of comments and how you can use them. Another way that comments are useful is for commenting out lines of code. So a lot of times when you're working with your programs, maybe you want to try to run your program without a certain line of code. And you might be running into a problem and you think a certain line of code is the culprit? Well, one option would be to just remove the line of code. So for example, let's say that this line of code was potentially like messing up my program. And I wanted to see how the program would act without that line of code. Well, I could just delete it, save the file,
02:58:32
and then run the program. And now we're running the program without that line of code. The problem with that, though, is I have to physically delete the line of code, right? If I wanted, though, I could just put a comment in front of it. And now this entire line is going to get ignored by c++. So it's essentially the same as removing it, but we don't actually have to physically remove it from the file, it's still gonna be there, we can still see it, we still know that it's there. It's just not going to get executed. So that's another useful way that comments can come in handy. So yeah, I think a lot of developers use comments, you definitely want to take advantage of them in your programs when they're appropriate. In this tutorial, I want to talk to you guys about pointers in c++. Our pointer is basically just a type of information that we can work with in our programs. If you follow along with this course up to this point, we've been working with all different types of data. So we were working with things like integers, which are whole numbers, we've been working with like doubles, which are decimal numbers when working with strings and chars which
02:59:35
are like plain text. And this is where I want to introduce you guys to another type of information we can deal with which are called pointers. And pointers are basically just memory addresses. So we're going to have get into what that means. What is a memory address, we're gonna talk about all that stuff. And I'll give you a full introduction into pointers. So over here in my program, I have a few different variables that I created. I have an integer called age and I gave it you know, a value Double college GPA and a string called name. Now all of these are variables in my program, right. So in other words, all of these are essentially just containers where I'm storing specific information. For my purposes, when I'm writing this program, I'm storing the value 19, inside of this age variable, right, I'm storing 2.7 inside of this GPA variable. But let's kind of, you know, go underneath the hood a little bit and talk about what's actually going on here. So in everyone's computer, you have something called your memory. And another word for this would be Ram. So you might have heard people talk about ram stands for random access memory, it's essentially
03:00:38
just the active memory that your computer's using when it's running programs. So whenever your computer runs a program, it's using Ram. So it's storing information inside of this memory, let's just say our program, for example. So in the case of this program, if I was to run this program, my computer would actually be using Ram. In other words, my computer is going to use its memory in order to store and keep track of information. In fact, it's going to be storing all of these values inside of its memory. So when I create this variable age, and I give it a value, what's actually happening is my program is going to take this value 19. And it's going to store it inside the physical memory of my computer. When I create this double GPA, my computer is going to take this number and it's going to store it physically inside of the memory in my computer. Same goes for that string. And so for all of these different pieces of data that are all actually getting stored inside of the
03:01:39
memory of my computer, so when I write the program, like for my purposes, I just know that 19 is stored inside the age variable. But when we go underneath the hood 19 is actually stored in side of the computer's memory, so it's physically stored in the memory. Now one of the cool things about the memory of our computer is there's a bunch of these little containers, right, each one of these variables, each one of these values is essentially just like inside of one of those containers, right? That's kind of a broad strokes explanation, but you can basically think of it as like this number 19 is stored inside of a container in memory, this value 2.7 is stored inside of a container in memory. And each of the containers inside the memory of my computer where these values are stored has an address. So it has an address, which uniquely identifies it. So for example, this value 19 is stored inside the physical memory of my computer, and it's stored at a specific memory address. This value 2.7 is stored in the memory of my computer, and it's stored at a physical
03:02:40
memory address. And so when my program wants to access this value, it can access it by using that memory address. When I want to access the variable, I can just access it using like age or GPA. But when my computer wants to access it, my computer has to access it using the physical memory address. So that's kind of like what's going on. And I want to show you guys how we can go ahead and access that physical memory address. So I can actually print it out, I get to say C out. And if I wanted, I could basically print out the memory address where each of these variables are stored. In other words, I could print out the memory address where this value 19 is stored, I can print out the memory address where this value 2.7 is stored. All I have to do is just make an ampersand and then I can type in the name of a variable. So I could type in like age for example. And what this is going to do is when I put this ampersand here, it's essentially going to tell c++ that I want to print out the memory address where the age variable is stored. In other words,
03:03:42
I want to print out the memory address where this value of 19 is stored. So when I run my program, you'll notice over here that I'm getting this kind of crazy number. So 0x, six, a FPS zero. This is a hexadecimal number, basically, it's just some long number. That will be very difficult for a human being to remember. But the computer is able to remember it just fine. So if you were to go to this physical memory address inside of my computer, you would find the value 19. I mean, that's essentially what this is doing. Right? So that's basically like where that value 19 lives inside of our memory. Right. So if I wanted to access or change it or modify it, my program can do it using that address. And that's essentially what we're talking about. And in c++, we have a word for these addresses. So we have like a special word that we use when we're talking about addresses. It's called a pointer. So generally, if I'm working in c++, and I want to talk about a memory address, I'm going to talk about pointers. So I would say that this is a pointer, right. So over
03:04:43
here, we can say that I'm printing out a pointer. And a pointer is just a type of information. It's just a memory address. So any memory address that we're working with, or that we're using inside of our programs is going to be considered a pointer. And the way that we can access the pointer of these variables or in other words, the way We can access the memory addresses where these variables are stored is by using this ampersand. So I have a little demonstration I want to show you guys, this is just a block of code that I wrote out before the tutorial. And it's essentially just printing out all of the memory addresses for all of these variables. So I'm going to go ahead and run this. So you can see over here, I'm essentially just printing out all the individual memory addresses for all of these variables. So we have age, and it's located at this memory address, we have GPA at this memory address and name at this memory address. So all of these memory addresses, if we were to go to them in our like physical RAM and the physical memory of our computer, we would see those values. And again, we would call these pointers. So this is a pointer,
03:05:44
this is a pointer pointer is just a memory address that is just a type of data, we're just giving it another name. So that's kind of cool. And that kind of shows you guys how we can access like the memory addresses of these different variables. But we can actually take this a step further. So I want to show you guys another thing that I can do. So I can actually create a variable where I can store the pointer. So over here, like I'm creating an integer variable, and inside of it, I'm storing an integer right over here, I'm creating a double variable, and I'm storing a double over here, I'm creating a string variable, I'm storing a string. And actually, if we want to manage and keep track of the memory addresses or the pointers inside of our programs, we can create a pointer variable. So I'm going to show you guys how we can do that. And a pointer variable will basically be just a container where we can store a pointer, right, it's a container where we can store a memory address. Generally, when we're working with memory addresses in our programs, we're not just going to work with random addresses, right. In other words, like I don't know,
03:06:46
any, like meaningful memory addresses off the top of my head. So generally, when we're using memory addresses, we're going to be using the memory addresses of the different variables in our programs. So therefore, when we create an actual pointer variable, in other words, when I create a container, what I'm going to store a pointer, I'm generally going to create it based off of one of these variables. So let's go ahead and create a pointer variable that will store the pointer. For this age variable, I'm just going to say int. And whenever we create a pointer variable, you always want to use this special character, which is going to be the Asterix, and then you want to type in the name of the variable. So I'm just going to call this P age. And a lot of times when you're creating pointer variables, you'll use this lowercase P. And then you'll type out the name of the variable whose memory address you're storing. So I could say int p age is equal to and now I can just say ampersand age. So remember when we use this ampersand, and then we type out the name of the variable that gives us the pointer. In other words, that gives us the memory address where the
03:07:47
value is stored. So I'm essentially storing this pointer inside of this variable over here called p age, I could do the same thing down here for this double, I could just say double Asterix, and I'm just gonna say p GPA. And I'm going to set this equal to ampersand GPA. So now this pgpa pointer variable is storing inside of it a pointer. In other words, that storing inside of it a memory address, and it just happens to be the memory address of this GPA variable. I can do the same thing for this string down here. So I can say string p name. And actually don't forget to put the Asterix and I can just set this equal to ampersand name. And now this pointer variable p name is storing the pointer. In other words, it's storing the memory address have this name variable. So now, I can actually work with these different pointer values using these pointer variables. So if I was to come down here, I could say C out. And I could just print out like p age. And now this is going
03:08:49
to go ahead and print out the value that's stored inside of that pointer variable, which is going to be a pointer, which should be the memory address of Ph. So you can see down there, that's exactly what we get. So that's kind of useful. And, you know, really, I think a lot of times people get a little intimidated or maybe confused with pointers. But pointers are really simple. A pointer is just another type of data that we can work with in c++. So you know, you can work with things like integers, which are whole numbers, you can work with doubles, which are decimal numbers, you could work with strings, which are just a bunch of characters, you could also work with pointers, which are memory addresses, you know, that's all it is. It's just a different type of data. And we when we create a pointer variable, it's just a container where we can store a pointer. That's kind of all it is. Now I want to point out to you guys, whenever I created this pointer variable, you'll notice that I use the data type of the variable that I was pointing to. So this pointer variable is storing the memory address of an integer. Therefore I said int over here, this pointer
03:09:51
variable is storing the address of a double. So I said double over here. Same thing down here. This is storing the memory does have a string. So I said string over here. That's basically just what you have to do when we're making pointer variables. This kind of brings me to another point I want to show you guys one more thing we can do, which is called dereferencing, a pointer. And dereferencing. a pointer basically means that we're going to grab the value that's inside of the memory address. So remember, a pointer is a memory address. So if I have a pointer, that is a the address of a physical, you know, slot or a physical location, in my computer's RAM, and my computer's memory, that's all it is, it's just an address, right? Like you have an address for your house. That's kind of what this is, it's an address. But instead of for a house, it's for a memory location. And when we dereference a pointer, we're basically telling c++ that instead of using the actual pointer, we now want to go to that physical memory address, grab the value out of there and use it in our program. And the way that I can do that is by dereferencing. it. So
03:10:52
you'll notice, like when I print out p h, right, this is a pointer. So when I, when I run this program, and I print it out, we get a memory address. But if I was to dereference, this pointer, we would actually end up getting the value that was stored at that memory address. So if I put an Asterix here, this is what's called dereferencing, a pointer. So I could say Asterix p age, and now we'll be dereferencing, the P age pointer. So now instead of getting that memory address, when I run this program, we're going to get the value 19 because that is the value at the memory address that the pointer was storing, and the pointer was representing. So I'm here in my program, and you'll see down here we get 19. So when I get rid of this Asterix, we get the memory address, when I put the Asterix here, we're dereferencing the pointer. So we just get 19. And that's kind of how that works. So hopefully that makes sense. I'm gonna show you guys one more thing. So dereferencing is really useful. And if I just said, for example, like ampersand GPA, right? GPA is this variable up here,
03:11:56
it's storing the value of 2.7. This is a pointer, right, so this is going to give me the memory address. So over here, when I run this, we get this memory address. But I could just dereference this, and hopefully this kind of shows you guys what we're doing. So I can just put an Asterix here, and this is going to dereference, this entire thing. So now this Asterix is dereferencing, this pointer, and we should end up getting the actual value, which is 2.7. So you can see we get this value. And you can I mean you can kind of chain these together. Not that you'd want to but I could say like ampersand, and now we'll get the memory address again. So that's kind of how that works. And, you know, pointers. And, you know, working with pointers, doing stuff with pointers can be really useful in c++. This is also a very useful concept in another language, which is called C. And C is actually the programming language that c++ is based on. You use pointers a lot in that ceiling which but they're also important in c++. And if you're going to be a c++ developer programmer,
03:12:59
you need to have at least a baseline understanding of what pointers are and how they work. So hopefully that gives you a little bit of an idea of what they are. Remember, essentially, pointers are just types of information. It's just another type of data that we can use in our programs, we can print them out, we can use them, we can store them inside of variables, we can also dereference them, as you saw earlier in the tutorial. So hopefully that helps. And hopefully now you can kind of play around with pointers. In this tutorial, I'm gonna teach you guys about classes and objects in c++ classes and objects are extremely useful. And this is a very important topic in c++. So I'm gonna give you guys a full overview of what these are. And we'll kind of get an idea of how to create classes and objects. So down here in my little program just created a few different variables. And basically, what I'm doing here is I'm just storing different pieces of information, right, I'm storing my name, inside of this name variable, and we're storing it inside of a string. I'm
03:14:02
storing pi, the first three digits of Pi in this double variable. And then I'm storing my favorite letter G inside of this character variable. So just using these variables, were able to store a bunch of different types of information. And actually, if you know anything about c++ data types, you'll know that by default, c++ allows us to store a bunch of different types of data. So we can store like text. We can store numbers, we can store like whole numbers, decimal numbers, we can store characters, we can store all of these different types of information. But here's the problem though. And this is kind of a limitation to this is that there's a lot of types of information. In other words, there's a lot of things there's a lot of real world entities that can't just be represented using a string or a number or a character. Like there's a lot of things that we just can't represent with a string or a number or a character like, like a phone for example, like I can't really like represent a phone In my program using like a string or using just a number, or think of something
03:15:04
like a computer, or a keyboard, or an animal or a person, like there's all these, like real world entities, that it can't necessarily just be represented using the limited data types that we have, or we only have like, a certain number of data types that we can use. And those data types really aren't enough for us to necessarily model everything in the real world. So imagine I was creating a program, for example, where I wanted to work with books, like maybe I was creating a program for a library. And inside of that program, we needed to represent book like, I wanted to be able to work with books and store books and use books inside of my program. Well, there's no book data type, right? There's no, like, I can't just come over here and say, like, book, you know, my book, whatever, like, there's no book data type for me to use. So this is kind of a problem. And, you know, forget, forget about books for a second imagine any other, you know, real world object, like a person or a location, or a musical instrument, or really any type of like object
03:16:08
in the real world that I'd want to represent in my program. And so you know, really, the problem is we only have a limited data types, right, there's only certain types of data we can represent. And this is where classes and objects come in. So what we can do is we can actually create a class. And a class is essentially a new data type. So when I create a class, I'm basically creating a new data type in c++. And that's really why classes are useful, because we can kind of create a blueprint for a new type of data in our program. So like I said, before, you know, I can't just come down and down here and create like a book data type, right. But what I can do is I can create a class that will specify what a book data type is. And then I can actually use that book data type that I created inside of my program. So I'm going to show you guys how we can do this, let's write a program that will allow us to store and work with and represent books inside of our program. So we're essentially going to be creating a book data type. In order for me to do this,
03:17:08
I am just going to come up here. And I can do this in the same file as this main function. And I'm going to create a class and a class is essentially just a specification or a blueprint for a new data type in our program. So when I create a class, I'm basically creating a new data type, I'm creating a new type of data that we can work with, and we can use in our programs, it's pretty cool. So over here to create a class, I'm just gonna say class, and then I'm going to give this a name. And generally when we're naming classes, we are going to name them with capital letter. So I'm just going to make call this book. So we're going to create a book class. So this book class is going to act as a blueprint or a template for the book data type. Okay, so this is just a specification, right? We're basically specifying what a book is, inside of our program. So what I want to do down here is I want to actually define the book data type. And we can do that by giving it attributes. So we can basically say that a book is going to have like two or three or
03:18:10
five different attributes that will describe it. And we're going to use all those other data types like strings, integers, doubles, characters, etc, in order to represent those attributes. So essentially, this book class is going to be a collection of attributes, which are going to be things like numbers and strings, etc. So I'm just going to say public, and I'm going to make a colon here. And then we're going to come down here, and I'm actually going to end up this. So it's a little bit easier to see down here, right below where I said public, I want to specify some attributes. So essentially, I'm mapping out what a book is going to be and what it's going to have. So let's think about what are the different attributes of a book? Well, the first and probably the most obvious is going to be the title. And we can represent the title as a string. So I can just say string title. And I'm just declaring these variables, I'm not going to give them values. So what's another walnuts thing? Probably the author, right. So another attribute of a book would be the book's author. And let's try to think of one more. So I think another good one might
03:19:12
be maybe let's do pages. So like this would be the number of pages in the book. And I'm sure you can think of a lot more we could think of like publishing date, like a publishing company, you know, you could think of, you know, a version number. There's a lot of different attributes we could store for a book. But let's keep it simple for now. Essentially, what I'm doing here, as I'm mapping out a blueprint, so I'm saying this is the blueprint for the book, data type. And a book in our program can have a title can have an author and can have a number of pages associated to it. So this is a more of a complex data type. Really, it's a it's a class. So this is basically all we need. So we're essentially just specifying what a book is. We're telling c++ what a book is in our program. So now I'm going to come down here and I'm actually going to create a book. So remember, this is just a blueprint. This is a template, it's a specification. But this isn't like a physical book, if I want to actually, like have a physical book that I'm going to work with, in my program, I need to create it down here. And we're actually going to be creating something called an object.
03:20:15
So here's a little terminology lesson, a class is the specification, it's the blueprint, it's the template of a data type. So this book class is the specification, it's the blueprint of a book inside of our program, an object is an actual instance of that blueprint, or it's an actual instance of that class. So an object is an actual book. So I'm going to create an object down here, we'll call it a book object. And that means it's, it's an actual book that's going to have an actual title, and actual author and an actual number of pages associated to it. And we can create, you know, hundreds of these different objects, but just know that a class is the template, it's the specification. And an object is an actual instance of that specification. So it's an actual book with an actual title, author, and page. So let's go ahead and do that. I'm just going to say book. And again, I'm basically just telling c++, what type of data I want to create or what type of data
03:21:15
I want to store. And then I'm going to give this a name. So I'm just going to call it book one. So now we have this book inside of our program, it's called book one. And what we want to do is start giving it some attributes. So I want to say, book, one dot title. And we can actually give this a title. So I can assign a value to book one for the title. So why don't we just say the title is like Harry Potter. So this is going to be a Harry Potter book. And I can do the same thing for author and four pages. So I can say book, one dot author, and I can just give this an author. So it's JK Rowling. And we're going to give this a number of pages. So I could say book, one dot pages, and this is going to be an integer. So let's say it has like 500 pages. And so now I'm actually able to represent, and I'm able to work with a book inside of my program. So again, this up here is a class. It's a template for what a book is, this down here is an object, it's a physical book in our program that has actual attributes. So it has this is the Harry Potter
03:22:20
book, with the JK Rowling author and 500 pages. So I'll show you guys what we can do, we can actually print out this information. So I could say C out. And why don't we print out book one dot title. And so now I'm able to store all of this information inside of this book, one object. So I'm basically able to represent a book in my program, when I run this program, now, you'll see we're printing out the title of the book, which is Harry Potter, I can do the same thing for like pages. So we could print out the number of pages in book one, which is going to be 500. So this is a really cool way for us to actually model a real world entity like a book inside of our program. So remember, before, we didn't have a book data type, right, I had nothing I could use to represent a book in my program. Now all of a sudden, I have one. So I can actually represent a physical book in my program. And we can make as many of these as we wanted. So I'm actually just going to copy this. And let's make another book. So let's say in addition to this Harry Potter book, maybe we want to
03:23:20
make another one. And I'm just going to call this book two. So I'm going to change all of these. So now all of these are sets of Book Two, and I'm going to change all these attributes. Instead of it being a Harry Potter book. Why don't we make it a Lord of the Rings book, and the author is going to be token. And let's say that this is like 700 pages. So now I have two books in my program, I have book one. And I have Book Two, book one has all of these attributes associated to it, Book Two, has all of these attributes associated to it. But both of these books are using this same book template, right? They're using the same book class, this is just a specification for what a book is. And down here, I was able to create individual instances of that specification. In other words, I was able to create individual book objects. So I could also print out stuff about book two. So now I could say like Book Two dot author, and this is going to print out book twos author. So now it's token. So
03:24:24
just like before, when we have like integers, and you know, strings, and stuff like that. Now, in addition to those data types, we also have a book. So I can represent a book, I can use a book, I can store it, I can, you know, modify all its values and stuff like that. And, you know, down here, basically, we're just defining the book and then we're giving it value. So I can say like, book one title, and I can also change these so if I said book to title down here, I could change it so I could change it to like Hunger Games or something. And now, Book Two title is going to be updated. So these work just like normal variables would work. And you can see now we're getting Hunger Games. So that's pretty cool. And honestly, this is just a, an introduction into classes and objects. There's a lot more to learn about, there's a lot more to talk about. But hopefully this makes sense, what you want to do is just play around with modeling real world object. So in here, I modeled a book. So I created a book data type, but you could create like a phone data type. If you
03:25:25
create a like a keyboard data type, you can create anything, you know, essentially, you're just taking a real world entity, breaking it up into individual attributes like title, author, and pages, and then you can represent it in your program. In this tutorial, I'm going to teach you guys how to use constructors inside of c++ classes. Now, a constructor is a basically a special function that is going to get called whenever we create a an object of a class. So let me show you guys, basically how this works. So down here, or actually up here, I have this class, it's called book. And if you follow along with the course, we created this class in the last tutorial, and basically this book class is just sort of a blueprint or a specification for what a book is inside of our program. So you know, a book has a title, has an author and has pages awesome. So then down here, we actually created some book objects. And an object is just an instance of a class.
03:26:25
So we have book one, and book one has a title. It's a Harry Potter book, and an author in pages, and all that stuff. So I'm going to show you guys where constructors come into play. So a constructor, like I said, it's essentially just a function that's going to get called whenever we create one of these book objects. So up here, I'm actually going to go ahead and create a constructor. And the way we can create we can create a constructor is just by essentially creating a function for this program. So I'm just gonna say book. And I'm gonna make an open and close parentheses and an open and close curly bracket. And essentially, what we have here is a constructor. So this is a special function now, which is going to get called whenever we create a book object. So let me prove that to you guys, I'm just going to print something out. And I'll just say, creating objects. And then we also want to print a new line. So down here, I'm creating two objects, I'm creating book one, and I'm creating Book Two, whenever I create a new object, you guys are going to see that this
03:27:30
function up here is going to get called. So let me run this program. And you'll see over here, it says, creating object twice. So it says creating objects. And that was when we made this line of code. And then down here, it also says creating object. And that was when we created this second object. So we created two objects. And this method, or this function got called twice. Hopefully that makes sense. Now another cool thing about this book function is that it can accept parameters. So I can come over here and we can say that this is going to accept a parameter like name. And over here, I can just print out like, whatever the user passed it. And so now down here, when we create these objects, we can pass values in so I could pass in like Harry Potter, that's the name of this book. And we could pass in like, Lord of the Rings. That's the name of this book, right? So now when I run my program, it's going to actually be able to use those parameters. So instead
03:28:30
of printing out like, creating object, it's printing out the value that we passed into the constructor. Pretty cool, right? And constructors are awesome, because we can use them when we create objects. So let me point something out to you guys. And I'm actually just going to get rid of this stuff up here. So only point something out when we wanted to create these objects, right. And when I wanted to give them a bunch of initial information, so when I created book one, and Book Two, I had to manually specify what the title was going to be, what the author was going to be and what the pages were going to be. And it kind of took up a lot of time, right, it takes a lot of time to have to manually type out book one dot title, book, one dot author. And I have to do that for every single object that I create. Well, that gets really, really tedious. And imagine if instead of just creating two objects, I wanted to create like 100 or 1000 objects like that would take up so many lines of code, I mean, just to create one of these objects, took up four lines of code. We can actually use these constructors in order to initialize our objects with information.
03:29:37
So like when I create this book, one instead of having to manually specify the title, the author and the pages. Instead, I could just pass those values into the constructor, and the constructor could initialize the values for us. So let me show you guys how we can do that up here. In our constructor, I'm going to specify that this constructor is going to take three values. So this book constructor is going to take a string title. And actually, we're just going to call this a title, it's going to take a string, and we're going to call it a author. And it's going to take an integer, we're just going to call it a pages. And I'm putting this a here, you don't have to do that, I'm just doing it. So it's kind of easy for us to see what's going on, a is just going to stand for arguments. So this will be like the title, that's an argument. Alright, so down here, what I can do is I can actually assign the values of title, author and pages to the values that we passed in. So down here, we're assigning the values of title, author and pages two, all of this information.
03:30:38
But instead of having to do it down here, I could just do that up here in the constructor. So I could say that the title is going to be equal to a title. So in other words, the title of the object, the title that we want to store for this specific book, is going to be equal to the title that gets passed in, I can do the same thing for the author. So I could say, author is going to be equal to A author, and pages is going to be equal to A pages. And again, you don't have to name these a title, a author a pages, you can name whatever you want. I'm just kind of doing this. So it's more obvious as far as what's going on. So down here, what I can do now whenever I create this object, I need to pass it a title and author and a number of pages. So if I tried to run my program, right, now, you'll see we're getting an error, we're basically getting an error, because we didn't pass in these values. So what I can do is I can actually take the title, I can take the author, and
03:31:43
I can take the number of pages, so 500, and I can pass them into this constructor, then I can just get rid of all this code, because we don't need it anymore. And I'll do the same thing for the Lord of the Rings book. So I can, and we can pass in the number of pages. And so now we can get rid of all this stuff. So we went from having eight lines of code to create two objects. So now we just have two lines of code. And now when I run my program, we should be able to essentially just give this object a bunch of initial information. So let's go ahead and print out this stuff. So you can kind of see what happened. So I'm just going to see out while we see our book, one dot title, and let's see what we get. So we should get Harry Potter, which we do. So we did exactly what we were doing before, except we're able to do it a lot easier and a lot cleaner now, by using this constructor. So this was a very, very powerful tool for us to use. Now, I also do want to point out, we can still modify these. So I could still say like book one title. And I could change
03:32:45
the title. So I can change the title to whatever I want. And it'll be updated. really all we're doing with this constructor here is we're just making it a lot easier. So we're essentially just making it easier for us to initialize a object with different values. So that's sort of the basics of using constructors. And I'll show you guys one more thing we can do, you can actually create multiple constructors. So I can create this constructor that will essentially just allow us to take in a title and author and a number of pages. But let's say that for some books, we wouldn't want to include one of those attributes, I can make another constructor over here. And this constructor, for example, won't take in any parameters. And I can just give these values like initial value. So I can basically just say like title is equal to no title. Author is equal to no author. And we could also say like pages is equal to zero. So this is basically just giving this object like some initial
03:33:47
information if we don't pass anything it and so now, what I could do is I can come down here and I can create another book. So I can say, like book, and we'll call this book three. And I now I don't have to pass in this information. So if I want, I can just do that. And we can print out like Book Three dot title, and it's going to be that title that we gave it and this needs to be capital whoops. So now you see we get no title. So a lot of times, people will create multiple constructors, you're basically giving the user multiple ways that they can create your objects. But I'd say the most common scenario is going to be this where you pass in all of the attributes, and then they basically just get assigned. So that's the basics of constructors. Hopefully that makes sense. constructors are very useful, so you're definitely going to want to use them in your c++ classes. In this tutorial, I'm going to talk to you guys about object functions in c++. You'll also hear people
03:34:49
refer to these sometimes as like instance functions. And essentially what this is, is it's a function that we can put inside of one of our classes and then And the different objects of that class can use that function in order to either you know, find out information about themselves, or modify information about themselves. So I'm just gonna give you guys a sort of introduction into how to do this. Over here, you'll see that I actually set up a class. And this class is called student. And this is essentially just a template for like what a student is going to be in our program. And I've said down here that a student is going to have a name, a student is going to have a major, and a student is also going to have a GPA. So that information is sort of like what defines a student in our program. And then down here, I created a constructor, which basically just allows whoever's creating these objects owner to pass in some initial information. And we're passing in the name of major and then also the GPA. So I'm essentially just using this constructor as I normally
03:35:51
would any other constructor. So this is our student class. And then down here, in my main method, you'll see that I actually have a couple students. So I created a student called student one, his name is Jim, he's a business major, and his GPA is 2.4. And then I have another student student two, her name is Pam. She's an art major, and she has a GPA of 3.6. So these are now two students that I'm working with. And I'm sort of representing inside of my programs. That's awesome. So let me show you guys, how we can use these different object functions. Let's say that we wanted a way to figure out whether or not one of these students was on the honor roll. Right. So let's say that at the school that these students attend, there's an honor roll, right, and let's say that the rules are being on the honor roll as you need to have a GPA of 3.5 or above. So if you have a GPA of 3.5 or greater, that means that you're on the honor roll. And let's say that we wanted to have an easy way to figure out whether or not a specific student was on the honor roll.
03:36:54
Well, what I could do is I could actually create a function up here in my student class, which each of the objects can use to tell if they were on the honor roll or not. So what I'm going to do is I'm going to go down here below the constructor, and I can basically just create a function like I normally would, so we can give this a return type. And I'm going to give this a Boolean return type. And I'm going to name this function has honors, and this function is basically going to return a true or a false value, it will return true if the student has honors right? No return true, if the student has a GPA of 3.5 or greater. If they don't, it's going to return false. So over here, we're not going to have to take in any information, we don't need any parameters, I'm going to make an open and closed curly bracket. Now down here inside of this function, we need to figure out if the object has honors. So I can just use an if statement, I'm just gonna say if, and I'm basically just gonna say if GPA is greater than 3.5. Or actually, if GPA is greater than or equal to 3.5, then
03:37:55
we're gonna come down here, and we can just return true, because if this code gets executed, that means that they have a GPA of 3.5 or greater. And then otherwise, if that's not the case, we can just return false. So that's essentially all this little function is going to do. It's very simple if they have a GPA 3.5 or greater, turn true, otherwise, return false. Now, what's cool about this function is each of the objects that I create can call this function, and depending on their specific GPA, it's going to return a different answer. So for example, let's go ahead and call this on our first student gym. So I can just say, C out I just want to print out the answer. And I can just say, student, one dot has honors. So now if I want to figure out whether or not Jim has honors, I can just call this function, I can say student one dot has honors. And now this is going to print out whether or not Jim has honors. So you'll see over here, we're printing out a zero. And just a tidbit.
03:38:57
Whenever you print out Boolean values, if it's a false, it's going to get printed out as a zero. If it's a true, it's going to get printed out as a one. So the fact that we're seeing a zero here means that student one doesn't have honors. And we can do the same thing for Pam. So we can do the same thing with our other students. I could say student to dot has honors. And now this should be true because Pam has a GPA of 3.6. So Pam is going to have honors. What's cool about doing this is that this function is going to be using different information depending on which object is calling it. So when the gym object calls it, the GPA is Jim's GPA. When the PAM object calls it, the GPA is Pam's GPA. So we can define a general function over here in our class. And depending on the object that calls it, it's going to be giving or doing different things. So the GPA is going to be different, depending on if Jim's calling it or if Pam is calling it and that's kind of why these are cool so you can make awesome These little functions. And generally people
03:40:01
will make these to get information about the object. So for example, figure out whether or not they have honors, or modify values. And in future tutorials, we're going to look at functions which are going to be modifying the values in our classes. But hopefully that kind of gives you an idea of what you can do with these. So you know, I did a lot of cases, you're going to want to have different functions inside of your classes that are going to do stuff. Now I do want to just point out one more reason why these are cool. So let's say that I have this program written. And, you know, I'm using all these different students. And maybe I have a program that like, calculates who's on the honor roll or something. Well, let's say that one day, the Dean of the School comes to me and he's like, Hey, Mike, we're going to change the qualification for honors. So now, instead of everybody with a 3.5, and greater is going to have honors, we're just going to say everybody with a 2.0 and greater is going to have honors, in order for me to make that change in my program. In other words, in order for me to update that information, all I have to do is go over
03:41:02
here and change this to 2.0. And now all of the code in my program is still going to work. And these has honors are still going to work. But now the bar for having honors is going to be lowered. So I can say student one has honors. And now this is going to be true, because we changed the qualification. So now we get a one. And that's kind of why these are powerful, you can kind of adjust different information and you know, control different things about the overall class and about every instance of the class using these functions. So hopefully that is clear. Hopefully, you have an idea now of what you can do with these different objects functions. In this tutorial, I'm going to talk to you guys about getters and setters in c++ classes. Now, getters and setters are extremely useful. And basically, they allow you to control the access to the different attributes and different elements inside of your c++ classes. So I'm going to show you guys the basics of using c++. In
03:42:07
order to do that, we're going to use a little example. So up here, I have a class that I created, it's called movie. And this is basically just like allowing us to represent a movie inside of our program. So I have a few different attributes, the movie is going to have a title, it's going to have a director, it's also going to have a rating. So those are three different attributes of a movie, right, we can basically say like, this is our movie, this is what it's going to be, that's what it's going to have. And down here we have a constructor. So I'm just passing in the title, the director, and then also the rating. And then over here, we're assigning those values to the values inside of the object. This is all pretty standard stuff. And if you've been following along with the course up to this point, this should kind of make sense. You know what I'm doing over here, I'm essentially just defining a class, movie data type. And down here, I actually created a movie. So I created a movie, and it's called Avengers and the title is The Avengers director is Joss Whedon. And the rating is pG 13. So um, I'm creating an actual movie. And you'll see down here, I can print out the rating. So why don't we run our program, we'll just sort of get on the same level.
03:43:11
So over here, I have pG 13. So it's printing out the rating, everything works, everything looks good. Now, here's the thing, a lot of times in c++, when we're creating something like a class up here, like this movie, we're going to want to be able to control what information can be stored for a particular movie. And let me give you an example. So down here, I have this rating as pG 13. Right. And generally for movies, there's, you know, a certain number of ratings that you can have, so it'd be like g PG, PG, 13, R, and then nr, right, so not rated. So just for our purposes, let's say that these are all the ratings that you can give to move PG, PG, PG, 13, Rn, nr, right? I'm sure there's some more that we could think of. But let's just say that those are the ratings that we're going to say are valid for a movie, those are the ratings that are going to be allowed for a particular movie. Well, over here, I'm inserting pG 13. But let's say I wanted to instead enter in something
03:44:14
else. There's nothing stopping me from just entering in nonsense like dog, but there's nothing that's stopping me from just typing in some nonsense rating there. And then running the program and being able to store it inside of my movie, right I In other words, I can set the rating equal to dog, even though that's not technically like one of the official ratings that we can have. And there's a lot of circumstances where there's going to be things like this, for example, you know, valid ratings that you're going to want to enforce. In other words, like when you're writing this program, you might not want an object, you might not want a movie object to be created. That's not using a valid rating. Like you wouldn't want this to be able to happen down here. You wouldn't want them to be able to put a rating in as dogs. So I'm gonna show you guys how we can And essentially just enforce that how we could make it so that the user can only create an object a movie object with a valid rating. And to do that, we can use something called getters and setters. Now, the first thing that I'm going to do, after I turn that back, is I'm going to head up
03:45:16
here into my class. So up here in my movie class, and I want to show you guys, one thing you'll see up here, I have this keyword public. And essentially, when I say public, and I put all this stuff underneath public, what that means is that all of this stuff is public. And basically, when something's public, it means that any other program any other code can access it. So essentially, any code outside of this class is able to access the title of the movie, the director of the rating and the constructor. So down here, I'm able to print out Avengers dot rating, because rating is public, right? So I can print this out, because it's underneath this little public keyword. But there's another keyword that we can use in c++, which is called private. And I can do the same thing as I did with public, I can just say private, and any attributes, any variables, any functions, anything that I put underneath this private keyword, is actually going to be private. So for example,
03:46:16
if I was to take this string rating, and I was to put this under here, under this private, what this means is now only code inside of this movie class is able to access the rating attribute, only code that's inside the movie can access the rating. So if I was to come down here now and try to print out Avengers dot rating, I'm not going to be able to do that anymore. And you're going to see that we're going to get an error. So you can see this highlights in red basically telling us that you can't print out Avengers dot rating, because it's private, so I no longer have access to the rating inside of my main function. Now one thing I will point out is I have access to the rating here in the constructor. That's because the rating variable, the rating attribute is in the same class as this constructor, so it's able to access it. But this main function isn't able to access it in any code inside the main function can't, because it's not inside of that class. So that's basically
03:47:19
the difference between public and private. And we can leverage public and private in order to control what ratings are able to get set for this movie. So let me show you guys how we can do this, the first thing I want to do is, underneath this public block, I'm going to create a public function, and I'm going to call it set rating. So actually, this is going to be void. And I'm just going to call it set rating. And this is going to take as a parameter, it's going to take one value, it's going to be a string, a rating. So this is going to take a rating as a parameter down here, inside of this set rating function, I'm basically going to say rating is equal to A rating. So now whenever I want to give a value to the rating, I'm going to make it so we have to go through this set rating function. So over here, instead of saying rating is equal to A rating, on set, I'm just going to say, set rating, and I'm going to pass in a rating. And so now whenever we set the
03:48:21
rating, it's going to go through this set rating function. And for example, if I wanted to modify the rating down here, so if I wanted to say like Avengers dot rating is equal to dog, I'm not going to be able to do this again, because rating is private, so I can't access it over here. But if I want to modify the rating, I can just go through this set rating function, because it's public. So over here, if I wanted to modify the rating, instead of saying Avengers dot rating, I could say Avengers dot set rating, and I can pass into this as a parameter, dog. Alright, so now anytime we're setting the rating, either up here in the constructor, or down here in the actual program, we have to go through this set rating function. And that's going to be really useful. So essentially, what we can do now is we can set up some rules. So I can set up some rules inside of this set rating function for what ratings are going to be valid. So I'm going to go ahead and do that.
03:49:22
Essentially, what I'll do is I'll create an if statement. And if the rating that gets passed in is one of the valid ratings like g PG, PG, 13, r nr, then we'll let them set it. Otherwise, we'll be able to essentially like throw an error or we'll be able to say like, Hey, this is an invalid rating. So down here, I'm just going to create an if statement. And I'm just gonna say if. And I'm basically just going to check to see if the rating is one of the valid rating so we can check to see if a rating is equal to G or a rating is equal to P And I can keep doing this for each of the valid ratings. So I'll keep doing this over here. And I'm just gonna say, or a rating is equal to pG 13. Or a rating is equal to R. And then there's one more, which is going to be or a rating is equal to nr.
03:50:26
So basically, what I'm doing is I'm creating this long winded if statement. And it's checking all of these conditions. So it's checking to see if a rating is G, or if it's equal to PG, or if it's equal to pG 13, or R or Finally, nr. Now, if it's equal to one of those, right, if it's equal to one of those valid ratings, then we can just go ahead and set it normally. So I could say like rating is equal to A rating, right? Because they entered in a valid rating, so it's going to be fine. But here's the thing, if they didn't enter in a valid rating, that means that we're not going to be able to set it as the rating that they entered, so I can say else. And otherwise, why don't we just go ahead and set rating equal to an R. So we'll say that if they entered in an invalid rating, like they tried to set an invalid rating, for the rating for the movie, we're just going to go ahead and set it to nr because they didn't enter in a valid rating. So it's just going to be not rated. So here's the thing. Now whenever we want to set the rating, we have
03:51:28
to go through this set rating function. So for example, I can come over here and I can say Avengers, set rating and dog. And then if I printed out the rating, it's actually gonna say nr, because they entered in an invalid rating, it's just going to default to nr. But here's the other problem is when I can't actually print out the rating. So I can't Access Avengers dot rating, because it's private, remember, so what we can do is we can create another function up here, and this one is going to be a string, so it's going to return a string. And I'm just going to call it get rating. And it's not going to take any parameters, it's just going to return rating, so it'll just return the rating. And so now if I wanted to print this out, I could say Avengers dot get rating, and I'll be able to print it out. So let's go ahead and see what happens. So down here, I set Avengers rating equal to dog. That's an invalid rating. So now when I get the rating, and I print it out, we should just print out nr. And actually looks like I have a typo here. So I forgot to put a double
03:52:31
equals in there. Let me see if I did that over here, too. Nope. Okay, so we should be able to run this now. Yeah, so down here, you'll see that we're printing out an R. So I tried to set the rating u equal to dog, that was an invalid rating. So when I went to get it, it's just giving me an R. And that's going to work for anything. So for example, over here, when I just first created this, I set pG 13. That's a valid rating, right, that is one of the official ratings that we can use. So when I run this, it's going to have that it'll have a rating of pG 13. But if I tried to set this to like pG 15, or something, that's not a valid rating, right. So when I run the program, it's going to basically say that it's not rated because we didn't enter in a valid rating. And you know, you can essentially do whatever you wanted there. The point that I'm trying to make though, is that there's going to be certain times where you want to control what values are, you know how the user can interact with the attributes of a class or of an object. In
03:53:31
our case, we wanted to be able to restrict what types of ratings were able to be stored inside of a movie. So I was able to set the attribute equal to private, which meant nobody could access it directly. They couldn't just say like, hey, the rating is equal to this, or the ratings equal to that. Instead, if they wanted to set a rating, they had to go through the set rating function. And they had to go through our little if statement here. And that is an awesome way to control access to the individual elements, or the individual attributes in a specific object. In this tutorial, I'm going to talk to you guys about inheritance in c++. inheritance is basically where we can define a class. And then we can define other classes. And those classes can extend the functionality or they can inherit all the functionality, all the attributes of that original class. So the easiest way for me to explain this is just to kind of show you guys an example. So over here in my program, I created a class and it's called the chef class. Basically,
03:54:35
this class is like modeling like a chef in our program. And there's a couple different functions that the chef class can perform. The chef can make chicken, the chef can make salad and the chef can make a special dish. Basically each of these functions just prints out like what it's doing. So this one says the chef makes chicken the chef makes salad. Chef makes barbecue ribs. Very simple class. And down here you'll see I'm actually creating an instance of this Chef class, and called chef and down here I'm telling the chef to make chicken. So let's run the program. And we'll see what happens. So when I run the program, you'll see, it prints out the chef makes chicken. Awesome. So we have this chef class. And let's say that I'm running my program. And I'm thinking to myself, like, Hmm, I think in addition to representing just a normal generic chef, I also want to represent an Italian chef. So what I'm going to do is I'm going to go over and I'll actually just create another class. So I'm just going to say class. And instead of creating a chef class, I'm going to create an Italian chef class. So I'll say, Italian chef. And let's say for
03:55:40
our purposes, when we're creating this Italian chef class, we want the Italian chef to be able to do everything that the normal chef can do. So this Italian chef can make chicken can also make salad and can also make a special dish. But in addition to that, the Italian chef will be able to do a bunch of other stuff, well, we can actually use inheritance in order to build this Italian chef. Because the Italian chef is going to be able to do everything that the normal chef could do, like make chicken, make salad and make a special dish, I can actually inherit all of these functions. From the chef class in my new Italian chef class, I'll show you guys how to do that, it's actually super easy. All we have to do is come down here. And I'm just going to make a colon. And then I can just say public and chef. So this is referring to the class that I want to inherit from. And now what's cool about this is this Italian chef is going to have access to all of the functionality up here
03:56:42
in this chef class. So it's basically going to inherit all of those functions. So I'll show you if I was to come down here and create an Italian chef. So if I just said, Italian chef, we'll just call it Italian chef. I could then say Italian chef dot make chicken. And even though I didn't specify anything up here, even though this class is technically empty, even though I didn't specify a make chicken function, or make salad or make special dish function up here, I'm still able to call make chicken on Italian chef. So if I was to run this program, now, you'll see it says the chef makes chicken. So I'm able to call that function on the normal chef and the Italian chef, essentially what's happening is the Italian chef down here is inheriting all of this functionality. So actually, just to sort of demonstrate this even further, if I came down here and I change this to chef makes
03:57:44
yummy chicken. Now when I run my program, again, you'll see that the Italian chef is also going to be able to make yummy chicken. So it's basically inheriting this function from the chef class. And what's cool about inheritance is that not only can we inherit all that in functionality, but we could extend on it. So the Italian chef isn't just going to be able to do everything the chef does. Italian chef can also do other stuff. So the Italian chef could also like make pasta. And down here, we can just basically print out like the chef makes pasta. So in addition to being able to make everything that the normal chef can make, this Italian chef can also make pasta. So I can come down here and say make pasta. But it's important to note that this normal chef can't make pasta. So if I tried to call pop make pasta on the normal chef, you'll see we're going to get an error. So you'll see over here, it's not going to be able to run the program. And actually, it looks like I had an error up here. So this should be a lesson sign. Alright, so now we'll
03:58:48
get an error down by the other chef. Yeah, so now we're getting this error, which is basically saying this, this chef can't make pasta, but the chef could make chicken. And now that you'll see the Italian chef is gonna make pasta and the normal chefs gonna make chicken. And actually one more thing I forgot, we need to put public over here. I keep forgetting to do that. So I'm gonna say public. And then this is basically going to specify that this function is going to be public. So now we should be able to run our program. And you'll see it says the chef makes yummy chicken and the chef makes pasta. So both of our chefs are able to do different things. And that's pretty cool. And the other cool thing I can do with inheritance is I can actually override certain functions. So you'll notice up here, the normal chef has this make special dish function. And this chef is making barbecue ribs as a special dish. But let's say that the Italian chef was going to have a different special dish. Well, I can do something called overriding a function. So I can actually copy
03:59:48
this function from up here. And I can paste it down here into my Italian chef. And essentially now this is going to override the function from up here. So instead I'm making barbecue ribs we could have him make like chicken parm. So now this Italian chef is going to be making chicken parm while the normal chef will make barbecue ribs. So if I came down here and I said make special dish on the normal chef and I said, Make spicy special dish on the Italian chef. Now these are going to be two different things because the Italian chef overrode that original function. So chef makes chicken parm and the chef makes barbecue ribs. So that's kind of how overriding can be useful. Like if you're inheriting a function and you want to change, like what it's doing, or modify what it's doing in the other class, you can do that. Now, just a quick terminology. So we would call this chef class, a superclass.
04:00:48
And then we would call this Italian chef class a subclass, because the Italian chef is inheriting from the chef class, which is the superclass and that is essentially, the basics of inheritance is you're essentially creating a class and you're inheriting all the functionality from another class inside of it. And then you can do things like add extra functions, you can add on to it, or you can override the functions that you inherit. Hey, thanks for watching. If you enjoyed the video, please leave a like and subscribe.

DOWNLOAD SUBTITLES: