C++ For Beginners | Primorous Arts | Skillshare
Search

Playback Speed


1.0x


  • 0.5x
  • 0.75x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 1.75x
  • 2x

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

    • 1.

      Introduction

      2:45

    • 2.

      Chapter 1 (Setup): Install Visual Studio

      4:48

    • 3.

      Chapter 1 (Setup): Get Everyone On The Same Page

      3:19

    • 4.

      Chapter 2 (Hello World): Explaining The Code

      12:26

    • 5.

      Chapter 2 (Hello World): White Space

      2:29

    • 6.

      Chapter 2 (Hello World): Writing To Console

      8:12

    • 7.

      Chapter 2 (Hello World): Comments

      3:45

    • 8.

      Chapter 3 (Variables): Intro To Variables

      6:24

    • 9.

      Chapter 3 (Variables): Integers

      7:42

    • 10.

      Chapter 3 (Variables): Floating Points

      6:40

    • 11.

      Chapter 3 (Variables): Booleans

      6:24

    • 12.

      Chapter 3 (Variables): Chars

      4:14

    • 13.

      Chapter 3 (Variables): Strings

      8:39

    • 14.

      Chapter 3 (Variables): Constants

      4:55

    • 15.

      Chapter 4 (User Input): Cin User Input

      9:32

    • 16.

      Chapter 4 (User Input): Getchar & Getline Functions

      6:25

    • 17.

      Chapter 4 (User Input): String To Int

      6:09

    • 18.

      Chapter 5 (Operators): Basic Arithmetic

      5:48

    • 19.

      Chapter 5 (Operators): Increment & Decrement

      7:28

    • 20.

      Chapter 5 (Operators): Modulus

      1:55

    • 21.

      Chapter 5 (Operators): Assignment

      2:18

    • 22.

      Chapter 5 (Operators): Concatenation

      4:15

    • 23.

      Chapter 5 (Operators): Casting & Truncating

      9:02

    • 24.

      Chapter 6 (Conditionals): Scope

      4:01

    • 25.

      Chapter 6 (Conditionals): Intro To Conditionals

      1:27

    • 26.

      Chapter 6 (Conditionals): Relational & Equality Operators

      3:02

    • 27.

      Chapter 6 (Conditionals): Logical Operators

      2:16

    • 28.

      Chapter 6 (Conditionals): If Statements

      6:36

    • 29.

      Chapter 6 (Conditionals): Nesting If Statements

      6:17

    • 30.

      Chapter 6 (Conditionals): If-Else Statements

      3:07

    • 31.

      Chapter 6 (Conditionals): Else-If Statements

      7:10

    • 32.

      Chapter 6 (Conditionals): Switch Statements

      10:44

    • 33.

      Chapter 6 (Conditionals): Ternary Operator

      9:42

    • 34.

      Chapter 7 (Loops): Intro To Loops

      1:05

    • 35.

      Chapter 7 (Loops): While Loops

      7:29

    • 36.

      Chapter 7 (Loops): Do While Loops

      3:13

    • 37.

      Chapter 7 (Loops): For Loops

      7:53

    • 38.

      Chapter 7 (Loops): Nesting Loops

      3:39

    • 39.

      Chapter 7 (Loops): Break Statements

      8:49

    • 40.

      Chapter 7 (Loops): Continue Statements

      5:40

    • 41.

      Chapter 7 (Loops): Validating User Input

      14:26

    • 42.

      Chapter 7 (Loops): Scope Resolution Operator

      2:40

    • 43.

      Chapter 8 (Random Numbers): Intro To Random Numbers

      3:53

    • 44.

      Chapter 8 (Random Numbers): Generating Random Numbers

      8:19

    • 45.

      Chapter 9 (Containers): Intro To Containers

      1:27

    • 46.

      Chapter 9 (Containers): Arrays

      7:52

    • 47.

      Chapter 9 (Containers): Multi-Dimensional Arrays

      10:07

    • 48.

      Chapter 9 (Containers): Vectors

      11:25

    • 49.

      Chapter 9 (Containers): Queues

      7:13

    • 50.

      Chapter 9 (Containers): Stacks

      4:37

    • 51.

      Chapter 10 (Foreach Loops): Intro To Foreach Loops

      1:44

    • 52.

      Chapter 10 (Foreach Loops): Foreach Loops

      14:08

    • 53.

      Chapter 10 (Foreach Loops): Auto Keyword

      8:07

    • 54.

      Chapter 10 (Foreach Loops): Maps

      12:12

    • 55.

      Chapter 10 (Foreach Loops): Iterators

      11:07

    • 56.

      Chapter 11 (Enums): Intro To Enums

      3:12

    • 57.

      Chapter 11 (Enums): Creating An Enum

      5:11

    • 58.

      Chapter 11 (Enums): Using Enums

      10:59

    • 59.

      Chapter 11 (Enums): Eums vs Enum Classes

      4:04

    • 60.

      Chapter 12 (Functions): Intro To Functions

      2:39

    • 61.

      Chapter 12 (Functions): Creating A Function

      6:46

    • 62.

      Chapter 12 (Functions): Function Prototypes

      2:58

    • 63.

      Chapter 12 (Functions): Return Types

      7:49

    • 64.

      Chapter 12 (Functions): Parameters & Arguments

      6:45

    • 65.

      Chapter 12 (Functions): Optional Arguments

      5:58

    • 66.

      Chapter 12 (Functions): Overloaded Functions

      4:38

    • 67.

      Chapter 12 (Functions): Recursion

      7:32

    • 68.

      Chapter 12 (Functions): Arrays As Parameters

      8:17

    • 69.

      Chapter 13 (References): Intro To References

      1:28

    • 70.

      Chapter 13 (References): Creating A Reference

      3:32

    • 71.

      Chapter 13 (References): References & Foreach Loops

      4:01

    • 72.

      Chapter 13 (References): References As Parameters

      4:17

    • 73.

      Chapter 13 (References): References As Return Types

      7:31

    • 74.

      Chapter 13 (References): Memory Addresses

      4:20

    • 75.

      Chapter 14 (Pointers): Intro To Pointers

      1:14

    • 76.

      Chapter 14 (Pointers): Creating A Pointer

      9:45

    • 77.

      Chapter 14 (Pointers): NULL Pointers

      3:52

    • 78.

      Chapter 14 (Pointers): Pointers & Functions

      6:20

    • 79.

      Chapter 14 (Pointers): Pointers & Arrays

      10:44

    • 80.

      Chapter 14 (Pointers): Pointers vs References

      2:56

    • 81.

      Chapter 15 (Classes): Intro To OOP

      4:03

    • 82.

      Chapter 15 (Classes): Creating A Class

      5:59

    • 83.

      Chapter 15 (Classes): Class Objects

      1:59

    • 84.

      Chapter 15 (Classes): Access Specifiers

      4:32

    • 85.

      Chapter 15 (Classes): Class Functions

      6:12

    • 86.

      Chapter 15 (Classes): Class Constructors

      10:32

    • 87.

      Chapter 15 (Classes): List Initializers

      5:50

    • 88.

      Chapter 15 (Classes): Encapsulation, Abstraction, & Properties

      16:11

    • 89.

      Chapter 15 (Classes): Inheritance

      29:24

    • 90.

      Chapter 15 (Classes): Classes Recap

      27:44

    • 91.

      Chapter 15 (Classes): Static Keyword

      18:07

    • 92.

      Chapter 15 (Classes): Class vs Struct

      3:06

    • 93.

      Chapter 15 (Classes): Prevent Base Class Creation

      2:01

    • 94.

      Chapter 15 (Classes): Member Access Operators

      2:57

    • 95.

      Chapter 15 (Classes): Polymorphism

      19:00

    • 96.

      Chapter 15 (Classes): Four Main Principles Of OOP

      5:01

    • 97.

      Chapter 15 (Classes): Friend Functions & Classes

      8:06

    • 98.

      Chapter 15 (Classes): Namespaces

      8:00

    • 99.

      Chapter 15 (Classes): Arrays Of Objects

      6:56

    • 100.

      Chapter 15 (Classes): Alt Class Creation

      11:50

    • 101.

      Congratulations!

      2:06

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

118

Students

--

Project

About This Class

Description

In this comprehensive C++ For Beginners course, I'll be taking you from having no prior programming knowledge, to confidently writing C++ code of your very own, as we master the basics and even tackle advanced topics like inheritance, polymorphism, and interfaces. With step-by-step guidance, practical challenges, and clarification on not just what to do, but why, you’ll be on your way to building real-world projects in no time!

Why You Should Take This Class:

  • Versatility: C++ is used in game development (Unreal), mobile apps, software engineering, and much more.
  • High demand for C++ developers: With an average salary of $100,000+ for skilled programmers, C++ is a great starting point for a lucrative tech career.
  • Learn by doing: You'll get hands-on experience with coding challenges from Chapter 3 onward to help you apply what you've learned and solidify your understanding of the concepts taught throughout the course.

Who This Class Is For

  • Aspiring Programmers with no prior programming knowledge.
  • Anyone who wants to take their C++ skills to the next level.
  • Programmers who need a refresher or simply want to learn a new language.

Materials/Resources Required

  • A computer (Windows, macOS, or Linux) capable of running Visual Studio or any C++ IDE of your choice.
  • No prior programming experience is required.
  • All software used in the course is free to use.
  • I'd also highly recommend having a physical keyboard.

Meet Your Teacher

Teacher Profile Image

Primorous Arts

Course Instructor

Teacher
Level: All Levels

Class Ratings

Expectations Met?
    Exceeded!
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. Introduction: Welcome to the Primoris Arts C plus plus course for beginners. If you are here, you're probably interested in learning to code, but feel a bit lost on where to start. I get it. I was there, too. When I first tried learning to code, I spent more time searching for a complete course, follow content to what I had already learned and or answers to basic questions than actually learning. Plus, I felt like I was just being taught how to go through the motions when what I really needed and wanted was to know how things worked and when to use them so that I could get started on my own projects. That right there is why we created this course so that you wouldn't have to deal with those same struggles on your journey to becoming a programmer. Everything you need to get started is right here in one place from beginner to advanced topics with real support available when you need it most. So why should you learn C plus plus specifically? C plus plus is a powerful versatile language used in game development, web browsers, databases, and more. With it, you'll have the foundation to start creating your own projects in a scale that is highly valued in the ever growing tech industry. And while C plus plus is considered to be a more difficult language to learn based on C language, it can be an essential tool for those looking to master programming in fields where memory and speed are of major concern, and it provides a strong foundation that makes learning other C based languages like C Sharp or Java that much easier. With that in mind, this course is going to start with the absolute basics. If you've never written a single line of code, don't worry, you're going to be just fine. We'll dive into the fundamentals like variables and gradually work our way up to more advanced topics like classes or objects, abstraction, encapsulation, polymorphism, inheritance, and more. From Chapter three onward, you'll be given the opportunity to get hands on with coding challenges specifically designed to help you solidify your understanding of the concepts taught throughout the course. So why should you choose this course to learn C plus plus? Because rather than just showing you how to go through the motions and having you end up with some project that you have no idea how to recreate on your own, I'll be teaching you not only how to write the code, but more importantly, when to use it and why. By teaching you to think like a programmer, you'll actually gain a much deeper understanding of each concept taught so that by the end of the course, you'll be ready to tackle your own projects with confidence. And to make things even better, everything we use in this course is free, meaning there are no free trials or hidden fees to worry about. Now, who is this course for? This course is perfect for absolute beginners looking to get started, experienced programmers looking to refresh or expand upon their skills and everyone in between. And remember, there's no rush. Feel free to go at your own pace and to come back and revisit specific lessons when needed. Here at Primoris Arts, we are committed to helping you succeed every step of the way. So if you are ready to begin your coding journey, then there's no better time than right now. We invite you to join us today in the next video as we dive into programming step by step with clear, concise and easy to follow lessons. Thanks for watching, and I'll see you in the next one. 2. Chapter 1 (Setup): Install Visual Studio: Welcome to the very first episode of the C plus plus for beginners course. This episode, I'll be showing you how to install Visual Studio, a completely free program that we'll be using to write and run all of our code. Now, unfortunately, if you're on a Mac OS or Linux, you will need to download another program that will let you write and run C plus plus code. But don't worry, once you do that, you'll be able to follow along just fine. And there are multiple free programs available, as well as plenty of tutorials online on how to get started with them. Then the next episode, I'll be making sure that everyone is on the exact same page so that you can follow along regardless of what program you're using. Real quick here and just a few examples of some programs I'd recommend. Whether you're on MacOS or Linux. You can use Visual Studio Code. Another one you can use is code blocks. And lastly, for macOS users specifically, you can use the built-in Xcode software. Anyways, for all of you that are on Windows, Visual Studio is the most popular program and the one I'd recommend using as it is very easy to setup and get started. So the first thing you are going to want to do is go to Visual Studio.Microsoft.com. Then at the top, go to downloads. Then you will see your Visual Studio with C plus plus. Go ahead and go to the download drop-down box and go to where it says Community Free. Make sure you do not click on the one that says free trial. Then once it is downloaded, go ahead and open up the Visual Studio Setup application and make sure to accept any prompts that may pop up on your screen. Then go ahead and wait for it to finish downloading and installing. Now, once it has finished installing, you can go ahead and create a free account if you like. As I believe it will make you create an account at some point, even though the program is completely free. However, at this point in time, I'm just going to go ahead and continue without an account. So that that way none of my previous account settings are on there and that we can go over everything together with a fresh and skull. So I'm gonna go ahead and click not now, maybe later. Now, I would highly recommend going with a dark theme. Not just a Visual Studio, but any program you use to write code in the future, as it will definitely be a lot better on your eyes. Then as for the development settings, I'm just gonna go ahead and leave them on general. Then we'll go ahead and hit Start Visual Studio. Alright, once it is open, we're going to go ahead and create a new project. And what you're going to search for a here at the top is C plus, plus. Another thing you can do is just go to where it says all languages and click on C plus plus. And you should see one for console application. So I'm going to click on that and then hit Next. Then you are going to go ahead and give your project a name. Now, you can give any name you want, but I'd recommend sticking with a Pascal Case naming convention. Meaning you aren't going to have any spaces. You're going to start with a capital letter and every subsequent word will also start with a capital letter. So go ahead and give it a name quick. Now, you'll notice that your solution name here at the bottom is the exact same as your project name. For this course. We're going to go ahead and leave it that way. But just know that in the future, your solution and project name do not have to be the same. Also, you can go ahead and choose any location you want for this project. Then make sure this box down here at the bottom that says play solution in project in the same directory. As unchecked. As this is generally good practice. Then go ahead and hit Create. Alright, then you can go ahead and close this out. And there you have it. We've now installed Visual Studio, and we've created our very first C Plus Plus project. Now what I want you to do is go to file at the top and hit Save All, and make sure to do that at the end of each and every video unless stated otherwise. Now, I know that not all of you are using Visual Studio. So in the next video, I'm gonna be making sure that all of you are on the exact same page as I am and are ready to learn how to read and write code together. And don't worry, those are the only program I'll be having you installed throughout this course. Anyways, thanks for watching, and I'll see you in the next one. 3. Chapter 1 (Setup): Get Everyone On The Same Page: In this video, I'm going to be making sure that all of you are on the exact same page as I am, so that you can easily follow along with the rest of the course. And remember, if you need any help at all. Please let me know down in the discussion section below and I'll be more than happy to help you out. Anyways, let's get started. Now real quick, for those of you who are using Visual Studio, we're gonna go to tools at the top and go down to Options. And under the bugging, click General. Then scroll down to the bottom. And you should see a checkbox that says automatically close the console when debugging stamps. Make sure this is checked. Then go ahead and click Okay. Then regardless of what program you are using, if you have any other texts outside of this hashtag, include iostream line in this closed curly bracket. So everything right here, just go ahead and get rid of everything else. Like so. Okay, so this should be the only thing left on your screen. Now, if this isn't on your screen, go ahead and copy this exactly. Now, once your code looks like mine, we're actually going to go ahead and add a few extra lines of code. Now, don't worry, you don't have to understand any of this. We're gonna go over it altogether in the next video. But for right now, I just want to make sure that everyone's code is working. So right after this HelloWorld line here, right after the semicolon, we're gonna go ahead and click Enter to create a new line. And we're going to type std, colon, colon, C dot, get open parentheses, close parentheses, semicolon. Then we'll hit Enter again. They create a new line. I'm going to type return. Then the number 0 followed by another semicolon. So everyone's code should look exactly like this. Now, regardless of what program you're using, you should be able to run the code as it is. Now for those of you using Visual Studio, you can just click this Play button here at the top, or the hockey is F5. Now for those of you using another program, you'll have to figure out how to run your code. And if there's a hotkey for doing so, I definitely would recommend remembering it as we'll be doing it a lot throughout this course. Anyways, once your code runs, I should simply say hello world. And when you press Enter, I should exit out of the code. Just like that. Now, all of you might not get a console window that pops up when your code runs. And that's perfectly fine. As long as your code runs and it says hello world, you're all good to go. As for the rest of you, the first thing I want to check, your code looks exactly like mine. Then if you're still having issues, please let me know down in the discussion section down below, and I'll be more than happy to help you out. Anyways. That is it for this video. The next video, we're going to be going over what all this code actually means. So thanks for watching, and I'll see you in the next one. 4. Chapter 2 (Hello World): Explaining The Code: In this video, we are going to be briefly going over all of the code we have here. Now, don't worry, I don't expect you to remember everything as we'll be going over all of it in more detail later on in the course. I just want to give you a brief introduction as to what is going on. And at the end of the video, I'll give you a quick summary of the few key points I want you to remember as we go forward. So at the beginning here, we have this include statement, which basically tells the compiler that we want to take the code from the header file named iostream and put it in our program. The compiler as what translates everything here into binary or ones and zeros so that our computer can actually understand it. And a header file is basically just a collection of code that is made to be used by other programs. Next, we have our main function. The main function is important because every C plus plus program has a main function, as this is where your program will start and end. And everything that is a part of the main function needs to be placed between these open and close curly brackets. And this is where a majority of our code will be written. Next, we have this line of code that prints out Hello world. And let's go ahead and break that down quick. So first we wrote std, which is a namespace that contains a definition for the object C out. And we'll get back to namespaces here in a minute. Now, see out as an object used to display text. And it is always followed by the insertion operator, which is two less than symbols. And what is to the right of the insertion operator is what you actually want to print out. When you want to print texts literally, or you want to print out exactly what you type. You need to have it between two double quotation marks like so. And this whole line here as a statement or an instruction for our program. And most statements we'll end with a semicolon. So to summarize, we're basically saying we want to use the object C out to print out hello world. Then this line that we added in the last video basically just says that we want to get input from the user. And the only reason we put this here as so that our program will not immediately shut down after running. As usually, once a program has ran through all of its lines of code, it will assume it's done and automatically close out. Now, depending on what program you're actually using to write code, your code may stay open with or without this line. But for those of you who need it, I'm going to leave it here onscreen as a reminder. So just to show you quick, if I got rid of this line of code and try to run my program, it would run instantly and then immediately shut down. So if I put that back and run my code again, I now need to hit Enter to close out. Now, it will actually let me type multiple letters and numbers. But once I hit Enter, it will immediately shut down. And that is because this get method here is essentially waiting. They take user input and as assuming you are done when you hit the Enter key. And lastly, we have this return statement here at the bottom, which just says that we want to return the whole number zero. And the reason we are returning zero as because our main function has a return type of an integer. Once the code that calls upon our main function receives this zero. And those are code ran successfully. And something you may see here, interchangeably with the zero is exit success with an underscore in between and in all caps. Like so. This will run just fine. As in C plus, plus. This is the exact same as just putting in the actual numbers zero. And I'm actually going to leave it like this for clarity. For those of you that do need it. However, that being said, C Plus Plus actually allows you to just remove this line and it will return zero automatically. But like I said, I'm going to leave this here for clarity sake, as this is what is actually happening in the background. So real quick, let's go back to the std namespace. The namespace is basically just that. It is a named space in which code resides. And you can kind of think of this as a person's lastName, e.g. if I just mentioned someone's first name to you, you might not know who I was talking about, but if I told you their last name, then when I said their first name, you don't have context as to who I was referring to. Exactly. That's the same with our code here. So when we say that sin as part of the std, namespace and nose were sent as coming from just in case there are other objects that also have that same name. And this std namespace comes from this iostream header file, which is used for standard input and output, which is why you will see this header file used and many C plus plus programs. So something we can do, instead of putting this namespace before everything that is part of that namespace has at the top here, right below our include statement. We can say using namespace. Std and then a semicolon at the end. And now it will automatically know where these objects are coming from. And we'll get into objects more later on in the course. As C plus plus is an object-oriented programming language. So now that we are saying that we are using this namespace and it knows where these objects are coming from, we can actually just remove this prefix here entirely. Just like that. And if I run the program, you can see that it runs just fine. However, that being said, some people like to leave those there for clarity sake. But me personally, I'd rather just type C out or CN, seeing as they're used so commonly. So I'll go ahead and remove it like that and save the program. And this is what we'll be using going forward. So make sure that you guys had this line of code and you'll be all caught up in. Lastly, let's go back to this Hello World text here. You may be wondering what this backslash n is and why it didn't appear when we ran our code. So as you can see, when I run a code, it says Hello world, exclamation point. Then our cursor is blinking down here at the bottom. And that's because this backslash n says that we want to go down to a new line. So let's go ahead and remove that quick and run our code again. And as you can see, our cursor is now at the end of the line. So that backslash n basically just says that we want to go down to a new line. Now, what if you wanted to write more text on the new line? Some of you may instinctively go like this. Makerspace, hello began and run that code. But you'll notice that that space is actually registered as a space on the new line. So if we don't want that there will have to remove the space from here. Because like I said before, everything between these double quotes will be printed out literally or exactly. However, this backslash is actually a special escape character that will read the very next character after it and do something specific with it. And there was a whole list of special escape sequences you can use. And let's just go over a couple as an example. The first one is the backslash n, which you've already seen for creating a new line. You can also do backslash, backslash for actually printing out a backslash. Like so. Another one would be backslash quotation mark. For actually printing out a quotation mark. Rather than signifying the end of our time. I run a code. You can see the double quotation mark right there. Anyways, let's go ahead and remove that and just put the backslash n background was like so in real quick, something you may see instead of backslash n is the following. So after a double quote, you may see another insertion operator followed by the word end with a lowercase l, like that. And then makes sure the semicolon is still at the end. If we run our code. This will also go down to a new line. And L is simply short for N line. And you can use these interchangeably as they do the exact same thing, just to remember that the backslash and needs to go inside of your quotes. And the end L needs to be on its own after another insertion operator, like so. Anyways, I'm gonna go ahead and put that back to the way it was. Now real quick. Something I'd like to mention is that although I called this the main function, you may hear people say main method as a method of function are often used interchangeably. And you may be used to people saying method, especially if you come from another programming language. Anyways, let's go ahead and recap real quick while we have one screen. So first, we have this include statement, which basically says that we want to use code from the iostream header file. Then we're saying that we're gonna be using the namespace std, which tells our program that this c out and sent object come from the namespace std. And like I said, you can basically think of a namespace as somebody's last name. And basically just a way to specify where something is coming from or what we're referring to. When we said scalp, they know that we want to use c out from the std namespace. Alright, so we're including code from iostream. These two objects here being C out and C n, come from the name space std. Then we have our main function, or a method which is a part of array C plus plus program, as this is where our program will begin and end. And inside of here, we have just a couple of statements. We have this first one, which is the CL object to print Hello World. We have this sin.gov, which is basically waiting to get user input. And then we simply have there so that our program doesn't close immediately until we press the Enter key. Then we have return exit success, which is the same as saying return zero as our main function needs to return an integer value or a whole number. And it will normally return zero when it has succeeded and its operation. So this and this are the exact same thing. And like I said, C plus plus will actually allow you to just remove that and typing. Alright, anyways, that is it for this video. Make sure you guys see what you have. And don't worry if you don't remember everything we talked about here. The only thing I really want you to remember going forward as the R code is going to take place within the main function. And then every C plus plus program has a main function because this is where our program begins and ends. And going on from this point forward, we're going to be breaking down every little thing step-by-step into its own individual video so that you can follow along easily, really comprehend what's going on so that you can use it in your own programs. Anyways, thanks for watching, and I'll see you in the next one. 5. Chapter 2 (Hello World): White Space: In this video, we are going to be talking about whitespace. Whitespace and C plus plus base over your first two characters that are used for formatting purposes, like spaces, tabs in sometimes new lines. And the C plus plus compiler or the thing that is taking all of this code and converting it into binary so that the computer can understand it will generally ignored whitespace. However, there is a few exceptions which we'll go through in this video. So to give you an example of whitespace, you can use as much new lines, as many times, as many spaces as you would like, generally. Even in-between things. So your code will run just fine. Okay? So basically, you want to use whitespace to make the code as readable as possible to you. And if you'll be working with others, you'll want to make it as readable as possible to them as well. So when is whitespace not ignored? Well, when we use quotation marks to tell our program that we want it to print out what is between them exactly. It assumes that when we put in a space that we want to space. So when we put a space here between hello and world, but showed up and our program. As with any other spaces that we played with in here. As you can see here. The other time is when we have an operator that uses two characters, like with our insertion operator here. If you were to put spaces in-between, that it would no longer understand what this was. And then there's really only one other time where the compiler will not ignore whitespace. And that is when you have a keyword like return here, you cannot separate it like so. And there you have it. You now know whitespace as in C plus plus programming. Basically, you can use as many new lines, as many spaces and as many tabs as you would like to make your code as readable as possible. With the exceptions being, not being able to break up keywords. Any spaces where you tell your program that you want it to print out exactly what you tell it to. And you cannot break up operators that have two or more characters. Anyways. Thanks for watching, and I'll see you in the next one. 6. Chapter 2 (Hello World): Writing To Console: In this video, we're just going to go back and go over how to print texts to the console. So the main way you're going to protect to the console and C plus, plus, as with a C object, followed by the insertion operator. Just remember that if for whatever reason your code will not accept this as because you need to include the iostream. And you'll want to let your code now that you are using the namespace std. Otherwise, you can explicitly say that it is from the namespace std, typing S, T, D colon, colon, followed by C out. But for right now, I'm just going to go ahead and remove that. Since we've already stated that we're using that namespace right here. And I personally find this a lot easier to read it. So I'm just going to clean this up a bit so we know all of our code right here. Then I'd like for you guys to follow along. And we're basically just going to remove this line and write texts to the console from scratch. So we'll go out and get rid of that. And anytime you want to write texts to the console, you're just going to say C out in all lowercase. Yes, the capitalization does matter when it comes to programming. Followed by the insertion operator. So we'll say C out space, the insertion operator put in another space. And then in quotation marks, we'll type in whatever we want to say. So I could say, listen many first-name text and feel free to put whatever you want in there. And then after the last quotation mark, you're simply going to enter statement with a semicolon. And then we run our code. They'll say, this is my first line of text and our cursor will stay right there at the end. Because we did not put in if we wanted it to go down to a new line. Alright, so there you go. That's how you can print out a single line of text to the console. But what if you wanted to print out multiple lines? Well, you have a couple of options. You can always just say backslash n. And then you say something like, This is my second line. And when your program, and there you go. And you can do this as many times as you'd like. Now, although we could just keep typing backslash n followed by any extra texts we want to write. This will eventually get pretty messy and make our code hard to read. So we do have another option. Let's go ahead and remove this extra code here. Then let's copy and paste this line down below. And we'll say something like, This is my second line. Like I said, feel free to throw whatever you want in there. And then we'll go ahead and run our code. And as you can see, it just says that this is my first line of text. Then this is my second line of text with no spaces, no punctuation. It's not on a new line. Nothing. That's because in programming, your code is going to do exactly what you tell it to. And it's not gonna do anything extra to help you out. So basically if you want something to happen, you have to make it happen. So if we do want to clean this up and make it come onto any line, have any punctuation in there, stuff like that. We'd have to type it in manually. So I can say this is my first line of text. Period. Backslash n will create a new line. And then you can put a period. I think that one as well. When I code again and there you go. Probably a little closer to what you're expecting before. And if you do want a blank line in between these two lines, well, you could simply just throw in another backslash n at the beginning here. And there you go. You've got a new line. And you can actually just keep doing that to get more new lines in-between. Should you shall choose. Anyways, I'm gonna go ahead and remove that. And if you remember our other option, instead of saying backslash n and something you may also see frequently would be after double-quotes to put it in another insertion operator. Insight end l, all lowercase. And before the semicolon. Then if we run a program again, you can see that it works just fine. If you remember, I told you that whitespace could generally be ignored by C plus plus. So if I were to go back and remove all the spaces in-between C out her operator, the quotation marks here. And at the end here, I'm going to program. It would still run just fine. However, I simply told you guys to put in a space, as we wrote this for readability, as something you'll always want to make sure to do when it comes to programming is make your code as readable as possible. Because although you may build a read and understand it today and you may think that anyone else who looks at it can get an understanding of it as well. You want to make sure that you can understand that tomorrow, a week from now, a month, or even years down the line without issue. And that is something I want you to keep in mind and practice as we continue on throughout this course. Anyways, that's pretty much it for printing out text. But I'm going to show you one last trick. So just as we can throw in another insertion operator to throw in this N line here to create a new line. We can also just put in the texts there as well. So instead of creating a whole new C out statement, can actually just copy this text along with the quotations. Get rid of this line entirely. And after this extra insertion operator, we can just paste in our line. And I'm actually going to bring this down onto a separate line just so it all stays within the screen here. Just like that. And this will run just fine. And we could even throw in our M line here along with another insertion operator in-between. And all of this will run as before. You go. So anytime you use the CLI object to print text to the screen, I'm going to make sure that you include the iostream, is that as were the object comes from. And when I mouse over it, you can see that it comes from the std namespace, which we stated right here. Then we always follow it with the insertion operator, which is going to insert what we put after it into our program. Then anytime you want to add more than one thing, you simply follow it with another insertion operator. So we said C out. Then we said we want to put exactly what's in-between these double-quotes to the screen. So he said This is our first line of text. Put an another insertion operator. Put an end line here to signify that we want to go down to a new line. And if you remember, we also code just put backslash n, like that. Then because we're adding something else, we put on another insertion operator. And then we put in another literal line of texts that we want it to be printed to the console, signified by a double-quotes again. Then lastly, at the end of this statement OR instruction for our program, we simply put a semicolon. Anyways, we'll be using the CL object many times throughout this course. So don't worry if you don't have it all down now, you're gonna be getting a ton of hands-on practice. And just to quickly recap on the syntax or the rules on how to write out texts to the console. You're simply going to say Cl, followed by the insertion operator, followed by double-quotes, and a semicolon at the end. And inside those quotes is where your text is going to go. And you can even put nothing there at all and it will work just fine. So go ahead and play around with that a bit. Re-watch the video if you need to, and I'll see you in the next one. 7. Chapter 2 (Hello World): Comments: In this video, we're gonna be talking about comments in programming. Real quick. Let's go ahead and remove all this extra code outside of her Cintiq statement and our return statement. So what our comments and programming, well, they're basically just what you think. A comment is basically texts that you write yourself to make your code more readable. So real quick, Let's go ahead and put back in our texts. We're writing out hello world to the console. So we'll say C out, insertion operator. And then in quotes come say hello world. And don't forget the semicolon at the end. So we've already worked with this program a few times now. We already know what it does. When we run our code. It's simply going to say Hello World on a single line. But what if we wanted to add in a few lines of her own to make this code more readable. Now, right now, I get that our program is pretty small and basic and this isn't super necessary. But as we get further down the line and start working with more complex programs, you'll definitely want to add in as many comments as possible to make your code as readable as possible. Because even though you might know what it says today, you wanna be able to look at this years from now when you don't remember writing any of it and still be able to read it. No problem. So how do we write a comment? On any line in our code here, we can put two forward slashes and everything after those two forward slashes will be completely ignored. When we run our program. You put in some text like this and run our code. And it's just going to say HelloWorld, just as I did before. In this comment, will automatically end once we go down to a new line. Now, there is a way to make comments that will run through multiple lines. So if you type forward slash followed by the actual symbol, everything past that point will be a comment. As you can see, it's commented out everything past that. The way to n, That is what's another Astro symbol followed by the forward slash again. So as you can see, that as much as I want here, but a new line. And it will all work just as I did before. There you go. Anyways, I'll go ahead and removal that so that you have it. You now know how to add comments to your code to make it more readable. But there is one more use case for comments, which is incredibly useful, which I'll go over with you now. So because anything within a comment will be ignored, you can actually come out a single or multiple lines of code to test your program without them. This can be incredibly useful when trying to figure out what is causing an issue or just to see how your code runs without something. So for example, let's go ahead and copy and paste this HelloWorld line a few times. Just like that. And we'll comment out a couple of them. So I could run my code around just as it did before. But it's only going to print out Hello World three times because this fourth one here is commented out. Now, obviously this isn't a great example of when to use this, but we will be doing this throughout the course. So you will get some hands-on experience of using this to your advantage. But I just wanted to make you aware that as possible and make you aware of all the use cases for comments. Anyways, that's pretty much it for this video real quick. I just want to mention that from this point forward, when I was starting every single video with a clean slate that looks just like this. And we'll be doing that from this point onward unless otherwise stated. So thanks for watching and I'll see you in the next one. 8. Chapter 3 (Variables): Intro To Variables: In this video, we're gonna be talking about variables. A variable is basically just a container for storing data values or a name given to a memory location. So far we've learned how to print something out to the user. But what if we actually wanted to get information back? Or what if we wanted information that we could refer to, access and manipulate later? Well, that's where variables come into play. So how do we create a variable? Well, let's go over that. So the syntax for creating a variable, or at the rules on how to create a variable is as follows. First you're going to start with the datatype. And this represents the type of data that you want to store inside the variable. You might want to store numbers, words, characters, or a true or false value, et cetera. And we'll be going over the different datatypes in the upcoming videos. After the datatype, you're going to put the name of the variable. Now, when naming a variable, you want to make sure your name is descriptive to what it is you're storing. So for example, let's say you were making a pinball game. You wanted to store the current score. Instead of just calling this number one, we might want to call it current score. Then we might want another variable for storing the high score, for example. And we wouldn't want to name them accordingly. Now, when naming a variable, there are a few common naming conventions which I'd recommend you use as you will see them quite often. And especially if you're working with other people, this is likely what they'll be doing as well. So generally, when naming a variable, it will always start with a lowercase letter. Also, I'd recommend writing it in a CamelCase fashion, meaning that the first letter is going to be lowercase. There's not gonna be any spaces or punctuation. And the beginning of each subsequent word after the first one, we'll start with a capital letter. The other option, which you'll see frequently for naming variables is snake case warrants those separating them by just having a capital letter at the beginning of every subsequent word out to the 1st, you'll just separate them with an underscore. And then you can leave the first letter as lowercase like so. Now for the remainder of this course, I'm gonna be writing our variables with a lowercase first letter, and then Kimball case. Because of the z is the most often and I find it easier to read personally. So I'm going to go ahead and write variable name, like so. And I'll be showing you other naming conventions when it comes to other things as we continue on throughout the course. So to declare a variable or basically state that you're going to need a variable. In this program, we're going to start with a datatype, followed by the name, and then a semicolon at the end. And then you have that you've created a variable. Like I said in the upcoming videos, we'll be going over the different data types and how to create them specifically and their use cases. Now, before we actually use this variable and enter code, we need to give it an initial value. There are a couple of ways to do this. So first of all, let's say that this variable was storing a whole number. Okay, so we have our datatype for storing a whole number. And then let's just call this score. For example. Let's say this was a score and a soccer game or something like that. Well, we could set its initial value equal to 0, like so. This is perfectly valid and now we've declared and give our variable an initial value so that we can use it in the future. You'd never want to use a variable before giving it an initial value for you end up with various errors. Another option would be to declare it now, like we already have, and to give it an initial value later. So let me show you what that looks like. So after we've declared a variable up here, we can reference it and give it an initial value later on in our code. So how do we do that? First, we just say the name of the variable that we want to reference. So we can say scope, then we give it the value that we want. So I could say equals five. For example. This would work as well. As long as we give our variables a value before using them later on, we are all good to go. Now, there's another way to declare and initialize variables that I'll show you right now. But personally, I wouldn't really recommend it. But in case you see it, I do want to let you know that it is out there. And who knows, maybe you'll decide to use it in a future project. So what I'm gonna be showing you is how to declare and initialize multiple variables on the same line. So we can do say the datatype followed by the names of the variables that you want of that same data type. So for example, let's say I wanted to create multiple variables for storing numbers. I'd put it in the proper data type, followed by the name of my first variable. So I'll just say var one, for example, separated by a comma. And then I can do as many variables as I'd like. I can do if R1, R2, R3, so on. Now these can have whatever name you want. I was just putting 123 as a basic example. Now, although this declares multiple variables of the same datatype, you can also give each one a initial value if you'd like. So after any of them, I can say equals. And then, and I could do this for each one. I could do it for just one or two of them, et cetera. So this is a way you can declare and initialize multiple variables in the same line as long as they have the same data type. And you could still always initialize them later on, like we did above. So for example, down here, I can say var three equals four. Now, I would have declared it here and initialize it down here. And this would be perfectly valid. Anyways, that's it for this video. The next video, we're actually going to start working with our very first datatype being integers. So thanks for watching, and I'll see you in the next one. 9. Chapter 3 (Variables): Integers: In this video, we'll be going over our very first variable datatype, being integers. Integers are used for storing whole numbers, will be going over data types that can store numbers with decimal points or floating point numbers later. Real quick, integers are not the only data type for storing whole numbers. But because it is the one that is most commonly used is the one that we'll be going over today. And don't worry if you ever did need to use one of the other ones. They are used the exact same way. The only difference is the range of values in which they can store and the amount of memory and they take up. So first off, there are two different types of integers. Basically the only difference between assigned and an unsigned integer is assigned ones are able to go below 0. So they can store anything from this negative value here to this positive one here. And an unsigned integer does not have permission to go below 0. But because of that, it can store it twice the positive value and that there is the only difference between the two. So when would you want to use an unsigned integer? Well, for me personally, if I know that my integer doesn't need to go below 0, I'm always going to use an unsigned integer so that I have the highest possible max amount of positive numbers available to me without using any extra memory by using a different data type. So for example, let's say you made a program for keeping track of say, a soccer game and neither team can possibly go below 0, you might as well just use an unsigned integer. So let's go ahead and create our very first variable of the integer datatype. So if you remember, the syntax for creating a variable is as follows. First, we are going to start with the datatype. In this case, we're going to say in all lowercase to state that we want to create an integer followed by the name. For this example, let's go ahead and say age, and then a semicolon at the end. And there you have it with declared our very first integer variable. Now, before we use this variable, we need to give it an initial value. Also, this is a great time to let you know that all of our code is read from top to bottom inside of our main function here. So I cannot initialize the variable age before the declaration of it. So for example, if up here I tried to say age equals 0 with a semicolon at the end. It wouldn't know what age is. Because like I said, it's read from top to bottom. But if we take that and put it below here, will work just fine. So just remember the old code is read from top to bottom. So in this first line, we declared a variable. And the second line, we initialize a variable. If you remember, we can do both of these in the exact same line. So I'll go ahead and delete those quick. And we're going to say age equals 0 and then a semicolon at the end. Now, what if we wanted to print this variable out to the user? Well, we basically do it the same way that we printed out texts to the user. So down here, we're going to say C0. And then the insertion operator, followed by the name of our variable. Remember anytime that we want to access or edit a variable, we need a user's name. So we're gonna say CL to print something out to the user. Then we're just going to type H. And the capitalization, by the way, it needs to be the exact same as the one up here. So if, for example, I changed this to a capital a, this variable is no longer recognized. Okay? So like I said, your capitalization in programming is very important. So if we did want to capitalize here, and we also need to be a capital down here. Now, let's go ahead and run our program. And as you can see, it prints out 0 to the console. We can also edit the value of a variable after it has been declared. So for example, down here, we could say age equals three. Now, we could have done it like this, where we declared our variable and then initialize it here and then printed it out down here. But I'm just showing you that after you give a variable and initial value, you can edit it later on. So we said we wanted to create an integer named the age. We set it equal to 0. Then we referenced H again. And so we want to set it equal to three. Then we printed it out to the console. So I run a code. And there you go. We printed out three. Now if we take a look back at our variable here, isn't very descriptive. Seeing is it just prints out the actual value of H here. So why don't we throw in some texts defects that. So back here, we wrote CL after insertion operator. Let's go ahead and put it in a couple of quotation marks, followed by another insertion operator before our variable name here. Then inside these quotes, we're going to say your age is colon space, just like this. And again, it's a lot easier to read now. So this is your age, is three. Now, one last thing I want to go over with you before we move on to the next video. Is that because our code is read from top to bottom. If we copy the outline here and put it after our declaration of a variable, like so. We can actually print out H twice with the two different values. So we'll create an integer variable called age, set it equal to 0. And then we'll print it out down here. Then we'll change it to three and print it out again. When our program, you can see what that looks like here. And there you go. The first one prints on 0, and the second one prints out three. And remember if we want to go ahead and fix that up so it looks a little cleaner. At the beginning of this one down here, we can simply say backslash, backslash n and R code. Create a blank line for us. And there you have it. You now know how to create and work with integers. And don't worry, we're gonna be using these a lot throughout the course. So you're gonna get a ton of hands-on practice. Also, the way we have it up here, where we declared and initialized a variable on the same line is generally considered good practice when you're able to do it. The reason for this is because you do not want to use a variable before you give it an initial value. When you declare it, you just give it an initial value right away. You'll avoid any serious issues. And lastly, before I forget this here is considered a signed integer. That is the default integer type. If you want, you can even explicitly defined this as a signed integer by saying signed space and then end just like that. But this, and this are the exact same thing. If you want to create an unsigned integer, you can simply say unsigned, like so. Anyways, that's all for this video. Thanks for watching, and I'll see you on the next one. 10. Chapter 3 (Variables): Floating Points: In this video, we're going to be talking about floating point types or numbers with decimals. Now we aren't going to go over every single floating point type in C plus plus. But we are going to be going over the two main ones. And I'll tell you how you can take the knowledge you get from this video and apply it to other floating-point types as well. So let's go ahead and take a look at these two main floating-point types in C plus, plus. The first one that we have here is a float. And a float can roughly hold seven significant digits. Now, if you don't know what significant digits are and you do plan to work with floating point types. I highly recommend looking into it. However, to briefly summarize what they are. The significant digits are each of the digits of a number that are used to express it to the required degree of accuracy starting from the first non-zero digit. Now, hopefully that made sense. If not, don't worry about it, you can always look it up at another point in time and it's not going to affect your ability to follow along in this course are really decode in general. However, that being said, if you do plan on working with data, that needs to be incredibly accurate, when you do plan on working with decimal point and values. I would highly recommend looking into it so that you know what each of the floating point types can store precisely. Then our second one here is a double, which can roughly store 15 significant digits. And the reason it is called a double is because it actually takes up twice as much space in memory. And the precision of a double compared to a float is about double as well. Anyways, let's go ahead and take a look at how we can create floating point types of r. So just like with any other variable, we're going to start with the datatype. So for our first one here, we're going to say float, all lowercase like that. Then we'll say something like height for the name. Then let's set this equal to something like 4.3. And then we'll just put a semicolon at the end. Now, one thing I do want to show you here is that if I mouse over this value, you can see that it believes this value here as a double. And that's because the default floating point data type in C plus plus is a double. So if you aren't actually working with a double, you need to explicitly tell the compiler that you can do that by adding a specific suffix depending on the floating point type you want to work with. And for a float that is going to be a lowercase, just like that. Now, if I mouse over this, you can see that it is now a float. Alright, so if you aren't working with a double, you need to add a suffix based on the floating point type you are working with. And you can easily find all that information online for the different floating point types should you choose to get into them. But like I said, for the majority of these will be the two types that you will use. Then let's also go ahead and create a double. So below this, we're gonna say double datatype. And once again, that was all lowercase. Then for a name, let's just set this equal to 5.6, something like that. Because the default data type for floating point numbers is a double. We don't need to add a suffix. Now, just to let you know, the syntax for creating other floating-point types is going to be the exact same as what these ones here. Just as it is with every other variable. We're going to start with your datatype, followed by the name of your variable. And remember, we only need these two things to declare it. And then we can also assign it on the same line, like we did here by adding in this equal sign or what some of you may know as the assignment operator. And I'll be using these words interchangeably. Just know that it is the same thing, followed by a value here. So like I was saying, we could have just declared our variables like this. Access them later by using just the name. And then set them equal to something later on in our code. Like so. That would have worked just fine as well. Anyways, I'm gonna go ahead and undo that. And one more thing I do briefly want to mention is that unlike with whole number data types, floating point types cannot be signed or unsigned. They are all just assigned, meaning they can sort of positive and negative numbers. Next, let's go ahead and print out both of these variables. So down here, we're going to say CL. Then we'll add another insertion operator, and we'll say inline. Don't add in another insertion operator here and say hi to. Just like that. And so now we've created a float called height and gave it a value of 4.3. Then we added the suffix to specify that it was a another floating point type other than a double, which is the default. Then we created a double as well called height two and set it equal to 5.6. And once again, a double does not eat a suffix because a double is the default data type for floating point numbers and or numbers with a decimal. Then we said we want to print out something. And whatever we want to print out, we wanted to print the height being 4.3. By the way, this f will not be printed. This is just a suffix for the compiler to explicitly say what this datatype is. Then we said that we wanted to go down to a new line. So we added in this line here, and on our new line we print it out height to 5.6. So if we run a code, you can see that everything is working just fine. Once again, note that suffixes for floating-point types do not get printed out. So there you go. That is how you can create and work with floating point types. And you'll be seeing more examples of this later on in the course as well. Anyways, that's it for this video. Thanks for watching, and I'll see you in the next one. 11. Chapter 3 (Variables): Booleans: In this episode, we're gonna be talking about Booleans, also referred to as bulls, bulls eye, or a data type for storing true or false values, also known as on or off or yes or no, or one or 0. So let's go ahead and create an example of one. For example, let's say we're creating a program for a light. We might want to store a variable for whether this light is on or off. So how do we do this? Well, just like with every other variable, we're going to start with their datatype. In this case, we're going to say Bool, all lowercase. Then we're going to follow it with the name. Will name this something like is. Then we'll go ahead and set this equal to false. By the way, just as a reference up here. If something is true on yes, whatever you wanna call it is the same as setting it equal to one, are saying that the value of it is one. And if something is false off, now I have you. It's the same as saying it is 0. So this here are a ball being is on set to false as the same as saying as on is 0. And this will work either way. Anyways, I'm gonna go ahead and put us back. Make sure that the word false in all lowercase as well. Anyways, let's go ahead and put this to the console and see what happens. So let's say something like CL is on. Awesome. Singers will be printing things out a time to the console throughout the course. I'll just be saying Cl, followed by what it is we'll be printing just to keep things going and keep the dialogue a bit cleaner. Anyways, let's go ahead and run our program. You'll see that it actually prints out 0. Because like I said, false is the same as 0 in true as the same as one. By the way, it can only hold one or 0. Or you can explicitly type false or true in all lowercase, but it cannot hold other numbers outside of that. Now, there actually is a way to have it print out false instead of just 0. And I'll show you that here in a minute. Real quick. I'm gonna go ahead and change this to true. And when I program again, you can see that it prints out one. Now, what I want you guys to do is actually copy RSC outline here with a semicolon. And then down below that, we're going to say CO. Then we're going to say cool alpha. Then a semicolon at the end. Just like that. Once we type this, we're actually enabling something. So basically, once this is enabled, anytime that we print out a bull value, it's going to actually say true or false. So if we paste our line, we just copied down below here, it should now say one because our value is true. And then after we turn on the spool Alpha here, it will say true. And let's make sure that this is on a new line, on our first-line here at the end. And let's go ahead and add another insertion operator. And we'll say end line just like that, or end l, I should say. When we run our program. And there you go. Now says one and then true. And this would be the same if we went ahead and change this to false and run a program, it will run the exact same way. Okay? And what we're gonna do is actually copy this top line again with our nth bind here. Then we'll delete this bottom one and paste them, this new one, like so. Then we're gonna go ahead and paste a couple of more times. And then below that will say CL, no, Alpha, all lowercase. Just like that. Then we'll go ahead and paste outline a few more times. Then when we enable for alpha here, instead of printing out boolean values as 0 or one, it will say true or false. And then when we disable it by saying No Bull Alpha, which was the default, it is going to go back to saying 000. So I'm just going to have a couple of comments here. And run our code. As you can see, 0 the first time, then print it out false three times, and then 03 times. Then we have a blank line because we went down a line here. By the way, the spool Alpha in no, alpha is part of the std namespace. If you did want to explicitly say that, once again, you could say std colon colon followed by alpha is also acceptable. But because we already have up here and that we are using that namespace, we do not need to redefine where this is coming from or explicitly saying where this is coming from. Again, one last thing to wrap up the video. Something you can do instead of just saying that you're going to use this namespace in general, which at this point we are using for CL, N line and we'll Alpha. You can explicitly say which things you're gonna be using from that namespace. So I could say using std colon, colon in line with a semicolon at the end, like so. And then you could do the same for CL and pull Alpha and notebooks cellphone. But I'm gonna go ahead and put that back to the way it was. Anyway. So you now know how to use and create Boolean variables, and we'll be using these more throughout the course as well. That's it for this video. Thanks for watching, and I'll see you in the next one. 12. Chapter 3 (Variables): Chars: In this video, we're going to be talking about the char data type. Now char is short for character, and thus datatype is used for storing single character. The range for this data type is negative 120 2027, signed, or zero to 255 unsigned. And you can make a signed and unsigned version the same way we did with the integer variable. Now, the chart datatype can also use ASC, ISI values, tos, play certain characters as well. If you want to know what these are, you can simply look up an A-S-C-I-I table reference. This is short for the American Standard Code for Information Interchange. Anyways, let's go ahead and create a chart datatype. So we're gonna say chart to say the data type of the variable we are creating, just like we would with any other variable. Then we'll give this a name. E.g. we'll call this letter. Just like that. Then we'll go ahead and initialize it right now as well. And we'll say something like equals. And then in C plus plus, when you're giving something the value of a single character or a char, you want to put it in single quotation marks. And then we could say, we could do capitally if we wanted. This will be printed out just like it would if we were to put this in double-quotes, one over C out statements. So fat as a capital, it will be printed as a capital. And if it has a lowercase, it will be printed as such. We can go ahead and test that out now. So down here, I'm trying to say, yeah. Letter. Then a semicolon at the end. By the way, for those of you using Visual Studio while you were typing, you may notice this drop-down box. And you can actually access it by using the up and down arrows and then hitting Enter to select one. Here, it automatically comes up with our variable name as we're typing it. And we can hit Enter to fill it out automatically. And this will become more and more useful as we were running out larger programs and typing quite a bit. Or if you have longer variable name. Anyways, let's go ahead and run our program. And as you can see, L typo. You can also go ahead and print out a random A-S-C-I-I character by typing in a number associated with one here. So e.g. I'll just put 25. Now, this isn't actually random and it'll print out the same thing every time. I just meant that I don t know what character this will print out. So we've gotten to run it. And as you can see here, it prints out a down arrow. And just to prove that it isn't random, if I run this again, It's going to print out a down arrow every single time. Like I said, you can always look up a table reference of the ASC II characters if you want to see what those are and mess around with that. Notice that because we didn't put in a single character as the value of our char variable here. We took out the single quotes. Also, just to show you this, let's go ahead and remove the value 25-year. Put our single quotes back in. And then I'm just gonna go ahead and type number like three and run our code. That will work as well. And just to show you this, just says before, you can go ahead and make this a signed a chart by taking sine. Remember that this is the default. So this and this are the exact same for say, unsigned, like this. And then this can only hold positive value. Now if we want to call it again, you can see that it still works. Anyways, that's all for this video. In the next video, we're gonna be going over the string datatype used for storing multiple characters, words, or phrases. So thanks for watching, and I'll see you on the next one. 13. Chapter 3 (Variables): Strings: In this episode, we're going to be talking about strings, which is a datatype for holding a series of characters or a sequence of characters. So basically anytime you want to store a series of text or numbers longer than a single character, you'll want to use a string. We are also going to include the header file string. So below, this will copy what's up above. So say hashtag include the less than symbol. We'll say string, followed by the greater than symbol. And there you go. Well now have access to all the code within this file. Also, for those of you on Visual Studio, you can actually right-click on this and go down to a go-to document. And you can actually look at the code within that file. But for the majority of you and myself, That's not really important. But for those of you who may end up needing it or are just curious, there is one way you can go ahead and look at that. So how do we create a string? Well, just like any other variable. So we're going to do a string, datatype and all lowercase. Then we're gonna go ahead and give it a name. So let's call this name, for example. Then we'll go ahead and set it equal to John Doe. Remember, anytime that we're working with a series of texts, we're going to use double-quotes. And when we're working with just a single character, will use the single quotes. Because we want to store a string of characters, we're going to use the double-quotes. And we'll say John, just like that. And if we want to print this out, we can simply say C out. And if we run our program, you can see it says John Doe. And by the way, a string, along with a lot of other things we've used and it will use in the future are all part of this std namespace. You can define it explicitly there if you'd like. You can even see that if I mouse over this in Visual Studio, that it lets me know that it is part of that namespace. But for the remainder of the course, I'm just going to leave this here so that anything that does a part of that, I do not have to explicitly mention it. Now, real quick, before we end the video, we're actually going to go over a few functions that are part of the string class. Like I said, we'll be going over are functions in classes. Later on in the course. There's gonna be multiple videos on it. So don't worry about understanding what they are or how they work right now. But I just wanted to go over a few of them with you so that you know how to use them. And just so you have an idea of what kind of functionality is out there for strings. So for example, let's go ahead and clean up this line here. So after a c out and the insertion operator, Let's go ahead and put double-quotes. And we'll say My name is. Just like that. Don't forget, we need to add another insertion operator to separate our variable from this here. By the way, this thing here between this double-quotes, Yes, I know it's text, but it is also referred to as a string literal. Now, when we run a program, they'll say My name is space and then the name that we inserted. But let's go ahead and also take a look at a few functions for the string class. So real quick, Let's go ahead and add in a few comments. Here. I'm going to say string to me. Right here, I'll say. And then down below, we're gonna say the length of our name, including spaces. And then down here, we'll say the plural version. And I'll show you why in just a minute. Alright, so the first method, we're going to go ahead and try out as the length method. And like I said, we're gonna go over functions indoor methods later on in the course. So for right now, just follow along just to kinda see how they work. So we're gonna do is say name, being the actual name of our string here, and then die, also known as the member access operator, or a period symbol. And then we're going to say length, all lowercase, open and close parenthesis, and a semicolon at the end. Then down here, we're going to do another one by saying name. Diet, push, all lowercase, underscore back, and then open and close parentheses and a semicolon at the end. And then inside those parentheses, we actually need to put in a character. And if we mouse over this, you can see that inside the parentheses there that says it takes a char value. So we're gonna put in single quotes. And then an S Just like that, because we're only working with a single character. We used the single quotes. And now what it's going to print out is that our name is John Doe. Because that's what we typed right here. Or whatever you typed there will be printed out. And then it's going to say the length of our name, including spaces. I'm just going to be eight. Then this function here actually adds an S to the end of our string or whatever letter we input here. However, this isn't gonna do anything. So if we run our code, it is going to work. And as you can see, it runs just as I did before. The problem is, we are actually doing anything with these values. So even though I now have the length, we didn't print it out to the console. So let's go ahead and do that. We're going to say CO, followed by the insertion operator and then our name dot length. And this will work just fine. So as you can see, there's John Doe and then eight. And we can go ahead commensurate new line by saying like we have in the past. And another insertion operator, like so. And that'll clean that right up. And then down here, we can actually print out this year because all this is doing is adding an S to our name here. So after this, we're gonna go ahead and say CL to create a new line. And then we'll print out our name. Make sure you put a semicolon at the end. And we'll run our code. Now says My name is John Doe. We printed out the length of our name as this is what this function here returns as that value. And we're able to put that value right here and our c out statement. And then down here, we tried out another function that was part of the string class, which allows us to add a character to the end of our string. So instead of John Doe, it says John Dos. And we had a print that out on a separate line. Like so. And like I said, don't really worry about getting all that down right now. I just wanted to kinda show you some of the possibilities with strings there. But you're not going to have to know it going forward. And if you are interested, feel free to look up the string class and some of its other possible functions. The only thing you really need to take away from this video is that when working with a string or an MP throwing in the string header file by using this include statement at the top. Then we created a string the same as we did with any other variable type, except that we specified that it was a string and it wasn't an integer or something like that. Then we used it the same way if we would with any other variable when printing it out. And while we're here, I want to show you that if we try typing in a different value, such as 54 or something like that, we're immediately going to get this error. And it's going to tell us that this isn't suitable for that datatype. So it's good to note that it's not going to let you mess up. If you try putting in the wrong data type. There's just going to let you know right away that it's incorrect, at least when it comes to something as drastically different as strings and integers. And what I mean by that, we'll get into in a future video here, but just wanted to show you that anyway, so that's it for this video. And from now on through the rest of the course, we're actually just going to go ahead and leave this include statement for the string file. Anyways, thanks for watching, and I'll see you in the next one. 14. Chapter 3 (Variables): Constants: In this video, we are going to be talking about constants. Now, a constant is basically just a variable. You don't ever want to change. And you might be wondering, why would I do that? Well, the reason is when it comes to programming, you want to do everything you can to make sure that nothing can go wrong with your code, keeping in mind human error. So for example, let's say you were making a program four o'clock, and you wanted that clock to know that there are 60 minutes in an hour. And that was never, ever going to change. You would want to make it a constant variable such that there was no possible way that could be messed up and mess with the rest of your code it down the line. So what we're gonna do a say const, which is a keyword to represent that we want to make a constant variable so that a CON as t. And then we're going to say, and then we'll say minutes per hour for the name there. And then we're going to go ahead and set that equal to 60 and put a semicolon at the end. There you go. We've now created a constant variable that can not be changed. You can do this with all the different variable types. Well, you have to do is put this constant keyword before the data type. So basically the syntax or the rule for making a constant variable. And you're gonna say const, followed by the datatype, followed by the name, then the assignment of the variable. So put them the assignment operator, followed by the value. Now, one thing to note with constants that we cannot just declare it like this. And then later, say minutes per hour is equal to ten. For example. If I mouse over the semicolon here, you can see why that is. This is the const variable. Minutes per hour requires an initializer. If you're gonna make a constant variable that you don't ever want to change. You need to initialize it and give it that value that you want to stay permanent, right, when you declare the variable. So unlike with normal variables, you cannot declare a constant variable and then initialize it later. That's all I got to be on the exact same line. So we'll say const int minutes per hour equals 60, just like that. And just to show you that we cannot change this later. Now, I know it doesn't look like a very big deal right now. But as you start working with bigger and larger programs that have multiple lines of code and may be multiple pages. You want to make sure that there's no possible way. Thus, minutes per hour could be changed because it needs to be 60. And especially if we're making a clock, right, if that is even a single digit off by the end of a month or a year. Whoever's clock this is, is gonna be messed up, right? So we just want to make sure that we make it as difficult as possible for our code to be used incorrectly. And even though the program itself isn't going to do anything, you don't tell it to do. You want to account for human error? Plus, it is not that hard to just add in this quick keywords and make sure that that value is permanent. And just to show you this, if I were to say minutes, Our down here, and then try to set it equal to something like ten. I'm going to get an immediate error telling me that this is not a modifiable value because this is a constant value. Whereas if I were to remove this, it is no longer constant. I can change it to whatever I want. So I'll go ahead and put that back. And because of how constants work and because of how code is read from top to bottom, even if I had multiple lines that didn't mess of it in my code already. As soon as I put that constant keyword there, It's going to make anything that tries to adjust this value come up as an error, letting me know that that can not be done. So if there was something that was messing with that before throwing in this const keyword is going to easily give me access to anything. There was trying to mess with that value. And one last thing before we end this video, I just want to show you that even though this value is constant and that we cannot change it, we still have access to the value is considered a read only value. You can read it but you can't write to it, you can't mess with it. So if we say CL minutes per hour and then run our code, you can see that we can still gathering that value. We can still tell that it is 60, but we just can't change it. And there you have it. You now know everything there is to know about constants and programming. Anyways, that's it for this video. Thanks for watching, and I'll see you in the next one. 15. Chapter 4 (User Input): Cin User Input: In this video, I'm finally going to be showing you how we can actually get input back from the user rather than just sending it to them. So for example, let's go ahead and ask for a few different bits of information from the user and then go ahead and print it back to them in the console. So real quick, I'm just going to add in a few comments. So we'll ask for their first name. Then we'll ask for their last name. And lastly, we'll ask for their age. Then we'll go ahead and print this all back to them. This is just a quick example of using comments to make the code more readable. But when you actually start writing bigger and larger programs, I would definitely recommend going into much more detail and really breaking down what each line of code, where each function is doing when we get to that point. Because when you come back and you look at your code, whether it's days, weeks, months, or even years down the line, or someone who's never seen your code before, it takes a look at it. It'll be that much easier to figure out what is going on up here at the top. Let's go ahead and create a few variables for storing their information. So first, we'll go ahead and create a string for storing the firstName. So we'll say string first name. Then we'll just put a semicolon right here at the end. Then below that, we'll say string lastName. And below that, we're going to go ahead and say age. Just like that. We've created three variables and we have just declared them. We have not given these and initial value. So although we could go ahead and put in an initial value for each of these, I'm going to go ahead and leave them blank just to show you that we can have the user put in an initial value rather than us putting in an initial value and just allowing them to change it. Right here, we're gonna go ahead and say CL. And then in quotes will say type your name. Colon space. Just like that. I don't put a semicolon at the end here. And then down below, we're actually going to go ahead and collect this information from the user. And to do this, we're gonna go ahead and say c n. Then the greater than symbol twice, followed by our variable name, first name, and then a semicolon at the end, just like this. Whereas up here we said c out to output information to the user. We're going to say C N to get input back from the user. And also instead of listen operator, that's going to insert something into the program or the console. In this case, we use the extraction operator to extract something from our program back to our code here. So the two less than symbols as the insertion operator, and the two greater than symbols is the extraction operator. And instead of outputting information, we were getting in information. Now personally, I like to think of these insertion and extraction operators as arrows. And imagine that my program is out over here somewhere. So I'm outputting information into my program and I'm inserting it back in this way to where my code is. So that's one way to look at it if that helps you. Great. So right now, if we ran our program, it's going to say type your first name. Our cursor in the console is going to end up right here. And they can type their firstName. And then when the user hits Enter, it actually puts their text into the program. So we actually get the information back. And that's kinda the same with our get method down here. And that way trust or press Enter. Once the user presses Enter, we can actually get information back from them. And so that's when everything executes. So right here we say type your first name. They can type whatever they want and then hit Enter. And that information or that string of characters is going to be stored right here in our firstName string. But we still need to type it back to the user or do something with it. Otherwise, it's just going to sit here in the variable and we've never actually used it. And it will be doing that down here in just a moment. But first, let's go ahead and create a blank line. So we're just going to say CO. And just like that, Let's go ahead and copy and paste this down below here. Since we're pretty much going to do the same thing to get their last name, except instead of firstName, we're going to say lastName, which is the name of our other variable. And up here we'll say type your last name. Then, just so all of our texts isn't bunched up together. Let's go ahead and add an another blank line. So I didn't undo this. I'm just going to throw in another insertion operator and say online. And I'll do the same. Then let's go ahead and copy all of this again and paste it right down here. Then we'll say type your age. Then the information they type in. We wanted to go into our age. Alright, and make sure when we get to this part in a program that you actually do type in a whole number. Because as of right now we have no way of checking whether or not they actually put in a whole number or integer variable can only hold a whole number. So to avoid any issues or any errors, just make sure you type in a whole number. If not, you can always close it out and re-run your program. Later on in this course, we'll actually be going over how we can check for different conditions. But for right now, we'll just make sure that we take everything correctly. Then we're gonna go ahead and print out their full name and age. So let's go ahead and say CL. Your name is space like that. And then we'll throw in another insertion operator. And we'll say firstName, followed by another insertion operator, double-quotes space in there. So another insertion operator and say, all right, and then down below that, we'll go ahead and say sciatica line. Another insertion operator. And we'll say your age is colon space, another insertion operator. And then go through under age. What a semicolon at the end. Let me zoom out here so you guys can make sure all of your code matches mine. Then we'll go ahead and run the program. So let's type of firstname, type, whatever you want and hit Enter. It's going to ask for our last name. Now notice when we hit Enter after typing your name, it did count. That is going down to the next line. So then this end line here went down one more, and the next one went down one more. So there's actually two spaces. So if we do want to clean that up, we can actually just remove one of these n lines from each of these. Otherwise, I think it looks just fine the way it is. I just wanted to make you aware of that. Then going to type in anything you want for the last name, Enter again. Then I'll ask for h. Going to type in a whole number. Can be anything you want, doesn't really matter. And hit enter again. And it will close out the program. And you might be wondering why closed out immediately. The reason is because we hit Enter at the end here. It actually counted that as part of the sin die. So what we're gonna do is just copy this and go ahead and paste it one more time. So run the program again, just recapping our stuff. And there you have it. So and that's for firstname, type, whatever we want as her last name, separate everyone. As for your H, same thing. Just make sure it is a whole number, since that is being stored in an integer, keep in mind, we could have just put this in a string and it would work just fine. Then we print it back to the user. Your name is and your age is followed by their name and age. And just to show you that this would work as a string, I'll go ahead and put string right there, run the program. And as you can see, it worked just fine. But I just wanted to show you that you can collect different variable types from the user. It doesn't have to be a string. Now, hopefully you learned something here. If you're confused at all, I highly recommend you go back, re-watch the video. And if you still can't figure it out and just let me know down in the discussion section below, I'll get back to you as soon as I can. But don't worry if you feel like you've got it, but you still want to get a bit more practice and we'll be doing a ton with those kind of stuff throughout the course. So anyways, thanks for watching, and I'll see you on the next one. 16. Chapter 4 (User Input): Getchar & Getline Functions: How's it going, everybody? In this video, we're wanting to be asking the user for their full name, storing it in a variable, and printing it back to them. Now, you might be thinking, hey, isn't this what we just did in the last video? Well, and sort of as, but in this video, we're going to be using a couple of new methods. So I wanted to stick with something that was already familiar to you. We'll introducing something new. So we'll go ahead and create a string here. And we'll call it full name. Just like that. And we're just going to declare it. We're not going to put anything in. Then. We're going to ask the user for their full name. So go ahead and say C out. And then in quotes will say Please take the colon space at the end. And then down here, we're going to print out their full name. Don't forget your semicolon at the end there. So say CL. And then in quotes, we're gonna go ahead and put backslash n. And we'll say your full name. Colon space, just like that. And then we'll throw in another insertion operator in print though, full name, the name that they gave us. Now in the last video, we went ahead and said sin to collect their full name. Let's go ahead and do that again. Just to show you something. You guys will just take this out. So we'll say cin, name. And then after we print out their full name, go ahead and put in another N line here. Just like that. And you'll see why I put this here in just a moment. Now, depending on what program you are using, There should be a way to run your code without debugging or checking for errors essentially. And in Visual Studio, you can either hit Control plus F5 or you can just hit this empty plate bind here at the top. And regardless of what program you are using, There should be an option for running your code without debugging. But if not, you can just take the Sendai get code. Just copy and paste this a few times and you should get the same effect. You just might have to enter a few more terms, exit out or hit the X at the top of your program. But I'm just going to put the one there that I've had throughout the course and run without debugging. And the reason for this is a bit more complicated than I care to get into at the moment. But for right now, just try and run your code without debugging or copy and paste this line down here multiple times and you'll be fine. So when we run our code, it's going to say, please type your full name. Go ahead and take John Doe for example. And then you'll notice it just says My full name is John. And then it asked me to press any key to continue. And that's because of Visual Studio, at least when you run your code without debugging, it will not automatically close out. Now, why does it just say John? Well, as because of this sin object here that we use to get input back from the user, will actually stop recording information once we put in this space here. That's why as soon as we were done typing Jon, it basically said, okay, I'm just John, right here in for the full name. But what if we wanted to collect everything including the space? Well, we can use a new function called the getline function. Right now, let's just go ahead and comment out this line here so that we can still see it, but it's not actually going to affect our code. So right below our cin statement that we had, we're gonna say getline and all lowercase open parentheses, followed by sin or C, comma space, followed by the name of our variable, being full name, close parentheses, and a semicolon at the end. Just like this. So now what we're doing is we're inputting the information that we get from our getline function here into the variable full name. Unlike our last statement here, which recorded information and put it in a variable up until we hit the space bar or the enter key, thus get line function or a method here is going to collect information until we hit the Enter key. So it's going to select everything on that line. Now, we run our code, we can actually type out our full name. And it will print back our full name. And then real quick, I got one last method to show you. So let's go ahead and comment out our getline method that we just used. And then below that, we're going to say fullname equals getchar, all lowercase, open and close parentheses, and a semicolon at the end. With this method or function here does as just get the next character typed in. And I'll show you how that works then go ahead and shut up your program without debugging again. Remember if he can't do that for whatever reason, just go ahead and copy and paste the sin dot getline a few times. You can always exit out of the program manually. So in, just type your full name, go ahead and type in whatever you want. Hit Enter. You'll notice that it just takes the first character. So we went ahead, type them whatever we want it. Then once we hit Enter, everything we've typed is actually put into the program. It saves just that first letter because that's what we told it to do and printed it back out to us. There you go. Yes. Every time we are working with the console, you guys can literally type however you want and it's not going to do a thing. Since you've had at their everything you've typed is actually going to be put into the program that we're running here. And then it will do what you told it to with that information. Anyways, that is it for this video, I just wanted to show you these couple of extra methods here that you can use when getting user input. Now, the get char function here might not be super useful for you most of the time, but there's definitely going to be multiple times for this getline function will definitely come in handy. And there are actually other ways of using this method which you can look up online if you're interested. But for now, that's the basic one and the one most likely use most often. Anyways, thanks for watching, and I'll see you in the next one. 17. Chapter 4 (User Input): String To Int: In this video, I'm going to be showing you how we can convert a string to a number and how we can do the opposite of converting a number to a string. So let's get into it. Now right here, I have a few different methods you can use. Just just show you that there is a handful of them out there. And there's actually a lot more than just the three that I have listed, but we'll just use this first one for an example. So let's go ahead and create an integer. We'll say number equals five. Then below that will actually go ahead and create a string variable. We'll say string, written number equals. We're going to put that ten inside of double quotation marks because we want it to be a string. So down below here, we'll go ahead and print out our integer number. So we'll say C out. Then below that will create a blank line. And then below that, we're going to go ahead and convert our string to a number. So how can we make sure this works? First, we'll go ahead and say number being our integer there. And then we're going to set it equal. Then here we're going to use a new method. So we're going to say S, T, O, and all lowercase in parentheses. We're gonna put the name of our string. So we'll say red number, close off the parentheses and put a semicolon at the end. So make sure it matches my here. So first we create an integer called numbers so that the five. Then we created a string called random number and set it to a string that has just the number ten in it, nothing extra. And then down below that, we printed out number, skip the line, or went down a line, I should say. And then we set number equal to written number, which should be ten. And we did that with this method is called the S, too short for string to integer, right? Because we want to convert this string over to an integer so that we can set our current integer equal to it. Then we'll create another blank line. And we'll also print out number again. By the way, for the rest of the course, I recommend just running your code without debugging it. And if you are on Visual Studio, remember you can just hold Control and press F5. And as you can see, we said five, then ten. Even though we just printed out the integer number twice. Because we're able to convert this number here inside of our string over to an integer. By the way, for those of you on Visual Studio, if holding control and heading a five is a pain, you can always hit this button at the top. Or if you really want to, you can go to Tools, Options, go down to debugging. General. Way at the bottom. Just uncheck that box that we checked back in the second video. This is automatically close the console when debugging stops. Otherwise you can always leave it checked and then you have both options available to you at any given time. And this is useful because when you actually send your program to somebody to use, It's not just going to stay open like it is in Visual Studio. So it's nice being able to have both options. But while you're just coding and practicing and learning, you can just go ahead and uncheck that and hit Okay. And we'll wait for you to press a key before closing out of the program, even when you run it with debugging on, by just hitting F5. And you can go ahead and remove this sin dot get if you decide to do that as well as I really only have those here for people who may need it. Now, the next thing we're gonna do is convert a number to a string. Well, how do we do that? So instead of converting number five here over to this number being ten, by converting our string to an end like we did up here. Let's just go ahead and comment out both of those lines. By the way, for those of you on Visual Studio, you can simply highlight the lines you want to comment out and then click this button here at the top and coming out all the lines that go select it. And if you ever want to uncomment them, you can just click the one right next to it, just like that. So we'll go ahead and comment those out. So right now, we've created two variables and we've printed out number. And then we've went down a line. So down here, we're actually going to do the opposite. We'll convert our number being 5-year to a string. So to do this, we're gonna go ahead and say Britain number being our string name here. Equals and then to all lowercase, underscore string, all lowercase again. And then in parentheses, we're gonna put in our integer. So we'll just say number, followed by close parentheses and a semicolon at the end. So what's it gonna do? It's gonna start reading number being our string equal to the string value of our number. So basically the opposite of what we did up here, but instead we use those two string method which converts this year to a string. Now all we have to do is print that out so that we can actually see the result of that. So we'll say C out. And then written number. When our program reprint now five twice, once being from our actual integer variable being number, and once being from our string, which is now equal to five, since that's what number was equal to. Well, now you know how to convert a string to an integer and how to convert an integer to a string. And the use cases for this will become more apparent as we continue on with the course and start getting into more complicated code. Anyways, that's it for this video and the next video, we're actually going to begin working with some basic arithmetic. So thanks for watching, and I'll see you in the next one. 18. Chapter 5 (Operators): Basic Arithmetic: In this video, we're gonna be going over some basic arithmetic in programming. So the first thing we're gonna do is create a couple of integer variables. So let's go ahead and say int num one equals five. Then below that we'll say int num two equals ten. Then, well that's we're going to create another integer variable, which is going to hold the result of any math we do in the future here. So I'll say result equals, and let's just set it to 0 for now. Remember, we could just declare it if we wanted to, but let's just go ahead and initialize it. Then down here, we're gonna go ahead and do some math. Now, math in C plus plus works the same way. It doesn't relate. For multiplication. You're actually going to use the asterisk symbol division. You'll use a forward slash, addition, a plus sign and subtraction. The subtraction symbol, also C plus plus, follows the same order of operations that you're used to when it comes to these four here. And there are also some other symbols and different things you can do that also fall into the order of operations table. So if you plan on doing anything serious with math in programming, I highly recommend taking a look at the C plus, plus whatever operations math table and just seeing how that all works. Anyways, let's go ahead and just do some basic addition here. So we'll say result equals num one plus num two. By the way, you can even set a variable equal to another variable. So if I say result equals num, one, result would be equal to five. But we're here, we're going to say num one plus num two, which should be 15. We can test this by saying c out. Run our code. And there you go. And just to show you that this does follow the proper order of operations overhead, and say multiply right here after an M two by two again. So if this was read left to right, it would be a five plus ten, which would be 15 times ten doing you want 50. But because this follows the same order of operations, so we're used to, it's going to do the multiplication first. So it's actually going to be ten times ten, which is 100, plus five, which is 105. And if we run our program here, you can see that that is the case. And in case you were curious about the math here, you can say result equals result times. Until now, it's going to say result being 0. We want to set equal to num one, num two, which we know is going to be 15. Then down here we're going to say result, which is 15 equals result being 15 again times num to being ten, it should end up 1 fifth. Then we'll go ahead and print it out. And there you go. So there you go. You now know how to do some basic math in C plus plus. And if we wanted to, we could even go ahead and ask the user for these numbers. For example, instead of setting these up here, I can go ahead and remove that. And we'll just leave our result to be equal to 0 to start. And then down here we'll say result equals num1 plus num2. But right before that, we're going to see out. Then in quotes will say type. Person number two. Added them flow that will say C out again. Second-order added. And then in-between those, we're going to have to say CAN one for the first one there. And then below that, we have declared two integer variables. We've declared and initialized a result variable. Then we print it out and we want the user to type the first number to be added. And I'm actually going to go ahead and put a colon space in here just to make it look a little nicer than once they take their first number and hit Enter. It's going to ask them for a second number. And if we want, we can go ahead and put in a new line just like that. Then they can type on their second number and hit Enter. And then right down here, it's automatically going to go ahead and add those two numbers together. And at the bottom, we can say something like this. C out and then quotes and put on a new line. We'll say the result is colon space like that. Make sure if they're semicolon at the end. And before I forget, I'm gonna put this colon and space right here. After we ask for the second number as well, we'll go ahead and run our code. Remember to do it without debugging. I don't want to just say 105 or you can type in whatever numbers you want to give you your result right here. So for me it says my result was 15. And there we go. And then if I wanted to clean that up even more because my counsel asked me to press a button before continuing. I could just show on a new line at the end here like that. And this will run just fine. And there you go. It looks pretty nice. Anyways, that is it for this video. In the next video, I'm gonna be showing you a couple of extra operators. So thanks for watching, and I'll see you in the next one. 19. Chapter 5 (Operators): Increment & Decrement: In this video, I'm going to be showing you a couple of new operators that you may not have seen before. But these are fairly common in programming languages. So the first one is the increment operator being two plus symbols, there was simply add one to whatever you're using it on. In the decrement operator, which is simply subtract one from whatever you're using it on. So let me go ahead and show you how these work. First, we'll go ahead and create an integer variable. So we'll just say int num one equals five. Then below that, we'll go ahead and actually use these operators. So for an example, let's just go ahead and use the increment operator. To do this. We're going to say one plus, plus and then a semicolon at the end. And there you have it. It is now going to add one to num one and the shoulders to you. We'll go ahead and print out num one. Num one, just like that and run our program. As you can see, it says sex. And if we were to do the opposite here and say minus, minus and run our program again. It will say for now, this is the exact same as if we did the following. So I'm going to comment that out quick and just say num one. Num one minus one. Just like that. And when I go, as you can see, for again, if I were to put a plus, I would have sent six. So as you can see, it's the exact same thing. It's just a lot quicker and a lot cleaner to say num one minus, minus or plus, plus. Then to do num one equals 91 and then plus or minus and then one. And you'll often need to simply subtract or add one to something, especially in programming. So that is another reason why this is available for you and why it's a good tool to have. Now one more thing I want to show you is that you can actually put these operators on the other side of the variable and it actually does something different. So if we go ahead and erase that line we just created and then uncomment out or num one with the decrement operator there. If I put the minus, minus on the other side or the decrement operator and run our code, it's going to print out the same thing. You might think, oh, well it works the same way then, but actually it's something different is happening here. And I can show you what that is. So Let's go ahead and create another variable. We'll call this num two and set it equal to. Also. Just to show you something quick while we're here, just in case you see it somewhere else, you can do the following. Instead of saying int num two equals five, you could just say int num two, and then in parentheses, you could have five. And this is the exact same. You might also see it in curly brackets, like so. This is also the exact same as if I just said num two equals five. I just want to show you this in case you see it. But personally, I find it less eligible and I don't see it often. So I like to just say my variable equals whatever. Okay? So I'm just gonna go ahead and say num two equals 0 for now. Now down here, if you remember, we had r minus minus and then num one, instead of putting our operator on the other side. And when we ran our code, it did the exact same thing as it did before. Subtracted one from our num one variable, and then we printed it out. But this is actually different. And you may be wondering, how is it any different? Well, down here, if I say num two equals minus, minus num one, and then actually print out num two and we run our code. It's going to look, you've seen just before because I was four. But if we actually go ahead and put this back to the other side, you'll see what the difference is. So if we delete this again and move it over to the other side where we had it originally and run our code. It's actually going to say five. And if we print out num one down here, I'm gonna go ahead and put that on a new line, just so it's a little more readable. Then if I run a code, you can see that that's going to print out for being num one. And then it's going to print out five being num two. Why is that? Well, let me tell you. So num one minus minus will subtract one from num one. Num one will be equal to the result of that. So num one is equal to four, but it actually returns the value before this operation happens. So it's basically the same as if we said num two equals num one. And you can look at it like this if we just highlight this to the end of our num one variable name here, it basically just says num two equals num one. And then afterwards this operation happens and we printed out that result. But if we move that to the other side and then highlight that same range being numb to all the way over to num one. You can see that the operation happens inside of that. And the operation actually does happen first here. So now it's going to say num two equals whatever the result of this operation is, num one will still be subtracted. So let's go ahead and see what that looks like. So if we run our code again, you can see that back here we said num one equal to five, m2 is equal to 0. And then num two was equal to the result of this operation, which was for num one minus one is four. Then when we print out num one, it has also four because we still subtracted one from num one. So num one is now four and M2 is equal to that being for those on the other side. Once again, just remember that we're asking them to, to be equal to num one and then for the operation to happen after, okay? So there you have it. Hopefully you now know how to use the increment and decrement operators. And if you don't fully understand it, that's okay. We'll be using it again later on throughout the course. But really it, The only thing you need to take away from it is that the plus plus operator or the increment operator, is the same as saying num1 equals num one plus one. So this here, this here are the exact same. Same would be if I said minus minus n, minus one. However, if you're using this in an operation, so if I wanted to say num equals num one plus, plus, for example, then num two will be equal to num one. And then this operation will happen normally. Or if I put the plus plus on the other side here, then num two will be equal to the result of this operation. Anyways, that is it for this video, the next video, we're going to be going over a new math operator called the modulus. So thanks for watching, and I'll see you in the next one. 20. Chapter 5 (Operators): Modulus: This video, we're going to be going over at the modulus operator, which is the percentage sign here. And this operator gives you the remainder of the division. So what I mean by that, well, let's go ahead and try it out. So first we'll create a couple of numbers. So say I'm one equals ten. And then int num two equals three. Then we'll go ahead and use this operator. So say num one modulus number two. Just like that. And then we're going to need to assign the result of this to something. So we'll actually create another integer called the zone. So say result equals 0 for right now. And then down here, we'll set the result equal to num one modulus num two. So what is this going to do? Well, it's going to take num one pink tan and divide it by 33, goes into 103 times, and then there's a remainder of one. So if we go ahead and run our program, results should equal one, but we still need to print that out. So go ahead and say CL, result. Gotten run your code. And there you go. That's the remainder of our division. And if we were to divide this and run our program, you can see that we get a result of three. And that's because the result is an integer value, meaning it only holds whole numbers in three, only fully goes into 103 times. So that's why the result is three. And we'll actually be talking about this a little bit more in an upcoming video here. Anyways, that is it for this video. The next video, we're going to be going over some different assignment operators. So thanks for watching, and I'll see you in the next one. 21. Chapter 5 (Operators): Assignment: In this video, we're going to be going over some assignment operators. Now, in the past, if we wanted to add a number to a current variable, we'd have to say something like this. One, for example, equals ten. And then to do our math, we'd have to say num equals num one plus five, for example. And this will give us the result 15. So num one, we're now equal 15 because our assignment operator takes everything on the right side and assigns it to what's on the left. Hence why it's called the assignment operator. Now, something else we can do, which is quicker is the following. We can say num one plus equals. Hi. This is going to do the exact same thing that we have here above is just quicker. So let me go ahead and prove this to you. So we said int num one equals ten, and then num one equals num one plus five, which should be 15. So to see this, we'll go ahead and say C out. And then let's actually go ahead and create a new line as well. Then below that will say num one equals ten, just to set it back to what it was originally. And then down here, we'll print it out again. So I'll say CL one. Again. Go ahead and run a program. So there we go. We printed out 15 twice. Like I said, those here. And this here are the exact same. It's just shorthand. And you can actually replace this plus symbol here with any of our other operators. So I could say minus equals five, for example, I know it printed out, printed out 15 from this first one and then five from down here. It's just a much quicker way of doing it. And just to show you that it even works with the modulus operator. And when a program in the remainder of ten divided by five 0. So anyways, that is it for this video. In the next video, we're gonna be talking about something called the concatenation. So hope to see you there. Thanks for watching and I'll see you next time. 22. Chapter 5 (Operators): Concatenation: In this video, we're going to be talking about string concatenation. Essentially what this says is the ability to take two different strings and combine them into one new string. So let's go ahead and take a look at how to do this. First, let's go ahead and create two different strings to combine together. So we'll say string firstName equals, and then go ahead and put in whatever name you like. And then we're gonna do the same thing for lastName. Then we're gonna go ahead and do one more string for a full name. So we'll say string for me. And we're just going to declare this one instead of initializing it. Just like that. Then let's go ahead and actually concatenate and or combine these strings together. So we're simply going to say full name equals first name, last name. Alright, now if we go ahead and print that out, so could just say CL, full name. By the way, if you're using Visual Studio and you have some kind of spelling error, or just want to rename any variable, you can actually just right-click on it. And then click Rename. And then just go ahead and put in the new name here. Then you can go ahead, hit preview. And this will actually show you all the places that it's going to be replacing the name of that variable. And you can go ahead and scroll through your code down here to see what that will look like and then hit apply. And there you go. Alright, so now if we go ahead and run our program, you can see that it puts both names together. And let's say we wanted to have these separated by a space. Well, we could just put a space up here and our first or last name, or we can just throw it in down here. So we can go ahead and put in an empty space like that, throw in a, another plus sign like that, and there we go. So as you can see it printed out or fullname and even through in our string literal right there. And there you go. That is probably the easiest way to combine two or more strings together. And just remember that this is actually called string concatenation. And there's one more way. I'm going to go ahead and show you. This method is technically faster than the first one. But I don't use it as often just because I personally find this a lot easier to read and because it is easier to combine more than just two things together. But I'll go ahead and show you this anyway. Also just a heads up. We could've done the same thing right up here when we declared our full name. So if we wanted to, we could just copy all of this right here. And then up here, we could have said equals, pasted that in the work as home. And so I'll go ahead and run my code here just so you can see that there you go. No difference whatsoever. So what does this other way that I was referring to? So instead of using the plus sign to combine two or more shrinks together, well, we can do is we'll just go ahead and delete this outside of our first name here, just like that. And then we're just going to say dot append in all lowercase. And then in parentheses, we can actually just throw in the variable or the string that we want to combine to this. And one more quick note for those of you using Visual Studio, this little box that pops up that shows you what this method is looking for. You can actually click on the arrows there and it will show you the different options that you have. So just a heads up on that. Anyways, we'll go ahead and type of lastname here. Like so. And what those is gonna do, it's gonna take our firstName and append the lastName to it. Now, if we go ahead and run our code, you can see that it does the exact same thing. However, like I said, I do personally find this a little less readable and more annoying if I wanted to add more than one thing together, granted, we could just put a space up here and our last name, and that will work. But what if we were getting this information from the user and not typing this directly, it'd be kind of awkward to use that way. So we'll just go ahead and leave it like that. And there you go. There's the two main ways you are going to concatenate or combine strings together in C plus plus. So thanks for watching, and I'll see you in the next one. 23. Chapter 5 (Operators): Casting & Truncating: In this video, we're gonna be talking about casting as well as truncating. Now casting is basically taking one data type and converting it into another. And truncating is basically cutting off the end of something. And I'll show you what I mean by that here in just a second. Okay, so first, let's go ahead and take a look at truncating. So we're just going to create a basic flow, say float num1 equals 5, and then go and type in whatever decimal number you'd like there. And then Dumbo that. We're gonna go ahead and say int num two and set it equal to num one. Also, don't forget to put an F at the end of here, float value here, so that you tell our program here that this is actually a float and not a double. Alright, so now if we go ahead and print out num two, it's actually just going to print out five. And I'll go and show you that now. So as you can see, it just printed out five. And you may be thinking that this number is just getting rounded down, but I'll show you now that isn't the case. So even if I were to put in something like 5.99999 and run our code again. It's still just going to say five. And that's because an integer value cannot hold any precision after a decimal point, right? It just holds whole numbers. And what actually happens in C plus plus as that, it's just going to truncate off everything after the decimal point. Like I said at the beginning of the video, truncating means to just cut something off. So basically any number that you have that you convert over to an integer, and it's just going to keep the whole number and ignore the decimal point and everything passed it entirely. So whatever this number is, no matter what is past the decimal point, it's just going to save the whole number. And this type of casting where we convert one data type being our flow here to another hand. We didn't have to do anything extra. You just wrote this out as we would setting any variable equal to another is called implicit casting. It gets changed automatically without any extra work on our end. Now, I'm gonna go ahead and show you an example of x plus a casting where we tell our program here that we want to change from one data type to another. Specifically, I'll show you how this will come in handy. A lot of times your program is going to go ahead and just implicitly cast something or convert it from one data type to another. And just go ahead and run normally. And it's not going to have any issues doing that. Like I said, if we were to run that, it's going to run just fine. No issues. That's just going to cut off everything past the decimal point because an integer cannot hold a whole number. And even if we were to make a double up here instead of a flow and put in some crazy long number after the decimal point like this, and then change this to a flow. So our num two is now a flow and it's going to change this double here into a float value. And if we run our program, you can see that it just held what it could. And the way it does this conversion is kinda confusing and it's not always going to work out the way it did here where it looks like it just rounded up past a certain point. I wouldn't really recommend doing this unless you don't really care about the extra precision as the way it gets handled, like I said, can be pretty confusing and sometimes you might get a completely different number than what you expected. So just a heads up on that right now, let me go ahead and show you an example of when you would want to explicitly convert something to a different datatype. So let's go ahead and erase all this. We're gonna go ahead and say int num one equals ten. And then int num two equals three. Just like that. Then let's go ahead and create a float called result. Set it equal to num one divided by num, starting ten, dividing it by three. And then down below this, let's go ahead and print out or flight. So let's say C out and run a program. As you can see, it just printed out three, even though ten divided by three is not exactly three. And the reason for this is because both of these here are integer values. The equation is done with integers, and integers cannot hold a whole number. So it just gave us three and then set result here equal to that. But if we want it to get the proper float value of this division, we can actually explicitly convert both of these numbers to a float. And the easiest way to do that, which you will see in a lot of programming languages based on the C language is to be the following. So we're gonna go ahead and put parentheses before our variable here. And then inside of these parentheses, we're simply going to put the data type that we want, this variable here, or the standard type here, WE convert it to. So we'll go ahead and say float just like that. And then here we'll do the same. So now we have to float variables, one being ten and the other being three, and they're gonna be divided. And then the result of that equation will be put into our result variable here, which will then be printed out to the console. So we'll go ahead and run a program. And as you can see now, we've got the proper value. And that's because the equation was done with floats. It was stored in a float value. And our result here was that equal to that. Now, what if we went ahead and removed one of these casts from one data type to another from our equation here, and then run our program again. As you can see, it still worked. And even if we just did it with the other one, like so and run our program. I would work as well. However, I wouldn't do this just to be on the safe side, even though this was a pretty basic equation and it worked out, I would recommend doing it with both numbers. Another example of this coming in handy would be as if we were working with a char datatype. So for example, if we changed our first integer here to char num1 and set it equal to ten because of the A-S-C-I-I table chart, this isn't actually ten. And I can show you that if we were to print this out, if I sub c out num one, just like this and run our program, actually printed out a radical tab, I believe. Let me go ahead and change this to another number. A better example here. As you can see, it printed out z and that's because of that table chart that we talked about when we talked about chars, that 122 isn't actually the value 122. But if we wanted to print out 122 from our char variable here, we could have just converted it down here. So let's say we want to turn this into an int that once again, just putting in parentheses, the data type we want and running our program. And there you go. And now printed out the integer value of our char being 122. Now, C plus plus actually offers a different way to convert from one data type to another explicitly. That is different from many programming languages that are based off the C language. So the way this was done, as if we went ahead and removed our previous cost here and said static all under case, underscore, cast. And then inside of a less than and greater than symbol, put the data type that we want to convert this to. So for example, and then the variable that we want to convert inside of parentheses. Like so if we run our program, you can see that this works just fine. Now if you want to do it this way, you can. Technically, it is a lot safer to do it this way. And it's also more noticeable that we're changing this to a different data type using casting ever. Because I also work with other C programming languages, I prefer to just do it the other way. And I also don't really find the risks of doing it to be that dramatic that I wouldn't want to type all of this out every time when I simply just want to change from one data type to another. So personally, I find this a lot easier. I think it's a lot cleaner and a lot quicker to type. And unless you're doing a whole lot of converting or doing something very specific with it. Which like I said, if you're more curious about that, feel free to look up the differences and why the other type is available on C plus plus in the first place. But for me personally and throughout the rest of this course, anytime we do any type of casting, I'm just going to do it this way because I do find it a lot easier and it is done the same way and other C programming languages. So I like to just work with one way regardless of what language I'm using. So there you go. You now know how to convert from one data type to another explicitly thing like this. And implicitly being like when we took it from a float to an integer, and it was done automatically. We didn't have to put in any extra stuff. Anyways. That's it for this video. Thanks for watching, and I'll see you in the next one. 24. Chapter 6 (Conditionals): Scope: In this video, we're going to be talking about variables, scope of variables. Scope is basically just a section of your code where a variable can be accessed. And generally speaking, there are three main places where a variable can be declared. The first is inside of a function like we've been doing, or inside of a block of code, which is signified by an open and close curly bracket like this. The second one would be inside of the definition of a function parameter and where that would be located. For example, in our main function here is in-between these parentheses. Now we'll actually get into function parameters when we talk about functions and or methods there later on in the course. And the third main place where you will see a variable declared is outside of your function entirely. And this is called a global variable. So I'll go ahead and show you that now and show you the differences between a variable declared inside of a block of code or function here and one declared outside of one. Now real quick variables declared incentive, a block of code or function are generally going to be called a local variable, wants, declare it outside of that, are going to be called a global variable. So let me go ahead and show you that now. So an example of a global variable would be if we said in Louisville, simple and set it equal to five. Now, to show you an example of a local variable, which we've been working with throughout this entire course. We'll just say int local number. This one equal to five as well. Alright, so there you go. This one here is a global variable. This one down here is a local variable. So what are the main differences between these two? Well, because this variable is located outside of any block of code, once again signified by open and close curly brackets, they can be accessed anywhere in our program. Granted, just remember the code is read from top to bottom. So anywhere below this, we could use and access this variable as normal. So for example, they can print it out down here. And if we run our code, you'd see that it works just fine. However, our local variable cannot be accessed outside of the block of code which it was created in or above it, because code is read from top to bottom. Now, currently, we're only working with one function being our main function. But as we get into working with bigger and larger programs, we're going to have multiple functions. So outside of just our main function will also have functions that we've created our self, for example. And this local variable would not be able to be accessed outside of our main function here. That is because for one, it wasn't declared or created anywhere above this. And because it is local and only available within this block of code, as soon as we have any code outside of this block here, it is no longer accessible. In general terms, it might as well just not exist outside of that block that I was created. And now, just to recap, a local variable as one created, basically within a function or within a block of code. And a global variable as one created outside of that. And generally speaking, a gobo variable will be available everywhere in your code. And a local variable will only be available within the block that it was created and that is the easiest way to think about it. One is local to the block that was created in and when as gullible available everywhere. Anyways, if you don't fully understand this concept, don't worry about it. We're gonna be working with it a ton, so you'll get a ton of things on practice and examples. But I just want to make you aware of this concept. Anyways, that's it for this video. The next video, we're going to be finally talking about conditional statements are won't be able to check for different conditions which is going to infinitely expand the possibilities that you have with your code. Anyways, thanks for watching, and I'll see you in the next one. 25. Chapter 6 (Conditionals): Intro To Conditionals: In this video, I'm just going to be giving you a brief introduction to conditional statements. So basically a conditional statement as something that checks to see if the condition is true or false. And only if that condition is true, well, it runs the block of code associated with it. Remember, a block of code is represented usually by an open and closed curly bracket. So for example, if we said is five greater than one? Well, yes, because five is greater than one, this condition would be true and it would print out Hello. However, if we asked if five is less than one, that condition would be false in this code would be skipped entirely. So that is basically what a conditional statement is and how it works. Basically just checks to see if a condition is true. And if it is, it will run the block of code associated with it. Otherwise that block of code is skipped. Now we'll be working with this a ton in the upcoming videos and creating our very own conditional statements. So you're gonna get a ton of hands-on practice. How's these can be used for a ton of different things. For example, let's say you were creating a game. You might want to see if somebody is the first player or if they press the button or completed a certain level or something like that. So hope that was pretty easy to understand. If not, like I said, don't worry about it. You're gonna be getting a ton of practice and more information and the upcoming videos. I just wanted to give you a brief introduction into the concept of conditional statements before we move forward. So thanks for watching, and I'll see you in the next one. 26. Chapter 6 (Conditionals): Relational & Equality Operators: In this video, we're going to be going over conditional operators. Now, conditional operators are there to help you check for different conditions. So let's go ahead and take a look at the six different types. So the first one we have here, which is created by using two equal signs, checks if something is equal to something else. So for example, five equal to five. And it is important to remember that this requires two equals signs because it's just one of them would be the assignment operator that we've been working with in the past videos. So if you want to check if something is equal to another, we'll use a double equal sign like this. Now, the one below this, which uses an exclamation point and then an equal sign checks if something is not equal to something else. And then off to the side of these, I'm gonna go ahead and give you an example of where this operator would create a true condition. So we can say five is equal to five. And then down here, for this not equal to one, we get say, three not equal to five. And this will be true because three is not equal to five. So now it's just an exclamation point followed by an equals sign. And don't worry, we'll be using these in the upcoming videos, sharing and get some hands-on practice with these. I just wanted to give you an introduction to each of the different conditional operators. The next one is the greater than symbol, which I'm sure many of you have seen in the past, which is just the greater than symbol and something like this would be true. So is ten greater than three? Yes, yes, it is. So this would be true. And then we could do the opposite down here. So say three, less than ten. And this one is just the less than symbol that you've likely seen before. Now, this one here is a little different to what you've seen in the past, where you normally would have the line under the greater than symbol. But it's pretty straightforward. You're just going to be the greater than symbol followed by an equal sign. We're just going to check if what is on the left is greater than or equal to what is on the right. And all of these are gonna go from left to right, just so you know. So for example, we can say is ten greater than or equal to five? And that there would be true because ten is greater than or equal to five and thus could even be ten, and that would be true as well. And then down below, we have the less than or equal to, which is just a less than symbol, followed by the equal sign here. And we can have something like five, less than or equal to ten. Now, these are just some very basic examples, but just note that instead of throwing in actual values here, we can actually throw in variables that we've created, which really broadens the possibilities because then we could do things based on user input, for example. So they go, these are all the different conditional operators that you can use when working with conditional statements. The next video, we're going to be going over just a few more operators. And then after that, we're actually going to create our very first conditional statement. So thanks for watching, and I'll see you in the next one. 27. Chapter 6 (Conditionals): Logical Operators: In this video, we're gonna be talking about the three different logical operators and C plus plus. And these are a few more operators that you can use when working with conditional statements. The first one we have is the and operator, which is created by using two n symbols or ampersand signs. And the way this one works, as it will check if the expression on the left and the expression on the right are both true, and if that is true, then this condition as a whole will be true. For example, is five greater than three? Is three less than fine, because both of these are true. This would be true only if both expressions are true. Well, it'd be a true condition. Now, the one below that is the or operator. And this was created with these two radical symbols here. And the way this one works is I will just check if the expression on the left or if the expression on the right is true, if at least one of those is true, the condition as a whole will be true. So for example, we can say, is five greater than three? Well, yes it is, is three greater than five though? The answer is no. Because this one is true. This whole condition is true because we just wanted to know if one or the other was true. And lastly, we have this NOT operator which you saw in the last video with the not equal to operator there, which basically only returns true if the condition here is false. Because five is less than three, which is false. And we're asking, is this not true essentially, then this whole condition would be true. So just to recap that the way this would work here as f, what is in here is false. The condition as a whole would return true because essentially will be getting the opposite back from this NOT operator. And another way you can think about it, as we're basically saying, is this not true? And because of this false, it is not true. So anyways, that is it for this video. The next video, we'll actually be creating our very first conditional statements that you can finally get some hands-on practice with using these different operators and with creating conditional statements. So thanks for watching, and I'll see you in the next one. 28. Chapter 6 (Conditionals): If Statements: In this video, we'll be creating and working with our very first conditional statement being the if statement. And this is the most basic conditional statement that you will see in programming. So let's go ahead and take a look at how this was created. So first, I'm going to go ahead and show you the syntax for how to create an if statement and you can copy those if you want. Otherwise you can just kinda follow along. Okay, so there you go. Basically, we're going to say f and then m parentheses. We're going to put a condition that we want to check to see whether or not it is true. And if it is true, we're going to do everything in our code block. And you may have noticed that this is basically what I showed you when I first introduced conditional statements to you. So let's go ahead and create our first one. Down here. We're gonna go ahead and say if five is greater than three. And then in curly brackets here, we're going to say C out. Five is greater than three. It looks like that. Now, if we run our program, we'll see that five is credited than three will be printed out to the console. And that's because our condition here was true. So basically we're just saying if this condition is true, run this code. And just to show you that this wouldn't work if this condition was false, we'll go ahead and flip this around. So we'll say is five, less than three. And when our code again, and you can see that nothing happens. And that's because everything inside this code block will be skipped entirely. Also, something else you can do, rather than putting an expression in here is just remove this entirely. I can just say if true, and this will actually work just fine. As you can see, our code ran. And you can also do the opposite by saying if false. And there you go. Now I can't really think of a great example of when you would use this in an if statement. But I just want to show you that you can put true or false directly into the condition. And just to show you a quick example of how to use the NOT operator that we talked about in the last video. We'll simply put those false here, parentheses, and then put our NOT operator on the outside. And so we're basically saying if what is in here is not true, which it is because of this false, we run our code. You'll see that our code inside of our code block there will run. And they go. Now, let's go ahead and remove that click. And I'm just going to put true in here just so you know that our code should run. And I'm actually going to remove these curly brackets just to show you something you can do, should you so choose. Now, if we were to run this, this code would run without our curly brackets in there. And if I were to change it to false, like so. And run our code again, this would actually be skipped. And that's because C plus plus actually allows you to remove the curly brackets when you were just putting in one statement like this. Now, personally, I wouldn't recommend this. I think it's a very messy way to write code, but I didn't want to show you that it is possible in case you were to ever see it in the future. Okay, So let's go ahead and put our curly brackets back and put our code up back inside. Anyways, there you go. You now know how to create an if statement became the most basic conditional statement you can create. Real quick just to show you that this does work with variables. We'll create a couple here. So we can say one equals five. And boy, that was number two equals three. Then down here, we can simply say is num one greater than two? And because it is, our code will run. Now, another thing I want to show you is that you could just put those on the same line like I did up here. So this, as you can see, that will work just fine. And a lot of times I will do this if I have something very small to run inside of my if statement, But normally speaking, I do find it a lot cleaner to have my curly brackets on separate lines. Also, for those of you on Visual Studio, you can actually just type it and then tap tab and it will automatically fill it out for you. So that's a pretty handy way to immediately create an if statement. In there are other things like that that you can have created automatically and Visual Studio. But during this course, I would recommend that you just typed this out in order to get the hands-on practice. Now, another thing I wanted to show all of you, because you can actually put a lot larger expressions in here. So for example, we could say num one greater than num two. And we'll just say ten greater than 35, greater than one. And because all of these are true, this should run, run a code. You can see that it ran just fine. But if I were to change this to Secretary sample on the end here and run our code again. Because all of those were not true. This does not run. You can mix and match these with AND, or, OR signs and makes us expression as long as you want. But the longer and bigger and more complicated you make this, I'd recommend using parentheses, just like you would with mathematical expressions to define your order of operations. So for example, I might want to put these two here instead of parentheses. So this was checked first and then check this after. Or I can even put the or sign in here and then say as ten greater than 35 is greater than six. And because this would be false, then this would have to be true because I would want to know if either this or everything inside of here was true. So because this one will show here, even though this one is false, or code would run. Because we are checking if this is true or if this is true. Also notice that if you don't put anything inside of your parenthesis here, you will get an error. And it will ask you to put in some kind of expression. Anyways, just to quickly recap, what I want you guys to take away from this video is to create an if statement. You're just going to say if in all lowercase and then in parentheses, you'll put a condition that you want to check whether or not it is true. Then inside of curly brackets, you're waiting to put all the code that you want to run if that condition is met. So there you have it. That's it for this video. Thanks for watching, and I'll see you in the next one. 29. Chapter 6 (Conditionals): Nesting If Statements: In this video, we're going to be talking about nesting if-statements. Now, what do I mean by that? Well, basically it's just one to one if statement nests or sets inside of another. So let's go ahead and take a look at what that would look like. So first, we'll go ahead and create a basic if statement like so. And then we'll create another if statement inside of the code block for that if statement. Just like this. And then down here, we're going to go ahead and change this one to false, like so. And then here we'll go ahead and say C out. And we'll print out second statement like that. Then let's actually go ahead and copy that whole line of code and put that inside of our first if statement as well. Just like this, we'll change this one to say first. So basically how this first if statement is going to work is just as normal as going to check. Is this condition true? And if it is, everything inside this code block will run. And if not, it's just going to go ahead and skip it entirely. Then this second if statement is also going to check if the condition inside of here is true. And if it is, it is only going to run the code inside of its code block being right here. If it is false, it's called block will be skipped entirely. And then the rest of the code from the first step statement will run as normal. Because if you're a member, code is read from top to bottom. So as I could as being red, it's going to check if this condition is true. If it is great, it's going to go ahead and start reading off this code. Then it's going to check if this condition is true. If it is L, read all the code inside of there, and then continue on. If it is not true, this code block will just be skipped and then it would continue on as normal. So if we ran our code right now, it's just going to print out first if statement. And if the code inside of here was also true. So if we just change that to true for now, run, our code is going to say second if statement and then first if statement. And the reason that it said second if statement first is because my code is read from top to bottom. So even though this first if statement was true, it still had to read whether the second one was true first and do everything inside of it first if that was true before continuing on down here to show you that that is the case. If I were to cut this code and put it above this first if statement like that and run our code again. You can see that it now did it the other way around. So there you have it. You now know how to nest if statements. And you can do this as many times as you would like. Now, before we end this video, Let's also go back to a previous topic, which was when we talked about local and global variables. Remember how I said a local variable is only available within its scope or within the code block of which it was created. So if we create a global variable up here, let's call this a low bone and set it equal to five. So because we have this global variable here, it is available everywhere in your code below it. So what that means is, instead of our first if statement, we can say first if statement. And then after that, we can print out global. Let's go ahead and create a new line. And then even in our second if statement, we also have access to global. This will work as well. So if we run our code, it's going to say first if statement five being our global variable. Second if statement. And also just to show you that this is possible as well. And our global variable, we could declare it out here and then initialize it down here. Remember, you want to make sure that you give a variable an initial value before using it. So before we print that out here, want to say global, then give it an initial value, three. And this will work just so first if statement three. Second if statement three. But now let's go ahead and take a look at some local variables. So if we get rid of that, first off, you'll notice that it no longer knows what global is. Just remove that they're somehow, if we were to create a variable inside of our main function, it would also be available within both of our if statements. And that's because e.g. if we said num one here, and it looks up that equal to five, e.g. this will now be available everywhere below it within its code block. So all the way down until this closed curly bracket here for that method, in this case is our main method. So just to summarize, our local variable is going to be available everywhere below it because code is read from top to bottom within its scope being this entire method here. But what if we were to create a local variable within one of these if statements? Well, it's only gonna be available within its block. So if we said num one equals five, outside of that code block being down here, because I code block runs from here to here, we can no longer access it. It might as well just not exist. So num one equals ten not work because we don't know what num one is. It is undefined. This variable doesn't exist because it's scope is right here. However, it would exist in our second if statement. So right here, we could still print out num one because it is within the scope. So there you have it. I just wanted to give you a better example of what I was talking about when we talked about local and global variables. So just remember, a global variable is one created outside of the code block in a local variable is one created within. A global variable will be available everywhere after it's created, a local variable will only be available everywhere after it's created up until the end of its code block. So there you have it. Hopefully that made sense. If not, go ahead and play around with local and global variables a bit just to see if the scope of its availability. Anyways, that is it for this video, the next video, we'll be taking a look at another type of conditional statement known as the if else statement. So thanks for watching, and I'll see you in the next one. 30. Chapter 6 (Conditionals): If-Else Statements: In this video, we're going to be taking a look at the if else statement. So let me go ahead and show you the syntax for the statement or the rules for how this statement is created. So just like before, you're going to create a basic IF statement where you're going to say f and then parentheses. You'll put the condition that you want to check. If it is true, then your code block after it. However, right after that, you're going to go ahead and put in the keyword else, which is also an all lowercase, just like F here. And then right after you say else, you're going to put just another code block with no condition. So basically we're saying if this condition here is true, we wanna do this just like normal else, or you can think of this as otherwise do this. So let's go ahead and see how this works. So down here, we'll create a basic if statement. So we'll say if, let's say five is greater than three, then after that code block we're going to say else and notice and Visual Studio here, how it automatically highlights both of these to show me those are connected. You can also just hit tab and it will automatically create the rest of this for you. Otherwise, you're basically just going to put another open and a closed curly bracket like this. So we've now created our FL statement, but let's go ahead and take a better look at how this works. So here, let's go ahead and see how printout condition was true. Then in our else statement or else code block here, let's go ahead and say CL condition. And the reason I had us write thes specifically is such a drive home how this is working. So basically, we're going to check this condition as normal. But if this condition is false, this code block here will run in a fixed condition is true. This code here will run. So basically you want to use an if else statement when you want to run something only if the condition was true, but you also want to run something if the condition is false. And so now if we go ahead and run our code here, you're going to see that it's going to say condition was true because five is greater than three. But if we go ahead and turn this around now, I will say it is five, less than three. And when they're cut again, it's going to say condition was false. So everything on your F code block will run if the condition is true and everything else. Code block will run if the condition is false. I know we've only been putting one statement or just one piece of code inside of these code blocks. But this can be as big as you would like. Right now, some of you may be wondering, why don't we just take this and put it down here? Well, we might not have wanted to say our condition was false if it wasn't, right. So this just allows us to have a specific block of code will only run if that is true in a specific block of code that will only run if it is false. So there you have it. You now know how to create an if else statement. The next video, we're gonna be looking at something called an else-if statements. So as always, thanks for watching and I'll see you in the next one. 31. Chapter 6 (Conditionals): Else-If Statements: In this video, we're gonna be taking a look at creating an else statement where we can basically check if a condition is true, then we can check if another or multiple other conditions are true, then we can still have something run if none of those conditions were met. So let's go ahead and take a look at how this works. So the basic syntax for this is we create an if statement just as we had in the past. And if that condition is true, this code here will run and then everything else would be skipped down to the end. But we can throw this out of here to see if another condition is true in case the first one wasn't, then if that is true, that only this code will run and the rest will be skipped. And then we can still have an else statement where we can say if none of that stuff was true, we would still want to do this. And there you go. That is the basic syntax for an else statement. So let's go ahead and create one of our very young. And just to have some fun with it, Let's go ahead and create a simple program for this as well. So we're going to say num one. And then below that, we're going to say int num two. Then let's go ahead and actually get some user input for these. So we'll say sent them blow that will say sin. Then at the end of all of that will create our else statement. So say if numb one is greater than two, then we want to do the following. So say C out one. And then in quotation marks here, we'll say is greater than. Then we'll also add in. To make that. We'll go ahead and underline. And I'm going to throw in a couple of spaces here, just so that'll look a lot cleaner when it prints out. Then down below that, we'll say else. And once again, this is all lowercase less than num two. Then we want to print out the opposite. So say CL num one, num two. And then below that we'll say else. And then remember, we don't put a condition on, we're just saying else. Want to say one is equal to two m? Just like that. Basically, we're gonna take two and pets and take an input for them one and then put for number two. And then we're going to say if numb one is greater than num two, then we're just going to print out num one is greater than num two. Else-if or otherwise, if num one is less than num two, then we want to print out num one is less than num two. Otherwise, if neither of those are true, well, then they must be equal. So we said else num one is equal to num two. And then if we want to clean this up, so it looks a little better to the user or us in this example up here, we can go ahead and do the following. We can say C out number, colon space like that. And then we'll just copy and paste that down below our cin num one statement like so we'll say type a, another number. And then I'll go ahead and zoom out quick so you guys can take a full look at the code, make sure yours matches mine. Then we'll go ahead and run our code to make sure it's working. And make sure you run it without debugging or add in a bunch of these sin dot getline. So go ahead and enter a number. Then for your second number, go ahead and enter a smaller number than the first one. And I should say that our first number was greater than our second number. And then we can go ahead and run our program again. But this time let's type in a smaller number for our first one. And there you have it set. Our first number was less than a second. And if we run it again, we can type in two of the same exact number. And it should say that they are equal. And there you go, everything is working. And just a quick note before we wrap up the video, I do want to let you know that you can have multiple else. If conditions here inside of your statement, you could say Alice num one is equal to num two for example, and then have a code block for that as well. And that will work just fine. Now, obviously in this example, we might as well just use else. But you get my point. You can have as many else if statements or conditions I should say as you would like. Also, you do not even need this else statement at the bottom. This is just something you can have, but you can get rid of it entirely in your code will still run as normal. So if I run my program, you can see that it's up and running, no issues. So when would you want to use an else if statement? Well, anytime you want to check for more than one condition and they're all related like they are here. You can use an else statement. You can have as many of these as you want, like I said. And then you can still even throw on your else statement to have a piece of code run if everything was false. And if I go back here to what we had, just remember that as soon as one of these conditions are met, everything else that as part of this else if statement here is going to be skipped. If this condition is true, it's just going to read what's in this code block and then skip all the way down to the bottom here. If that first one is false and then the second one is true, then it will just read what's in that code block and skip everything else. And then if nothing was true, then our else statement will run if we have one. So just be wary of that, but it does read from top to bottom and then only the first statement that comes true is going to matter. So even if and else-if statement here, we turn this around and we said is greater than or equal to. And then run our code, num one, I put in ten. For number two, I put in five, for example. And there's only going to print out pen is greater than five and less because as soon as one of these conditions is true, it doesn't care about the rest regardless of how many else-if statements you have. So just be wary of that and makes sure that if you are wanting to check for a condition first, that you have that one at the top. There you have it. Hopefully you have a good understanding now of how if else-if and if else statements work. And if not, I highly recommend you go back and re-watch the video that you are having trouble on and maybe play around with it a bit more just to get a better understanding of how it all works. Now that being said, you will see these more throughout the course. So you will get some more natural hands on practice. Anyways, that's it for this video. Thanks for watching, and I'll see you in the next one. 32. Chapter 6 (Conditionals): Switch Statements: In this video, we are going to be taking a look at in creating what is called a switch statement. And what this does is basically take an integer value and then run a block of code based on what that value is. So right here, I have the basic syntax of that for you. So basically we're going to say switch and all lowercase. And then in parentheses, we are going to put our integer value. And this could even be the name of a variable that is an integer value. Then we're going to have open and close curly brackets. And inside of that, we're going to say case, all lowercase, followed by the number. We want to check if that's what this number is. So we can say in case this number is one, we want to do this. After that, we're going to say break with a semicolon to break off that section of code. And then down here we can say case two, we want to do this and then we break that off as well. And we can keep doing this on and on and on. And then we can also have a default, or we just say default in all lowercase followed by a colon. Then we could do this. Then we can have a break statement at the end of that as well, although this one is not necessary. Then closing curly bracket, and you can just have one case statement here, or two or three, or like I said, as many as you want. I just have a two here to show you that we can have more than one. So hopefully that wasn't too confusing for you. If I was, We're gonna go ahead and create one from scratch. You can get a better idea of how this thing works. Now, real quick, some of you may have already made the connection that this looks very similar to our else statement. Well, that's because it is the only real differences here are that for one, this takes an integer value. Rather than being able to put in a fault condition our expression that we want to check to see if it is true, then we just do something based on what that number is. So you can kinda look at this as saying f, putting in our number and then something else, Ceph the numbers, this number is this, and then our else statement. But I just want to let you know that a switch statement is faster. And a lot of cases, if you are just trying to see what a number is, this is gonna be a lot more readable. So let's go ahead and create one of our very own. So down here, we're gonna go ahead and say int response equals 0. Then below that, let's go ahead and say CEO will say, please choose an option. And I will say colon space. And then we'll put in an inline there as well. Then below that will create another c out statement. We'll say type one for Main Menu button and an endline here, or create a new line. I'm just going to say backslash n. For this one, we'll do the same thing below, say tape to four options. Just like that. And I'll create a new layer again and below this will actually create our switch. But before we do that, let's go ahead and accept the response from the user. So we'll say correspondence. Then the creditor switch, if you remember, we're going to say switch and all lowercase. And then in parentheses, we will put in an integer or our variable, in this case being response. And then after that, I'm going to have open and closed curly brackets. And then in-between those curly brackets, we'll create our cases. Or what we want to check to see if our response or our integer here is equal to. So for example, we'll say case all lowercase, by the way, space one for our value that we want to check, and then a colon. Then after that, we'll say CL main menu. Then, well that we'll go ahead and say break all lowercase with a semicolon at the end. After that, we'll create another case for two, and we'll say options. So say case. Coleman SEO options. By the way, for those of you using Visual Studio, you may have noticed that will automatically put in a second quotation marks for you. However, if you do end up typing another quote on your own, it will just skip over that. It's not going to cause any problems. It's not going to throw in a second one and you can continue on with typing. But it is nice that it creates shift for you in case you forget. Then we'll go ahead and put a semicolon at the end and we'll say break again. And actually I'm gonna go ahead and put our c out here on a separate line. Let's kill. I think it looks a little bit cleaner. Then down here, if we wanted, we could keep throwing in more cases to check for. Or we can even throw in a default if we want it as well. And let's go ahead and do that just for fun. So say default. Then we'll, that, we'll say CL will say option is actually it will go ahead and say your choice. Like I said, you could throw in another breakdown here. If you so choose, some of you might find that it looks more symmetrical or what have you, but it does not necessarily, essentially, the sprayC just breaks you out of this section of code since it isn't held them by curly brackets, like we've seen in the past. And also, even though we just have one line of code and nice, and you can have as many as you like, just like any other block of code. Now I'm going to zoom out here quick, so you guys can make sure that you have this all copied down. And also grow fast for those of you wondering what these little minus blocks were. For those of you using Visual Studio and many other programs, how this as well, you can actually click on that and it will actually collapse everything inside of whatever that is. This is on If Statements, it's on methods and other things like that, but it will still leave what it is for you. And then it will even have the curly brackets were three dots here. And if you mouse over that, you can see what code is being minimized right now. Now, the code will still work. The code will still run. It's just minimize basically. I mean, you can simply click on that plus button now and minimize that for you. So if maybe you're working with a lot of code at some point in time and the screen suddenly they look a bit messy. You can just close that up once you're done working with something or if you're trying to work on something else. Anyway, I just thought I'd show you that quick. Anyways. Now, if I go ahead and run our code, you can see that. So let's choose an option, type one for me, many of type two options. So let's go ahead and type one just to see what works. There you go. It says Main Menu. And I'm actually going to throw in a new line on both of these just to clean that up a bit, or all of them, I should say like that. And run our code again. If we take two and it'll say options. And if we run it one more time here and just type in any whole number outside of what we've made a case for and hit Enter. You can see it'll say your choices and valid because none of our cases were true. So as you can see, it works very similar to an else if statement, except inside of these parentheses, you will need an integer value. And if we wanted, we could have actually just typed in a number like this and run our code. And it's still waiting for us to type in something for response. So you can go ahead and do that. But regardless of what we type in, it's going to say main menu, because what was on our switch was one. And there you go. Now, obviously there's not really a great reason you would do this. Normally, you're just going to throw in your variable here and take some input as a switch is going to usually be there to check for multiple options, right? So you're going to take something that can vary. Now I did want to warn you, especially for those of you that may be coming from another programming language if you remove one of these break statements. So in our case one here, if I went ahead and removed that, it's actually going to run all this code until it gets to another break statement regardless of whether our response was two. And if we go ahead and run our code here, you can see that that is true. So if I type one, for example and hit Enter, it's going to save Main Menu and options even though we typed in one. That's because without that break, it actually does something called fall-through, where it's just going to run through and run all the code inside your switch until it hits a break statement or at the end of the switch. And if I took out our other break statement here, you can see that it would even print out your choices and valid if we typed one. However, this fallthrough will only begin from where it was true. So if I type into, if I run a program here again, I should say Options and then your choices and balance. So there you go. Now, there might be cases where you want this fall through. Maybe you come up with some neat idea where that will be handy. And C plus plus is one of the few languages that I know of that will allow you to do that, at least by default. But normally, they're probably not going to want that to happen. So you want to make sure that you throw in the break after each of these outside of the default one sense it will automatically stop once it hits this closed curly bracket anyways. And lastly, I'm just going to show you what this would look like. Had this been an else statement, just to show you the similarities here. So we could have said if response is equal to one, and then put it in our code here outside of the break. And then below that, we're going to set the font is equal to two. Then below that, we can just say else and put this in. So hopefully you can see the similarities here. Like I said, they basically do work the same way. However, this allows us to put it in a much larger condition. We want to check if it is true or this has just put in an integer value. And personally, if this is what you're doing is just checking an integer value and wanting to see what it is equal to. I believe that this looks a lot cleaner and it also runs faster. So there you go. You now know how to create a switch statement. Anyways, that is it for this video. Thanks for watching, and I'll see you in the next one. 33. Chapter 6 (Conditionals): Ternary Operator: In this video, we are going to be taking a look at an working with something called the ternary operator. And you may also see this referred to as a conditional operator. So the syntax for a ternary operator is as such, you're going to have a condition that you want to check if it is true or false. And then that's going to be followed by a question mark. After that, you're going to have an expression or a piece of code that you want to run. If that condition was true, followed by a colon. And then a second expression or a piece of code you want to run if that condition is false. And lastly, you're going to close this off with a semicolon. Now, down below here, I already have an example for you just to show you how this works. And don't worry, we're going to be creating one of our own. I just wanted to show you how this might look in actual writing or code. So my condition, this is one greater than five, followed by a question mark and then my first expression, or a piece of code here. So in this case, I said C out hello, then I had my colon. And then the second expression or piece of code that I want it to run if the condition was false. So this C out. So because this one is not greater than five, this piece of code will run. If I run my program here. You can see that it says buy. But if we turn this around here and ask Is one less than five? Because this is true. Then our first piece of code here, which is on the left side of our colon, will run the code again. You can see that it says Hello. Also, I want you to notice that I did not put a semicolon after my double-quotes. And that's because this is the end of the statement, not this. Alright, so let's go ahead and create one of our very young. So I'll go ahead and remove that. And then we're going to say one. And below that, we'll say CN number, right? So we'll take a number that we type. And then let's go ahead and credit ternary operator. So we're going to say num one greater than 0. And this will be our condition. After that, we'll put in our question mark, and then we'll follow this up with an expression or a piece of code that we want to run if this condition is true. So we'll say C out, not one. Then in quotes here, let's go ahead and say is a positive number. Then we'll follow that up with a colon. Then after that, we'll say C out again. Num one. And then this time we'll say is a negative. Notice that I put a space before is on both of these because there's not gonna be one by default after our num one is printed out or are integer there. Now, if we run the code, I could say something like five. Let's say five is a positive number. Okay? But if we run this again, and I put negative five and center, they'll say negative five is a negative number. And that's because we said this num one greater than 0. Then if it is, we said we wanted to print out num one is a positive number. And if it wasn't greater than 0, we wanted to print out num one is a negative number, right? There you go. That is the basic way to use a ternary operator. And a lot of times you'll basically see this used instead of an if else statement. So let's go ahead and create what this would look like as an FL statement. We could have simply said if one is greater than 0. And then inside of there printed out num one is a positive number. So I'm just going to copy and paste this from above. And below that we'll say else and paste this in. And we'll have to make sure to put a semicolon at the end of both of our C out statements. And there you go. This is how we could've done the same thing with an if else statement. But there are a few more ways that we can use this. So let's go ahead and take a look at that. And I'm going to go ahead and remove our if-else statement. Now, let's go ahead and take a look at another way we can use the ternary operator. So down here, let's go ahead and say string result. And we'll set this equal to a ternary operator. So right here we're going to say num one greater than 0, followed by a question mark. And then depending on what that is, we want to actually just set our string equal two is a positive number or a negative number, kinda like we did before. It's right here. Then follow it up with a colon, and then put in this other string here. Then our semicolon at the very end. Now we created a string called result and are setting it equal to the result of this ternary operator, this whole operation here. So it's going to say as num one greater than 0. If it is, our string will equal this. And if it is not, then our string will equal this. And now down here, let's go ahead and print that out. So we'll say C out num one. Then after that, I print out the result, will create a new line like that. Then let's go ahead and remove our previous ternary operation there and run our code. Now, you can see that it pretty much works the same way as it did before, with the only difference being that we actually used this ternary operator here to set something equal to a result based on this here. And let's go ahead and put this in an if else statement again. So say if numb one is greater than 0, then we want to set a result equal to this here. Else Alice will set result equal two is a negative number. Like so. Now you can use whatever you want, whatever you find preferable with your current situation in your programming. I just wanted to show you the different ways that you can use this. And there's actually one last way that we're going to go over, which is that you can actually nest ternary operations. So everything outside of our integer declaration and our sin line, Let's go ahead and get rid of. Then. Let's go ahead and create a string and call it resolved. And we're not going to give this a value right now. So down here, we're going to go ahead and see the result. Set this equal to. Let's go ahead and put in the condition for ternary operator. So in parentheses, we're actually going to say num one is equal to 0. Then we're going to follow that up with a question mark in quotes there, just like that. Then we'll put in a colon and then another ternary operation. So go ahead and put an open parenthesis. And we'll say num one greater than 0, followed by a closed parenthesis. Then we'll put it in another question mark. And we'll say positive quotes there. After that, we'll put in a colon followed by negative in quotes there. And then we're going to put in a close parentheses and a semicolon. And then back here where we said num one greater than 0, we're going to put in an open parentheses. Alright, so let's go ahead and break this down. Personally, I think it looks confusing, but if you do see this in somebody's code, I want you to be able to read this and know how this works. So real quick, just to mention this. Yes, we could have just put this whole thing right here. I just didn't want to add any more to this already crazy in line of stuff. Granitic just want to set string. Anyways, let's go ahead and go over this. So we set a result equals, and then we wanted to set it equal to whatever the result of this whole crazy thing was. We said, is num one equal to 0. If it was, then we want to say the result equals 0. Okay, cool. So if num one is equal to 0, we're going to say 0. But if it is not equal to 0, then we're going to do another ternary operation over here. So I is num one greater than 0. So we want to set result equal to positive and f naught was equal to negative. And then we throw in all these parentheses just to make sure that the order of operations was proper. So he said as num one equal to 0 if yes, then we said 0. If not, then we wanted to set result equal to whatever was in these parentheses we wanted to do with this whole operation. First, we said num one greater than 0 for this condition, if yes, then we'll start at the positive. If not, we'll set it to negative. And so we just threw that in-between its own set of parentheses to make sure that the result was equal to the full result of this operation, right? So there you have it. You now know how to use the ternary operator. Now, me personally, I don't use this a whole lot, but I have seen it in a ton of other programmers code. So I did want to make sure to throw in this course so that you guys knew what it was, what it meant and how it works. And who knows. You might personally really like it and want to use it for yourself. Anyways, that is it for this video. Thanks for watching, and I'll see you in the next one. 34. Chapter 7 (Loops): Intro To Loops: In this video, I'm just going to be giving you a brief introduction to loops and programming. In the upcoming videos, we'll be creating different types of leaps of our very own. So right here, I have the basic syntax or a rule for how a loop works. Basically you're going to have a condition, and this condition works the same way as it does in an if statement. So essentially, if this condition is true, we're going to run a certain block of code. And then after that block of code has run, it's going to recheck if that condition is true. And if it is, we're going to do this again. And it's just going to keep doing this over and over and over and over until our condition is false. And then once that condition is false, it will break out of the loop and continue on reading code. In there, you have it. Basically, we're going to have the condition, and that condition is true. We're going to run some code and will continue to run that code until the condition is false. So hopefully that made sense. If not, you're going to see multiple examples of this in the upcoming videos. So as always, thanks for watching, and I'll see you in the next one. 35. Chapter 7 (Loops): While Loops: In this video, we're going to be talking about the while loop, which is probably the most basic loop that you will see. I have the syntax for that right here. So what you're gonna do is you're going to write well and all lowercase. And then in parentheses, you're going to put a condition that you want to check to see if it is true, then wow, that condition is true. You're gonna do everything that you have within your code block or your curly brackets. Following that, it is going to continue to run this block of code until this condition is false. So let's go ahead and write one of our very own. And I'm also going to talk about some of the issues you may run into if you're not careful. So for right now, let's go ahead and create an integer. I'm going to call this iterator and set it equal to zero. Then, well that will create our while loop. So we'll say, wow. And then in parenthesis, we'll say iterator is less than 100. And then pull that in curly brackets will create a block of code that we want to run. So let's go ahead and say something like CL iterator. Then in line. Now, don't get ahead of us here and run this block of code as it may crash your program, if not your computer and Jenna, and I'll tell you why. So we have int iterator equals zero. And then we said, well, our iterator is less than 100. We want to print that out. But this condition will never be false, right? So it's going to run through, it's going to print out iterator. And then it's going to say, alright, let's check our condition again. The condition is still true. Let's do this again and again and again and again and again. So what we need to do is make a way or our condition to be false. Because if you do not do this, this code will run infinitely. So down below here, we're gonna go ahead and say it, iterator plus, plus. I'll put a semicolon at the end. So now our iterator is going to increase by one every time this block of code runs, then it will check the condition and it'll keep doing this until iterator is equal to 100 because then it will no longer be less than 100. So go ahead and run the code. As you can see, it immediately printed out different lines of code starting from zero all the way to 99. And if we wanted to print out one through 100, e.g. we could just change our iterator appear to one and make this say less than or equal to 100. We ran this again, is going to print out one all the way through 100. Now, one thing to note is that it will not check for the condition again until the end of the block of code. So it's gonna run everything inside your block every time that this condition is true, then check the condition again. And only once that condition is false, what break out of there and continue reading your code. So if e.g. down here, we said C out, done, thrown an N line there and run our code again. You can see that as soon as it reaches 100, it prints out done. Now, another thing I want to make you aware of, which does work with if statements as well as that, because one is the same as true, because it is not as hero. Essentially, putting one here is the same as saying true. And putting the zero here is the same as saying false. So zero is the same as false, and one is the same as true. Every, you would never want to put while true here. Because although this as possible, you will not be able to get this to say false. So there you go. I know how to create and use a while loop. And yes, you can. You help put loops and if statements or whatever code you want within that. So just to recap, to create a while loop, you're just going to say, well, you're going to put a condition that can eventually become false and will become false within the context of your loop. So before we had iterator less than or equal to 100, and then we had a way for that condition to become false if it wasn't already when we first came across the world. Because if this one is false, this would have been skipped in the first place. So you're going to say, well, in parentheses, you'll have a condition that can become false within the context of your loop if it is not false already. And finally, you'll have your code block. What we did here, where we had an iterator to run through each time. Our loop brand is a very common way of breaking out of a loop at some point in time. Now, before we wrap up, let's go ahead and look at another use case for this. So let's go to our middle of this and we'll say int response. Set this equal to one. And then Dumbo, that will say, wow, in parenthesis will send response is not equal to zero. And then in our curly brackets, say C out. Please take zero and employ that will say See in response. Now we credit an ink called response and this n equal to one. And then we said one response is not equal to zero, which it definitely won't be the first time around. We're gonna print out, please type zero and hit Enter. And if the user does not type zero, this code will run again. However, once they did type zero, it'll just say done. So if we run a program, it's going to say Please take zero and hit Enter. And we can take any number and hit Enter. And it'll ask us to do again. We could do this forever if we want it. Otherwise, if we type zero and hit Enter, it'll say done, and there we go. So that's just another example of how we could have used our while loop and had a way to break out of it without using an iterator. Now, real quick, I do want to make something very clear. If we were up here, set response equal to zero, e.g. then in our while loop condition, said response equals one, this would actually run forever. And the reason for this is because we would actually be assigning one to response. So we say response equals one. And because of this, the while loop would actually look at this as just this. And as we know, one is the same as saying true. So this might as well say, Well, true. Even though we meant the check whether response was equal to one. So anytime you are working with a condition, makes sure that you never just use the assignment operator. And you always say is equal to by putting in two equal signs, as you definitely do not want to crash your program, especially if this is a piece of code that you've been working on for a long time and haven't saved or something like that. It can be pretty traumatic. So just wanted to give you a heads up on that. So hopefully now you know how to write and use a while loop and can even think of some examples and your head of where this would be useful. Anyways, what I actually want you guys to do is go ahead and save what you have here. So we're gonna be using it in the next video when we talk about a do while loop. So as always, thanks for watching, and I'll see you in the next one. 36. Chapter 7 (Loops): Do While Loops: In this video, we're going to be talking about something called a do while loop. And if you remember in the last video, I asked you to save everything you had. However, if whatever reason you didn't do that, no worries. Just go ahead and pause the video. Copy this little bit of code here, and you should be all good to go. So I have the basic syntax for a do-while loop right here. Worth the rule for how to write one. You're going to do in all lowercase. And then in curly brackets, you're going to have your block of code that you want to run at least one time. And there's just going to run once, no matter what. After that, you're going to say, well, just like we have in the past, and then parenthesis, you'll have a condition. If that condition is true, your block of code will run again and again and again until the condition is false, then you also need a semicolon after that condition. So the main difference between a do while loop, a while loop other than how it is written as your block of code will run one time no matter what. And then I will only repeat while the condition is true. Alright, so let's go ahead and take a look at how to create this and make one for ourselves. So down here, we're going to change our while loop into a do-while loop just to kinda show you how comparable they really are. So right here, we're going to say do in all lowercase. And then we're gonna go ahead and cut our code block here from our previous while loop and place it in front of the word do. And then after a while loop condition, we'll just put a semicolon just like that. And there you go. We've now created a do while loop. So what it's gonna do now is say Please type 0 and hit Enter. And then I'll let the user put in a response being us in this case. And then while response is not equal to 0, but they go ahead and change that there is going to keep repeating. And then once we are done and it's going to say, Let's go ahead and change that to something like thanks. Now if we run our code, it's going to say Please type here and hit enter as for response. And as long as that response is not 0, it's going to keep repeating. What's a is 0, it will say, thanks. So if we run our code here, you can see that it says please type 0 and we can type whatever we want and keep hitting Enter and over and over and over and over until we actually type 0. Then we enter, it will say, thanks, and there we go. So notice that this code ran even though response was equal to 0 in this condition was false because the response was 0. This would have only been true and it was not equal to 0. So like I said, the only difference between a while loop and a do while loop is the syntax or how you're going to write it there, and the fact that your code will run one time. So regardless of what response was before, this code is going to run once. And then it's going to check to make sure the response is 0 before breaking out of this code. Because we said if response is not equal to 0, we wanted to keep running this code. And there you have it. You now know how to create a do while loop. Anyways, that is it for this video. Thanks for watching, and I'll see you on the next one. 37. Chapter 7 (Loops): For Loops: In this video, we'll be looking at creating and working with something called a for loop. I have the basic syntax for one for you right here. So first off, you're going to save for in all lowercase and then in parentheses, we're gonna have a few different things. First off, going to have a variable initialization. And don't worry, I'll be showing you what I mean by that here in a little bit. Then you're going to follow that up with a semicolon. And then after that you're going to have a condition. Let's condition works just like it does in any other loop. So while the condition is true, your code is going to continue to repeat. And then after that, you're going to have a variable update, which is where you'll be updating the variable that you create them. So let me go ahead and show you what that looks like real quick. I did want to let you know that all of these things could technically be removed from a for-loop without any errors as long as you leave the two semi-colons in there. However, if you do not have the condition that goes in the middle there, this will always be considered true. And you would have an infinite loop, which like I said in the past, you normally always want to avoid. So let's go ahead and create a for loop around. And women create a for loop. We're actually going to be trying to get the same result that we did with our while loop in a previous video, where we printed out one through 100 on separate lines. Just so I can show you if the differences between a for loop and a while loop. So just shut off, we're going to say four. And then in parentheses, we're going to say int I equals 0, the semicolon at the end. And there you have it. Right there we have our variable initialization roof created and initialized a variable. And you can name this whatever you want. You can also set this equal to whatever you want, but a lot of times setting it equal to 0 and seeing it called I is the most common being short for iterator. And you'll see why in just a moment. Next, we'll have our condition. So we're going to say I less than or equal to a 100. I don't put a semicolon after that. And then right after that, we're going to have our variable update wherever you're going to update the value of our variable. So let's say I plus, plus. And then we'll have our code block inside of here. We're going to simply print out, I will say C0. And then we'll create a new line like so. And this loop is good to go as is. So let's go ahead and summarize what's going on here. So we said for, to declare that we are creating a for loop. And then in parentheses, we have all of the things that are basic for loop will have, We have heard of variable where we said int I equals 0. Then we put a semicolon to separate this from the condition, let me say I less than or equal to 100. This is the condition that needs to be met for a for loop to run in the first place. And if it is going to continue running, this must continue to be true. And then after each time our code has run through, it is going to increase I by one. And we could have also just said I plus equals one or I equals I plus one. It really doesn't matter. This is just the space that you want to have for updating your variable. So a lot of times this is how you're going to see a for-loop graded I equals 0, I with a condition and then I plus, plus or plus plus I. Like I said, I is short for iterator, but you can call this whatever you want it. And then instead of for loop, we just went ahead and print it out. I then created a new line. And what this is going to do is print out 0 through 100. But if we change this to one here, I'm just going to print out 100. So let's go ahead and run our code. And there you go, one all the way through a 100. So when you create a for-loop, you're normally going to create some kind of variable to work with. You'll have the condition that you want to be met. And if that condition is met, it's going to run through your code. And then after it has run through your code, it is going to go to your variable up to here, do whatever it is told to do. So in this case we said I plus plus. So I is going to increase by one because that's what I plus plus does, as we know from the past. And then after this variable update, it will check our condition. Again. If our condition is still not met, it will run through this block of code again. Then once this condition is false, he'll continue on with our code. And we know that from the past. And if we wanted to, we could say C0, done down here and line like that. And what it has done, it will print out. Okay. Now, what if we didn't want to increase I by just one? Well, like I said, you know, kinda put whatever you want in here. But this is the spot you want to have for updating your variable. So we could say I equals I plus two. And there we go. And it's going to print out one because our condition was true. I always find it printed out one and then it added to I. So I was now three. That's still less than a 100 printed out three, and so on. And if we were to change those to 0 here, then essentially it would just print out all of the even numbers. So as you can see, 24681012 and so on. Alright, so when would you use a for-loop instead of a while loop? Well, for one, a for loop is faster than a while loop. Also, you normally want to use a for-loop when you know exactly how many times you want to run through a loop, Let's just do something like this. I less than ten. We'll print that I and increase i by one. Now, if we run it through, it'll run through exactly ten times. So we got 12345678910. So when you know how many times you want to run through a loop, normally, you'll use a for loop when the condition that you need to be met to stop running through live. As more complex, you'll usually want to use a while loop. And then for a do-while loop, you only want to use that when you know, you want a piece of code to run at least once, and then you want it to repeat if a condition is met. But a majority of the time, you'll probably just be using for loops and while loops. Now, to wrap up this video, I do want to quickly go back to what I talked about earlier where I said that we can have a for loop just like this. And this will be completely valid. However, without a condition, it will run infinitely. But that being said, this is valid and you can put in whatever you want. So real quick, let's go over an example of taking out everything except for our condition. So we could have said int i. Then in our variable initialization, we could just set i equal to 0, or we could have taken it out entirely and said I equals 0 up here and just not had a variable initialization. However, we still need the semicolon to separate it from the condition here. Then we said iOS intent and we could even take out our variable update. No problem. However, if we do do that, like I said, you always want to make sure there's a way for your loop to stop within the realm of your loop there. So inside of our loop, we could have said I plus plus code. And as you can see here around just as before, no problems. You don't need to have it all in the same line here, and it will work just fine. Just make sure that you still have some way to break out of the loop and you have a variable that you can actually work with. So that's why we created it back here. Now, I just wanted to show you that that was possible. Most of the time you probably won't be doing that, but that is an option. And in case you see that in someone else's code, you'll know why, you'll know how it is working and so on. But most of the time you'll probably just do it the regular way that we did before. Anyways, that is it for this video. Thanks for watching, and I'll see you in the next one. 38. Chapter 7 (Loops): Nesting Loops: In this video, we're going to be talking about nesting loops. And nested loops is basically the same concept as when we went over and nesting of statements. It's basically just a loop within a loop. And you can keep doing it over and over and over, just like you could with those statements or anything else. So let's go ahead and take a look at how this might work. So we're going to create a couple of for loops for this example. So we'll say for int I equals zero, I less than ten, and then I plus plus. Remember, this is pretty much going to be how all your basic for-loops look. So anyways, inside of here, I'm going to say CL. I just like that. Then below that, we're going to create another for loop. So I'm going to say for j equals zero, j is less than ten. J plus plus. And the code block for that, I'll just say cl j. And before I run a code, let's actually go ahead and remove the M line from up here. Or we said CRI and I'm going to go. So let's take a look at what happened here. So first we printed off is zero because that's what I was. And then we hop into our other for loop. And we'll just call this our j for-loop pronoun. Also another way you may hear of loops referred to when you have nesting loops as the inner and the outer. First printed out I, that was zero, and then it prints it out j, which was also zero. Then J was increased by one. So j was equal to one, which is still less than ten. And so it printed out J again on a new line because we have this N line here. And I feel that all the way through nine. Once I hit nine, jay was no longer less than ten. So it did not print out j again. Then I was increased by one. So I was now one and it prints it out one. After that, it started our for loop over j was equal to zero. J is less than ten. Print out j, j was zero and went all the way through nine again. And I kept doing this and doing this and doing this and doing this until finally I was nine. She went to zero again, print them all the way through. And then once I always equal to ten, it stopped. Now, if we wanted this to look a little nicer, e.g. we could have printed out I along with it. So down here, let's go ahead and say C out I. And then we'll print out j. Then let's go ahead and remove the CLI up here. And now when we run our code, it's actually going to say 00 because i and j were both zero, then zero for i, one for j, zero Froude to for j. And it's going to keep doing that all the way to nine. I'm just going to print out one for AI because AI is going to increase by 10 for j and so on. And even though we're printing out two different numbers, we actually just printed out zero all the way through 99 there. And one interesting way we could have printed out 100 is inside of our J4 loop. We could have said if I is equal to nine and j is equal to nine, then we want to see out 100. Then if we run our code again, you can see that we've printed out zero through 99, just like before, except once I was equal to nine and j was equal to nine, we also printed out 100. So there you go. Anyways, thanks for watching, and I'll see you in the next one. 39. Chapter 7 (Loops): Break Statements: In this video, we're going to be talking about the break statement. The break statement is something you've actually seen before when we worked with switches and we type something like this break line with a semicolon. Now, the break statement, as you can see here, may only be used within a loop or a switch. And we've already seen how it works within a switch. Today, I'm gonna be showing you how we can use it with loops. Now, what the brainstem it does is automatically break you out of the loop, and it does that instantly and does not continue with the rest of the code. So let's go ahead and see how that works. So right here, we're gonna go ahead and create an integer. We'll just call this i and set it equal to 0. Then below that, we're going to create an infinite while loop. Now, I know I said, normally you do not want to create an infinite loop because you'll never get out of it. But with a break statement, you actually can. So I'm going to show you how that works right now. So we're going to say wow. And then inside of that, we're going to say true and create a block of code. So right now, this loop would run infinitely, but we're actually going to make it so we can break out of this. And inside of here, we're going to say I plus, plus, just like that. Down below that, we're going to create an if statement. I'm going to say def I is equal to ten. Then we want to break out of the loop. And you just going to say break and all lowercase with a semicolon at the end. And that will break us out of the entire loop. Okay, So then down here, we're gonna say CL loop. Just like that. Also notice that I put our iterator being I, in this case, outside of our loop. And end operator is basically just something you are using to iterate through a loop x amount of times. So if we want to put it in here instead, it would infinitely because every time we run through a loop, I would have been created and set equal to 0. I would have been increased by one hand, it would have never equal ten. And this will happen over and over and over and over. But if we put it upside of our loop, then this variable is stored and saved outside the loop. Remember we increase it by one the next time through the loop, it's going to increase by one. Again, kinda like when we had this inner for loop and took it out. So what's going to happen? We're going to create an integer called i, set it equal to 0. This is gonna be our iterator for our upcoming live. Then we created an infinite loop when we said, Well, true. But each time we go through a loop, we're going to increase by one. So it's going to start at 0 and go to 1234, etc. Then once I is equal to ten, we have this break statement in here to kick us out of the loop. And then down below that, actually want to print out, I just like that. And we'll put up on a new line each time. And I'll show you why in just a second. Now, let's go ahead and run our code. Now, as you can see, we went 123456789, but we didn't print out ten. Once. I was equal to ten from us increasing it by one, right here, it checked through our if statement. I said yes, and then it broke us out of it and didn't continue with the rest of the code and our loop, we are immediately broken out of it as soon as it hits this break statement and it continues on with the code. That is why we did not print out ten. So there you have it. You now know how to use the break statement. Basically, you're just going to take break, thrown a semicolon, and it will kick you out of a loop or kick you out of a block of code when you use with switches, like we've seen in the past. And it is essentially the only way you're gonna be able to kick yourself out of an infinite loop, like you see here. But you can use this in other ways and not just in infinite loops, right? We could have just said, if I is less than a 100, and then this would have ran a 100 times and stopped. But once we hit ten, or let's just set this to something like 30, it's just going to go ahead and kick us out anyways. So as you can see, once we got the 30, it kicked us out and didn't bother printing it. That is how use the break statement to kick you out of a loop early. There are multiple reasons why you might want to do this. Obviously here it seems a little bit redundant, but you might want to run a loop a certain amount of times until a certain condition is met and then just go ahead and break out of it right then and there. And to show you an example of this, we could have said string password and set it equal to, we'll set it equal to nothing for now. And then inside of here, we could have said, Well, password is not equal to. And then we'll just say password 123 because we have the most secure password in the world, the right. And then down here we're going to say cin password, just like that. And then if password is equal to, let's just say stop. Let's go ahead and change this to exit. We're gonna go ahead and just break out of the loop. So while password is not equal to password 123, we're gonna keep asking for a password. And you can actually go ahead and do that right here, will say, please type the correct password. Just like that. Then we'll ask for a password. If they put it in the correct password down here, I'll say. And let's go ahead and cut that. And inside of our loop, we'll say password is equal to. And then we'll say password. 123, we'll say congrats. Otherwise, if password is equal to exit, we'll go ahead and break out. And I'll go ahead and print out exit. Will say something like exiting or exited. Now, obviously this isn't perfect. We don't really have anything after this, but this is just an example of how you could use a while loop and why you might want to have a break statement for other options. So go ahead and run it. Now it's going to ask for a password. I can type whatever I want That's going to be saved in the string. If it's not the way password. It's just gonna keep telling me that if we and exit, it will say exited, and there we go. Now if I run it again and take password 123, I will say congrats. Now, what if we wanted to give the user only so many attempts? Well, after we did this and after we checked if password was equal to 123, and we could even throw in another break here. If we want it down here, we'll say password was correct. Then after that, we'll say i. And then attempts. Password that was incorrect. Attempts. And then outside of our lip will create an empty. Set it equal to. So actually we'll go ahead and set it equal to, okay, because I don't want them to have some amount of attempts. And then down here, right below her, right above our CL line will say I minus minus, and then put in a semicolon. So it's going to see how password is incorrect. Then I'll print out i attempts, right? So let's start with five. Don't get it right. I'll say for password is incorrect for attempts left, center, right? So go ahead and run our code. We've taken something, say password is incorrect for attempts left, please type the correct password, 3210. And unfortunately it's just gonna keep going into negative unless we do something about it. So right here, we can say, if I is equal to 0, print out you in breakout earlier, just like that. And after this, let's go ahead and do a new line so that it cleans up our code a little better when we do fail. It's right here. Please, to have the correct password typed in something wrong. Servers incorrect. You're four times left, three themes left to 10 times left. Youth, Ben, locked out, and there you go. We've now created a very basic idea for a password program. So hopefully you have a good idea of what a breaks them as how to use it. And maybe you have a few ideas now when to use it. So hopefully you enjoyed the video as always, thanks for watching, and I'll see you in the next one. 40. Chapter 7 (Loops): Continue Statements: In this video, we are going to be taking a look at the continue statement. Now, like the break statement that we talked about in the last video, the continue statement is also used when working with loops. So let's go ahead and take a look at how this works. First off, we're gonna go ahead and create a for loop. So we're going to say for int I, set it equal to 0. Then if there's less than ten, i plus, plus, just like that. Then inside of our for-loop here, we're gonna go ahead and print out i. So we're going to say I create a new line afterwards, like so. Then up above our c out statement here, Let's go ahead and create an if statement. So we're going to say is equal to. And let's just go ahead and put something like five here. Then we want to do the following. We're gonna do is put in the continue statement. So we're going to say continue, all lowercase. And what this does is it actually skips the current iteration of a loop. And I'll also show you how this works and a while loop as well. So what's going to happen here? Well, if we didn't have this if statement, we know that we would normally print out 0 through nine, but because we do have this here, as soon as I is equal to five, it's going to read this continue statement. We're just going to skip everything else after it. But unlike a break statement, which would kick us out of the loop entirely, what's going to happen instead, as it's gonna go up here and read our variable update. So it's going to update I by one in this case. And then it's going to check the condition again and then continue forward. So what will happen is we'll print out 0 through four and then go to sex and onward. And if we run our code here, you can see that that is what happened. So it printed out 01234. Then once I was equal to five, that's I continue. Then I went ahead, skipped up here to our variable update, read that off by one. So I was now six and then it continued as normal. So there you go. That's how continue statement works in a for-loop. Real quick, by the way, with the continue statement and the break statement, if you have a nested loop. So let's say we had another loop outside of this one. So if we had something like this, now, you guys don't have to copy this right here. I'm just trying to show you guys what I'm referring to. But basically, if you have a nested loop here, your continuous statement and, or your break statement will only affect the loop that it is. And so this here would just affect this loop and not the outer loop. And if this was a break statement, it would only break us out of the inner loop and not the outer one. So if I go ahead and print out, I like this and print out j. I get my new line here and run my code. So now if you look at our code here, you can see that we've printed out 0 for r I, and then for the J variable, we went 01234, skipped over to the next iteration of our loop, which also increased j by one in this case, and then continued onward. And it did that every single time because of our continuous statement. And how do we hit a break statement here, like we used in the last video and run our code. You can see that it only breaks out of the current loop. And so we only go from 0 to four with our j variable, and it keeps doing that every time through. So there you go. So how does it continue statement of work, the while loop? Well, it works the exact same way except we don't have a variable update. So if it's something like int I equals 0 up here. And then in our while loop, we said while I is less than ten and inside of our code block here, we're just gonna go ahead and print out I create a new line. And then we also want to make sure that we have a way to break out of this while loop, right? I don't want this to go on forever. So say I plus, plus like that. So without anything being done to this, we know that it's going to print out one through ten Fahrenheit code here. We can see that that is the case. But what if we throw in our continuous statement? So if we say if I is equal to five, once again, be careful to make sure that this is equal to. And then here we'll just say continue. Like so. Once it reaches our continuous statement here, That's just going to recheck the condition and not do anything extra. And that is another reason why we would want to or I plus plus or our iterator here to be before the sustainment. Because if it was after, like this, first off, it would run through, increase that by one. So let's say one, print it out and keep doing that until I was five. Now, once I was five, it would continue and check our condition again. And five is less than ten. So to run through this again and continue and keep running and running and running forever. So we want to make sure that what is causing our while loop to end happens before this here, at least in those case, as not to create an infinite loop pain crash our program. So if I run the code here, you can see that we've printed out 1234. Once we hit F5, we continue and re-render condition six. So it's no longer equal to five, and then it continued to print out. So there you go. You now know how to create and use the continue statement. And remember, all it does is skip the current iteration of the loop. And with a while loop, It's just going to recheck the condition. And with a for loop, if you have a variable update in there, that's going to do that first and then check your condition anyways, that is it for this video. As always, thanks for watching and I'll see you in the next one. 41. Chapter 7 (Loops): Validating User Input: In this video, I'm going to be showing you how we can actually validate user input. Thus far, in this course, we've just been making sure that we've been typing in proper values. So for example, if one of the user or ourselves in these cases, to type in an integer, we've just been typing in a whole number. But if we were to actually go and type in something like hello, we would run into some problems. Now, depending on what kind of program she'll be making, this may or may not actually be an issue for you, as you may never need to take in random user input that you need to make sure it is correct. But I do want to show you how this is done in case you do need it, or in case you do want to implement it into any programs you may create well, trying to learn and understand language. And just so you have this at your disposal and can recognize it if you see it in someone else's code. Now, just a quick disclaimer, I probably won't be using this at all throughout the rest of this course. This video will just be here for reference and for you to learn how to do this, should use choose. The main reason for that. We'll just be because of the extra complexity, but I feel like it adds to the code. And personally I just don't use it a whole lot in the code I make using C plus plus. And let us just because I usually work with game development and I don't have the users just typing in random things to fill in a variable. But because we are working with a console application and asking for the user to type in whatever they want to fill in a variable. I just feel it as relevant to teach you how this works. So sorry for that little bit of rant, but let's go ahead and get started. So first off, we're gonna go ahead and say, I just like that. And then we're gonna go ahead and create an infinite while loop to get user input. So we're gonna say wow. And then inside of here, we'll just say true. Remember, another way we could create an infinite loop is to create a for loop like this and then just do that. And that's what we're on forever because there's not a condition in there. And it will just assume that the condition is true essentially. But I like to use the Wiley personally because I think it's a little more readable than an empty four-loop. So we're going to do is go ahead and ask the user to type in a number. So we'll say C out, please type a whole number. We'll do colon space like that. And then below this, we're gonna go ahead and create an if statement. So we're going to see in like so. Now we're going to type in a bunch of code that's not gonna make any sense to you. But for right now, just go ahead and follow along and I'll go over it all with you at the end and try and make it as easy as possible to understand. Alright, sorry, in here, we're going to say break like that. So I'm just going to put in a break statement. And then below that we're going to say else. And then in the code block for our else statement here, we're gonna go ahead and save the following. We're going to say C out. This input was in the valid, please type number space. And then Dumbo that we're going to say cn dot Clear and then open and close parentheses, then a semicolon. Then below that we're going to say c m dot ignore. And then in parenthesis, we're going to say numeric. All lowercase, By the way, underscore limits the less than symbol, stream size. All one word, all lowercase. And then the greater than symbol followed by two colons. There, I'm going to say max, all lowercase, open and close parenthesis comma space. And then we're going to put in a single quotation mark like that, backslash n, followed by another single quote or close parentheses, and then a semicolon at the end. And this is everything we need to validate. Our input is a bit much in my personal opinion, but this is the way to do it in C plus plus. So let's get into it. So right here at the top we said, please type a whole number. Then down here, we said if CNI, So what does this actually going to do is take input just like normal and then put the input into high. And if the operation works, this will return true. Then it's going to hop into art code block here, which is going to break us out of the loop because we finally got the input we are looking for. However, if this operation fails, then we have our else statement here to cover us. So we tell the user, Hey, this input was invalid, please type a whole number. Then what we did is we said cn dot clear what this is as a method for basically clearing the air that would have come up from trying to put whatever the user typed in into I. That wasn't a whole number. Alright, so that's basically the easiest way to think about this method as we're just clearing the air that would have come up from the user trying to put the wrong data type into I. A little comment here, clear for trying to input the incorrect data type into our integer. And I'll actually just say variable. So see, you know, that this would work with any other datatype basically. Then down here we said cin.ignore. And then after that, you essentially put in the amount of characters you want ignored. And so this whole thing right here, up until this max here basically says we want to ignore the max amount of characters that were inputted up to this backslash n character. Now, the reason we ignore everything. Until the new line character, which in this case would be when the user typed enter as because we know that at the end of whatever the user decided to type in, let's say they typed in an entire book of words. When we ask them to type a whole number, that our program here, it's going to ignore everything they just typed. Because at the end of everything they typed, they hit the enter key, which is the same as a new line character. Ignore everything up to new line character. Enter. Now, why did we do this? Well, first off, we cleared the error that came up from the user to input and incorrect data type into a variable. Then if we would have ran through our loop again without this line of code here, we basically just would've gotten the same exact error and this would never work and we would just be stuck in this loop. Basically, what we did is basically told our program, Hey, just ignore everything that was just type, we're going to start completely fresh. So by ignoring the air and telling our program to ignore everything that was tight, we're basically starting with a fresh, clean slate. Alright, so hopefully that makes sense. Also, another thing I quickly want to mention as anytime you're working with something like this, that could potentially cause an infinite loop as any kind of pause in your program when using an infinite loop. Well, a lot of times be a nice fail-safe for your program crashing. But I would always say, just to make sure we've seen a lot of this before. The only difference is that now you know that you can input something like this. So we can say CAN followed by a variable like we've done in the past. And this will actually return true or false depending on whether or not this worked. Okay, and then down here we have our break statement that we've seen in the past that will break us out of this loop entirely. So that's how we can get out of our infinite loop. And down at the bottom, we have this right here that basically just says, hey, your input was invalid if this did return false. And then we have these two lines of code to give us a clean slate. So let's go ahead and run our program. And it asked for a whole number. And I'm just going to take something like hello. And notice that our program didn't crash, didn't come up with any crazy errors. And actually just to show you one more thing here back in our if statement, Let's go ahead and just print that. I will create a new line like that. Then let's do the same inside of our else code block here. See out there in the line so that you know that we can use those for April and we're not having any errors. Okay? So I can type whatever the heck they want and enter and it is not going to cause any problems. I printed out, I know issue. And just to be safe, even though this dead print out 0, I would actually recommend that we initialize i up here. Personally, I would recommend that you give eye in the initial value before doing something like this. But as you saw, it did work, it did print out 0 in this case. But generally to be safe, That's something I would recommend. Okay. So you can type whatever you want. And after that, you can see that we've printed out I serve they use AI, no problem. And down here we said that they impose on valid, Please say the whole number. And if we had wanted to to clean this up and we could have done the following. We can just take our c out. Please take the number and put it up here outside of our while loop. And that will work just fine. So around back there, whatever we want. We can keep doing this as much as we want because we have an infinite loop. Here's a good example of when an infinite loop is useful when we're trying to get some input like this. And we really don't know when we're going to want this to stop. Now, granted, this is an infinitely, this is a good example of when you would want to use a while loop over a for loop is when the condition that you want to be met is more complicated and you don't know the exact amount of times that you want to run through the loop, right? Because normally with a for-loop, you're going to say when our iterator or a lot of temperature you're used to I in the past as a shorthand for iterator is less than or greater than a certain number or something like that. Anyways, if we go ahead and type in a whole number like we asked, that is going to print it out and kick us out of our loop and we're all good to go. But there's another way you can do this. So if you say something like CAN, right here, down here in the if statement, can actually say c n dot fail. However, for our code block drawn here, we actually want to see if it did not fail. We could just say, if not fail like that. And if we ran our code. You can see this works the same way as before. And if I type a whole number, BAM, kicks us out. Just like that. All good to go. So there you go. You can also use the CN dot fail method to see if that operation failed or not. So if we didn't want to do that, make it just cut this here, paste it down below, cut all of us here, and put it in an if statement and remove this. And this would work just fine as long as we put in a continue statement right here, right? So every time it failed, we would actually just skip back checker condition, which is true and run through. And if it didn't fail, well, we would break out of it. So for my code here again, let's see if we take whatever. I'm just going to go ahead. Repeat. But we're still in our loop, right? Because we just see this dark contains statement that we talked about in a previous video to go back through and check her condition and skip the rest of the code. But if we actually type in a number, there you go. Kick this out just as we would expect. Now, real quick for those of you wondering, as we have seen the word stream quite a few times now, we have been getting code from the iostream header file. We're talking about a stream size here. If I mouse over C n and C out, they all come from that stream file. So what is a string? And don't worry, you don't really need a fully understand this for this course. I just wanted to tell you what this is. Now basically in C plus, plus, a stream is just a stream of characters that are transferred between the program and the input output. And so our C out and C n are what we've been using to interact with the input and output. And when we have this here, this is essentially our stream. This is our characters that are transferred between the program. And as we know, they are only transferred to the program when we hit Enter and all of this text here, both the output from R code and the input from us. So the character is being transferred back-and-forth between our program and the user is part of this input, output stream. So when we said Hey, we want our program to ignore the bank stream size of characters, type, or basically saying any possible thing they could have typed into our console here, go ahead and ignore it. And that way we started with a clean slate. We don't have to worry about the error coming back up, et cetera. That's pretty much it. That is how you validate user input is essentially with these two lines right here. And making sure that the input went through. Anyways, drop it, the video. I'm just going to quickly recap everything we've just talked about. There was new to this video. So first off, we know that we can use our CN statement here directly in a condition and it will return true if the inserting of whatever the user typed into the variable succeeded and false otherwise, then we also learned about the seat and dot fill method, which will return true if this operation failed and false if not, then we learned about these two methods here. And just remember that the cn dot clear here basically just clears the error that comes up from the user trying to input incorrect data into the variable. Then we have the cin.ignore method, which basically ignores the amount of characters that we ask it to up until the point that we ask it to stop, which in this case is just everything that the user typed in. And this is just to prevent the air from coming right back. Alright, so hopefully this isn't too complicated. Now, like I said, I won't be using this a whole lot throughout the course as I believe it's a lot of code that kinda convolutes the videos about a specific topic a bit. But in cases where I do have a make a full program, I will go ahead and throw this in just to show you that this is something we should do if we were asking for user input and to remind you of how to do this and when. Anyways, hope you enjoyed the video. Thanks for watching, and I'll see you in the next one. 42. Chapter 7 (Loops): Scope Resolution Operator: In this video, I'm going to be talking to you about the scope resolution operator and what this operator is for accessing a global variable has the same name, a local variable. So generally speaking, I wouldn't recommend that you have two variables out of the same name. But in the rare case where you do have this for whatever reason, I'm going to show you how you can access the global and the local variable separately. But generally speaking, I wouldn't recommend it, and I'd recommend having more specific variable names. So you always know exactly what you were working with. Because even though we've been seeing things like int x, for example, if I was using this, the chart, the score in a soccer game, I'd want to call this score and not something generic like this, where I'm not going to know what that variable is holding. Alright, so let's go ahead and see how to use the scope resolution operator. So first off, normally, if we have two variables of the same name like this and we try to run our code, we're going to get an error. And that's because you have two variables with the same name. And if you look down here, I actually got an error that says int x redefinition. I tried to finding the same variable twice. However, it will let you have two variables of the same name. If, for example, one is global and one is local. And as you can see, if I try running my code now, it runs just fine and we didn't get there. Now, what if I wanted to access this variable over this variable or vice versa? So let's go ahead and separate these out with some kind of value so that we can see which one we are accessing. This value at ten, and I'll give her a local variable, a value of five. Then we're gonna go ahead and say x, just like that and run our code. And as you can see, it prints out five. And that's because by default, you will be accessing the local variable. But with the scope resolution operator, you can access the global variable. So if they see it again, like we did before and then before our variable, if we just put into Colin's just like that and run our code, you can see then we print out five and we also printed out ten. And that's because these two columns here are the scope resolution operator that will let you access the global variable versus the local variable. And that is pretty much all there is to it. So if you do it with two variables with the same name, you use two colons being the scope resolution operator to access the global one. And you do nothing different to access the local one. Anyways, that's it for this video. Thanks for watching, and I'll see you on the next one. 43. Chapter 8 (Random Numbers): Intro To Random Numbers: In this video, I'm going to be giving you an introduction to random numbers in programming as a whole. And then after that, in the next video, we'll actually be generating random numbers within a desired range in C plus plus. So let's get started. First off, when we want to generate random numbers, you're going to want to add these two files to your program. So up at the top here, we're going to say hashtag include inside of a less than and greater than symbol. We're going to say C std LIB. And that's just the C standard library as with other short for. And then this next one, we're going to do the same thing, except we're going to put C time. And this one here is we're working with, alright, so I'm gonna go ahead and move this down here. Now when it comes to the random numbers in programming, because computers just do what they're told that you can't actually get a random number. It's not possible, regardless of all of the programs and applications you've seen, it's not actually generating a random number. However, to the user, it does look random. And let me explain how that works. So first off, there's this really complicated equation out there that you can put a number into to start with. And it will give you what appears to be a random number based off what you gave it. However, if you kept giving it the same number, it's going to give you the same number back. So in programming where you actually want to do is give that equation a different number every single time. So if a user believes they are always getting a random number in the way we do that is by giving this equation the current time. And because it takes the date and the time, that number will always be different. In another thing I want to show you guys in this video is something called overflow. So first off, an integer variable has this as its max number. And another way we could get this without just remembering and saying int underscore max and just a heads up. There are other keywords like this out there that we won't be talking about in this course. So if you're interested, that's something you can always look into. Anyways, what I'm going to do here, let's just say x equals semicolon at the end. And then we're going to say x plus plus. So we're going to add one to that and then print that out. And to show you what is happening, Let's go ahead and do the phone. And this is CL line. And then men, just like this, and this is going to give us the minimum integer value. As you can see, they are the same exact thing. And the reason for that is that once an integer it goes past as maximum value does something called overflowing, which is where it just wraps around back to it's a minimum value. And then we'll be counting up from there. Now, what have we got to make this an unsigned integer? Then we would say int max, and this will give us the max value for an unsigned integer. Then run a program, actually stay a positive number and just wrap around back to 0, since that is the minimum range for an unsigned int. Anyways, I just wanted to show you what overflow was real quick and you'll understand that better idea of why I showed you that in the next video. Plus is just a good thing to know for the future. Anyways, the main thing I want you to take away from this video is that random numbers and program aren't actually random and they are what we call pseudo random, so they just appear random to the user. And what is actually happening as we're entering a number into a big equation that gives us a seemingly random number. And by changing what this number is, we can get a seemingly random number every time. And that is why we insert the current time into this equation because it uses the date and time giving us a different number. Anyways, that's it for this video. Make sure that you guys add these libraries and we're just going to leave them here for the rest of the course since it's not going to hurt anything. Thanks for watching and I'll see you in the next one. 44. Chapter 8 (Random Numbers): Generating Random Numbers: In this video, I'm gonna be showing you how we can get seemingly random numbers in C plus plus. Now, just ignore this line of texts that I have right here, and I'll show you what this means and a little bit. Anyways, let's go ahead and create a program that simulates rolling a six-sided die ten times. So first off, what we're gonna do is we're gonna say for int I equals 0, I less than ten. I plus, plus. Just like that. Inside of our loop, we're going to say CL, rand, all lowercase, open and close parentheses and put a semicolon at the end. And what this was going to do is automatically just give us a random number between 0. So number between rand max. Now, rand max has a minimum value of 32,767. So we're going to get this crazy range of numbers. Like I said, that is the minimum value, not the maximum. So before we run our code, let's go ahead and put us all on a new line. Then we'll go ahead and run it. As you can see, we've gotten ten seemingly random numbers. However, let's go ahead and try and memorize a couple of these. So this first one is for 1184676334. Those are our first three. And if we run a program again, oh, would you look at that? We got the same exact numbers. And if we did it again, we'd get the same numbers again. And this will continue to happen forever because like I said, it does not truly random. And we're given the equation that I talked about in the last video, the same exact number to start with every single time. So they get more seemingly random numbers. We need to answer a different value into the equation that gives us a seemingly random number. Now, to do this, what we're gonna do is at the top of our program. And by the way, they're only going to want to do this once. Otherwise you won't get a seemingly random numbers as you potentially could. You're going to say that strand, all lowercase. And then in parentheses, we're going to say time all lowercase. And in parentheses again, we're gonna put no. You may also see people put 0 and it's essentially the same thing. And if I mouse over it, you can see, but it's also 0. And then at the very end, we'll put a semicolon like so what this is going to do is when our program runs, we're gonna be putting the time and date directly into the equation that gives us a seemingly random number. And then this random method here is going to give us the result of that. Although this gave us ten seemingly random numbers, the problem was, as you saw, once we called upon it again and we started our program completely. Those first ten numbers were the same every time. And we could have done a 100 numbers and redone our program. And they would have been the same 100 numbers. Again, even if those 100 numbers were different. So now when a program runs, because it's going to use the time and date when the program first starts up. The equation that I have talked about is going to give us a list of seemingly different random numbers every single time, because the time should be different every time we run our program. Real quick, what this thing does as a seed, the random equation with a number. And you can actually just put in a positive number here, like five. And this will work. And as you can see, if I run our program on numbers and the dead before. But if I run it again, there'll be the same numbers. So I'm going go back here. So anyways, why do we only want to have this in our program once? And why didn't we just put it in our for loop? Well, the problem is your computer reads so quickly that by having this inner for loop, we may potentially print out the same number multiple times because we would have had the same exact number seeded into our equation. Because this line of code here, it may have ran twice during the same exact time, and therefore, the numbers wouldn't have been as random as it could have been had we only run this once at the beginning of our program. And because this will allow you to get a list of numbers, you only want to do this once. And like I said, just put this at the beginning of your program. If you are trying to get a random number and you'll be all good to go. We can call upon this as many times as we want. Like I said, when we can even increase this if we'd like and run our code, you can see we get a 100 numbers. And if we look at these top three and it's kinda memorize those real quick. One I could again, you can see that they are no longer the same each time, which is exactly what we want. Now, like I said, we're trying to create a program here, the simulates a six-sided die. And well, these numbers are insane. So how do we fix this? Well, remember that modulus operator that we talked about when we talked about basic math operators. Well, this is a great example of when this will come in handy. So what we're gonna do is around our random method call here. And like I said, we'll get into method calls later on in the course. We're going to go ahead and put parentheses. Like that. And then in here, we're going to put the modulus operator followed by the number we want, so say sex. And now we know that we can only get a value between 05 because this operator, if you remember, gives us the remainder of this number divided by this number. But if we're trying to simulate dice roll, will probably want a number between 16 and not 0 to five. And well, not a problem. Because afterwards, say plus one, just like that. Now we'll get one to six. And if we run our program, There you go. 12345678910, seemingly random numbers between 16. And we can run this as many times as we want. And as you can see, they seem to be at random each time. So there you go. That is the easiest way to get seemingly random numbers within a range you desire. So one more thing I want to touch on is that depending on what we're using, this random number four, there's a chance that depending on what that number is, we make it an error. And it may be hard for us to get that number to come up again, especially depending on the range that we make it out of it. So you can do is actually just save this time, no value somewhere, and then print that out so that on the time that you get the air, you have this value to recede into your program. So what we could do is say something like unsigned this seed and set this equal to time. Just like that. You may also see this referred to as time 0. This does the exact same thing. There's no difference between this line here and this one. So if you do see that just now, same thing. And then down here, we can go ahead and put up. So we could say C out Seed created a line. Now if we run a program, what you can do, if you did get some kind of error while running your program here, you can copy this exact number, go back into your program. And I'll just delete this here quick. And where we have ESR and here you can just input the same exact number we had. And because this needs an unsigned integer value and not an int for an ago head and catch those two enzymes. Like so. There you go. Now we can get the same number every single time that we got when we had our error and diagnose it that way. So that it's just something you want to keep in mind that you may want a way to get the value that was inputted into your program. If you've played any kind of game like Minecraft or something like that, where they have random number generation and they give you a seed that allows you to get that same map again. Well, there you go. That's because that map generation isn't actually random. And you can just put that same value back into the equation and get the same map again. And there you go. That's how you get random numbers in C plus plus. And now you also know how to get the same set of random numbers should use of juice. Anyways, hope you found this video helpful. Thanks for watching, and I'll see you on the next one. 45. Chapter 9 (Containers): Intro To Containers: In this video, I'm gonna be talking to you about containers. A container is essentially just an object that holds other objects and the objects that it's holding, or often referred to as its elements. Another thing about containers is that they have a ton of functions that you can use to work with all of the elements within them. Now there are a bunch of different containers in C plus plus. And in this course we're only going to be going over a handful of them. But I highly encourage you to look up all the different containers and at least see what it is that they do so that, you know, they're available to you should you need them. Also only be covering a few functions and our methods for each container type that we'll be covering. So once again, if you want to learn more about them, I highly recommend looking up the different functions out there so you can see what's available for that as well. Now, when deciding what container you want to use for your project, you'll want to not only choose one based on how well it handles working through all of its different functions, but also based on the functions themselves. As each one will have something they can do a little bit differently than the other. It's, like I said, you want to choose based on how quickly each one does, one task or another, as well as the functionality that is available to it. And for those of you who come from another programming language background, you may have also seen these referred to as collections. Anyways, that's it for this video. Thanks for watching, and I'll see you in the next one. 46. Chapter 9 (Containers): Arrays: In this video, we're going to be talking about arrays. Now. Arrays are the very first container that we'll be talking about in this course and are often considered one of the most basic container types that you will see in other programming languages as well. So first off, arrays are basically just a group of elements of the same type stored in sequence. And they also are going to allow you to access multiple elements using the same identifier. Remember, a container is basically just something that holds multiple objects. And the objects within that container are referred to as elements. So that's where that word comes from there. So let's go ahead and take a look at the basic syntax for an array. So first you're going to have the datatype followed by the name, followed by open and close square brackets. And then you'll finish that up with a semicolon. And then inside of those square brackets, you're going to have the size of an array. Now when you create and use an array, you started to know the size or how many elements you want to store it in that array, as it is really inefficient to go with changing this. So let's go ahead and create basic array of integers. So to do that, we're gonna go ahead and say hello to buy int array like this. And then inside of these square brackets, we'll give it a size of five, for example. And then we'll put a semicolon at the end. So this here is going to create an int array named int array with five integers in it. And we can name this whatever we wanted. I just decided to call it int array for simplicity sake. Now, notice if I mouse over this, it says they are not initialized. That means they do not yet have an initial value. And there's a few ways that we can go ahead and give it one. So the first one would be to put it before the semicolon. So, so far, we've just declared this array, but we haven't given any of the integers in there and initial value. So after this we can say equals and then to open and close curly brackets, like so, I noticed those dotted lines went away on my screen here. And now each integer in our array has a value of 0. So this is one way. You can give each integer value 0, but we can also go ahead and give them an exact value and different values within these curly brackets. So to do that, we can simply say something like this where you'd go for one hundred twenty two, twenty one, and seventy six or something. So now all five of our integer variables in our int array have an initial value that we've set. So the first one will be for, the second one will be one, and so on. Also in C plus plus, you are allowed to just remove this equal sign and this will be the exact same as if we had it there. Personally, I like to have it there since we already use the assignment operator here to initialize stuff like variables and things like that. But like I said, you can take it out. So he do see someone's code without that, you know that it is the exact same as with it. Now, what if we wanted to go ahead and print out all phyla are integers? How do we access these? Well, let's go ahead and create a for loop. So we're gonna say four, and then we'll say int I equals 0, I less than five, I plus, plus. And then inside of our code block here, we're going to say C0, I'll say int array, because that's the name of our array, followed by open and closed square brackets and a semicolon at the end. And then inside of here, we need to put the index of the one we want to print out. And because we already have an iterator that's gonna go from 0 to four. We're just going to put in. Now to show you what the indexes accordingly. The way arrays work as this very first integer variable, NRA is at index 0. So if I just wanted to print this one out, I'd put 0 here like this, and then it goes 01234, etc. So we're just gonna go ahead and put an I. And then after this, let's go ahead and throw in an airline and run our code. And as you can see, we've printed out 41520 to 121 in 76, alright, and to show you that these all got an initial value of 0 with nothing in here. If we run our code with nothing in there, they'll print out 0. Alright, now let's go ahead and take a look at the other way to initialize the values in our array. So let's go ahead and get rid of this equals and the curly brackets here. And just the same way that we accessed the values in our array to print them out. We can also access the values to initialize them. So we can say int array. And then in square brackets, we could put 0, for example. Just say equals whatever we want, like that. And then put a semicolon at the end. And this will work just fine. We could go through like this if we wanted to go 1234 and get each of these a different value. Alright, if we run a code, you can see that this works just fine. And we went 01234. We could have done these in any order if we want to. Now, let's say we wanted to get all of these the same exact value of say, 30. And let's say we had a lot more than five and our array. So let's say we have a 100 of them. Well, instead of doing this, which would take us for ever, we can just go right down here. So I less than a 100. And then down here, we'll just add another line above where we are printing out our array values and say int array. I will use i for index once again here, and we'll say equals 30. And now it will print out 3000 times. And I'm not going to count these, but trust me, there's a 100. Alright, so there are the different ways to initialize your arrays. Also, we can go ahead and set the variable equal to the value within an array if we wanted as well. So we can say x equals int array and then throw in an index like to, for example. And if we were to go ahead and print out like so, I'll go ahead and comment out this line. So you can see that that is working since there's a ton of 30s there and there you go. So there you have it. You now know how to work with arrays. Now, when you want to use an array, well, first off, remember that the size of an array is fixed. So you need to know the exact value, but you're gonna be putting into it. And I can show you that by doing the following. So if we go ahead and erase those here and said x equals a 100, and then try to put x in here for the size of our array. You can see that we get an error because it needs to be a constant value that cannot change. Now, we could make this a constant and this would work just fine. But just note that the size of your array needs to be known in advance. Also, they are efficient for getting the values back out of them as we did down here when we printed those out. And they're great for when you want to get the elements within your array out in order, like we did down here as well. Now went to avoid using them. Well, you should avoid them when you want to insert or remove elements from the array, because like I said, they're fixed size. So if you aren't going to know the size when you start up your program, that is a problem. And a good example of when you would not want to use I'm I right? Anyways, that is it for this video. In the next video, we're gonna be talking about something called a multi-dimensional array. So thanks for watching, and I'll see you in the next one. 47. Chapter 9 (Containers): Multi-Dimensional Arrays: In this video, we're gonna be talking about multidimensional arrays. Now, the best way to probably think about multi-dimensional arrays as arrays of arrays. Also, a multi-dimensional array can be as many dimensions as you need or desire, but be aware that the memory used up, it goes up drastically with each dimension added case. So that's something to be aware of. So I have an example of a two-dimensional array here for you, which will be creating here in a minute. So for 2D array, you're going to have your data type, your name just as before. But now instead of just one set of square brackets, you're going to have to, if you wanted to do a three-dimensional array, you'd have a third set of square brackets and so on. And then you need the size for each of these dimensions. And I just put rows and columns to help you visualize what this would look like if you visualize this as a 2D grid where you have rows and columns, and you can visualize the elements of a two-dimensional array with three rows and three columns being visualized as such. Now, to actually access each one of these elements, we would be doing what is over here accordingly. So this first one will be 000102. Then we're gonna go down to row one, column zero, because remember, indexes start at zero when it comes to containers. So this will be 10111220, t1, t2. Alright, so let's go ahead and actually create this and print it out like it is up here. Okay, So what we're gonna do is we're going to say, I'll say more type array. And then in square brackets here, we're going to say three after that. And we'll say three again. I'll put a semicolon at the end. Also, we can go ahead and initialize our multidimensional array in a similar fashion to how we did a regular array. So we can say something like equals and then have two curly brackets like so. And these will all be initialized to zero. Also. We can do it the other way where we put in a value directly. So if we wanted the values to look something like this here, we could say one comma two, comma three, like so. And then create another set of curly brackets for the next row. You could say 456, and then do it again. So seven, nine. And now the last thing we have to do to make this work as put this whole thing and another set of curly brackets. So we'll go like this. And we also need to put a comma between each set. So just like that. And if you find it easier to visualize, you can do something like this. But anyways, I'm gonna go ahead and move these back to how it was. Then we're gonna go ahead and actually print out every value and are multi-dimensional array. But before we do that, I'm gonna show you one last thing that I forgot to show you in the last video, and I'll show you how it works with a multi-dimensional array as well. So just in case you come across it, as I would not normally recommend doing this because you can come across some unexpected issues is you can say int my array, e.g. we'll give this a size of nothing. And then we can say equals and then just initialize it in our curly brackets like this. And this will actually work just fine. And it will automatically know that it's going to be the size of four. Now, you can do the same here, but you can only remove this first value. So in this case, it'd be like removing the amount of rows, which will be figured out by how many sets of curly brackets we have. Then the amount of columns you need to specify, because this will not work. As you can see, we immediately get an error, right? So I just wanted to show you that quick. After the size back in there explicitly as well. Then let's go ahead and actually print out our array here in a fashion that looks like this. So do this, we're going to create two for loops that we're going to say for our zeros are less than three, r plus plus. And then inside here, I'm going to add another for-loop and say int c equals zero, less than three, C plus, plus. And we'll create a curly brackets like so. And then we're gonna go ahead and print out the values. So we're gonna say C out. Alright? And then in square brackets here, we're gonna put R and then instead are other square bracket. Then after that, we'll just print out a blank space like so. And then below that for loop, inside our other one, we're going to create a blank line. Also, just as a heads up, those Hollywood access, initialize and edit any of the values and our multidimensional array. So before where we would just type the name of the array followed by the index of the element. Here, our index would look something like this as I showed you earlier. So what's going to happen is we're going to go through our first for loop. And I called this variable are short for rows, but you could call it whatever you wanted. So first time through R is going to be equal to zero and c will be equal to zero. So it's gonna get index 00 and print out one because that is what we have, right? And then it's gonna go back through this loop again and print out two and then three. And then one chicken was back around, rows will change to one. So we're going to be at 01:01, 112, et cetera. So let's go ahead and run this and they go print it out. 123, McDonald line 456, and down the line again, 789. And there you have it. Now, we could have also created a double for loop like this to initialize all of these values one through nine, like we did up here. So let's go ahead and see what that would look like if we went ahead and copied those double for loop and pasted it up here, notice that we didn't get any errors because both of these, both are local to their specific code block and don't exist outside of it. Having art here and C here, and R here, and see here, it doesn't cause any problems. Then let's go ahead and remove our initial initialization up here. But we can go ahead and initialize them all to zero. That's fine. And then down below this we're going to say int I equals one. And then here we're going to say, instead of printing this out, we'll say multi-rate RNC just like before. But instead we're going to say equals. And then just below that will say I plus plus, so that it increases by one each time. Alright, now, let's go ahead and run our code. As you can see, that all initialized, just fine. And the only reason that's actually down a few lines here, just because we left them the new line. But as you can see, just the same way that we were able to print them out in order using this double for loop. We were able to initialize them in order using a double for loop, right? Because it's basically the same exact thing. But instead of just printing it out, we gave it an initial value, doing just increase I by one every time they give them a value in order. And while we actually have all those cut up, I might show you how we can quickly make a multiplication table and if we wanted to. So let's just go ahead and change these to ten, e.g. or we could even do something like, let's say something like 30. And then down here will change, rose to 34, our r value. And both of these, and same with our own value. And we're going to start them both at one in all cases here. Then, because we were shooting on both at one, if we actually wanted the table to have a full 30 digits, we'd want to change them all to 31, like this. Then right here, we're going to remove this I plus, plus. And so setting it equal to i, we're going to say R times C, just like that. Now we can remove heart and I variable entirely. Alright, so now we've created a multidimensional array of size 31. Then we created a for-loop with an integer variable that starts at one and continues to live through as long as it is less than 31. And we increase our by one each time, then we do the same thing with C down here. Then we put r and c for our indexes here and just set the index equal to R times C. And then we're gonna go ahead and remove this, the line statement here. But we'll leave it in the one down here. Then, instead of just putting in a single blank space here, we're just going to put in a tab. And a one easy way to do this would be to put in a single quote like this or a set of single quotes, I should say. And then backslash t, which will create a tab. By the way, anytime you have the backslash and a character, Let's can be just considered a single character for a char. Alright, so I'm just going to run just fine. Now, let's go ahead and run our program through. And as you can see, this is actually pretty big though. Let's go ahead and lower this down to something like 11 for each of these. And run our code again. And they go 1-1012 times two times two is four. And if we look over here, six times 636, et cetera, and we've now created our own multiplication table with just a few for-loops and a multidimensional array. So it's pretty neat anyways, that is it for this video. Thanks for watching, and I'll see you in the next one. 48. Chapter 9 (Containers): Vectors: In this video, we'll be talking about vectors. And you can think of vectors as essentially a re-sizable array with some extra functionality. And that's because there are a bunch of extra functions or methods associated with them. And I'll be showing you a few of those here in this video. But if you want to see all of the possible methods or functions out there, I highly recommend you look those up as there is a ton of them. Now, real quick, I went ahead and put back in here the syntax for an array. So if you remember, we're going to put the data type, the name, and then the size and some square brackets. However, the syntax for a vector is slightly different. So we're going to say vector and all lowercase followed by the datatype in-between, a less than and greater than symbol. And lastly, we'll have the name followed by a semicolon at the end. Now before we take a look at how to create and use a vector of our own, we do need to add in another header file at the top of our code here. So we're going to say hashtag include. And then in-between the less than and greater than symbol, we're just going to say lector, just like that, now will have access to everything we need. So let's go ahead and get started. First off, we're going to say vector. And then in-between the less than and greater than symbol, we'll have our datatype followed by the name, and we'll just call this my the vector, just like that. And inside of here, just like with other containers, you're going to put a multitude of datatypes. Thus far, we've been using integers, but in this case, why don't we just go ahead and put in strings. Okay, So we'll just say string, and instead of calling it my vector, why don't we just call it backpack, like so. And then just like we could with arrays, we can actually go ahead and immediately put some elements into our vector here, right here, we could say equals and then in curly brackets. Now, notice here that we don't have the size of our vector and that's because I said it is re-sizable. It basically is a dynamic size, meaning it can change as our code runs. So right here, we can go ahead and give it a default size by throwing in some default values. So let's just pretend we were filling up a backpack here. So we'll put in a few strings. So we could say something like book, Kama, pencil. And we'll also throw in notepad. Okay, now, obviously these are arbitrary and just words, but you get the idea down here. We can actually print out things from our vector the same way we would with an array. So we can actually just say backpack and then open and close square brackets inside. We'll put the index and the index works the same exact way as it did with arrays, where it goes 012. So we said biotech one and we wanted to print that out. We would say C out backpack one, like that. And if I run my code here, you can see that it says pencil because that is the string at our first index here or at index one. Now, the interesting thing about vectors compared to arrays is that like I said, it is a dynamic size. We can actually add things to this and there'll be added at the very end. In also, like I said at the beginning, there is some extra functions or methods associated with vectors that we can use. Let's go ahead and take a look at one of those right now as well. So down here we can say backpack or the period symbol there. You're gonna see a list of options here, at least some visual Studio. But we're actually just going to go ahead and type in size and then open and close parenthesis and a semicolon at the end. What this is going to do is tell us the exact size of our backpack vector. And if we wanted to, we could spend that out as well. I say C out before it, just like that. And let's go ahead and throw in a new line here. There's a deadline for that, just so it's not on the same line as this. And now when our code runs, it's going to print out pencil. In this case at least. And then right here, it's going to print out the size. And unlike with indexes where it goes 012, the size is going to go 123, just like you would expect when he wanted to know the size of something, they'll know exactly how many items are in it. So there should print three. And if we run our code here, you can see that that's what it does. We print it out pencil and then on a new line, we print it out three. So how do we actually add items to our vector? Well, if we go down here, we can just tape on her back there again so that it knows what we're referring to or what we're wanting to work with, which would be backpack in this case. And then we're gonna say dot and then see this pushback method here. That's what we're gonna be typing it. We're gonna say push and the score back. And then in parentheses, we're gonna put what we want to add to our vector. So we're just going to put the value that we want to add, just like we put a pencil in Notepad up here, add a another string, since our vector is made up of strings. So right here, let's just take something like thriller and we'll put a semicolon at the end. And now ruler is going to be at index three because we have 012 and then we're adding this one. So this would be three. And down here we say CL backpack three. And let's go ahead and make sure it was put on a new line. So we'll just say M line like that and run our code. You can see that as the case pencil. Appear, printed out three, added a ruler and then print it out what was at index three. Now right here, it's pretty easy for us to tell that this ruler item we just added is at index three, saying is everything's are right here in the Feds, right here on our screen. But what if we just wanted to print out the last item in our vector? But we didn't know exactly what index that was. Well, because we have this size method we can use that will tell us the exact size of it. We could actually just say backpack dot size. Then after this, we need to say minus one. Now, the reason we have this minus one here as because even though our backpack or a vector size forks, we have four items here, it does not have an index of four because it goes 0123. And if we want to access the actual value here and print that out, rather than just printing out three right here, we're going to put this in square brackets after the name of our vector, just like we would if we were trying to access any other value here. Let's go ahead and cut that. And we'll say backpack in square brackets. Let's go ahead and paste that in like so. Now it's going to print out what's that backpack index four minus one or whatever the max size is minus one, which will always be the last item in our, okay, so that's how you can get the last item, should you need it? And if we run our code right here, you can see wheat that in fact print out ruler. Now we can also remove items from our vector and there will be removed in the same order. So just like it adds it to the very end of our vector, we can remove things from the end of our vector. So real quick, I'm just going to go ahead and remove this c out backpack. One line we have here, so we create a backpack. We print out the size, which in this case is three. We add an item, we print out what our last item is, then we're actually going to go ahead and remove that last item. So down here, we're going to say that op, underscore it back. All lowercase there, open and close parentheses and a semicolon at the end. And what this is going to do is push the last item right out of our vector. So if we copy our printing of the last item here and paste that below and what aren't coat, you can see that, well, we created a new line first, then we printed out three because that is the size of our vector. Then we added a ruler item, printed out the last item being the ruler saying is we just added that and then we got rid of the ruler. So now our last item is Notepad and we printed that out as well. So there you go. You now know how to create and use a. And these three methods here being the size, the pushback and the pop back method are probably the three most common methods and OR functions you'll be using when it comes to vectors. So when do you want to use a vector? Well, when you want a bunch of data to be stored in order just like it was with an array. And when you do not know the size at compile time or when you run your code. So we didn't know what the size was going to be, and we didn't have to enter a size like we did with an array. It was just able to change dynamically as our code rent. So if that is something you need, you might want to consider using a vector. And if you plan on adding and removing elements from the very end of the container you are using, then a vector is another good option because as you've seen here, there is a method for that. And because our information is stored in order, we could have iterated through this and printed it all out just like we could with our array. Now, when would you not want to use a vector? Well, if you're trying to add or remove elements in the front of the vector, meaning like back here where book was at index 0 or somewhere in the middle. Especially if you have a lot more items in here, then this probably isn't for you. Also, if you already know the size of the container and that's not going to change. Might as well just use an array. Now it's dark here. Let's go ahead and print out everything in our vector just to kinda show you one more neat little trick that we can do with a vector because of these extra functions. So right here, I'm going to say for int I equals 0, I is less than backpack die size. Then I plus, plus, and then below that in curly brackets are simply going to say backpack. And then square brackets. We'll say hi, and after that will just create a new line like so. And now it's going to print out everything in her backpack regardless of how many items are in our Peck, Peck, right? Because it's dynamic now based on the size. And then let's go ahead and remove everything up here, just like before, our for loop and run our code. And as you can see, we've printed out book, pencil, pen. And even if we change our size here, and we could do this up here, or we could do so using our push-back function again. So we'll say backpack dot push back. Then in parentheses there will say something like tens. And then if we run our code without changing anything here, you can see that we now printed out pens. So there you go. There's just another use case for the size method. And another reason why you may want to use a factor for the extra functionality that we talked about at the beginning. And that's because as you can see, we can change our vector by increasing the size and decreasing the size. And our for-loop never has to change. And before I forget, we're gonna go ahead and leave this vector header file and our code from now on along with the rest of these. And we're going to continue doing that from here on out. Anytime that we add a header file, we're just going to leave it unless otherwise stated. Anyways, that's it for this video. Thanks for watching, and I'll see you in the next one. 49. Chapter 9 (Containers): Queues: In this video, we are going to be talking about queues. Now a queue is another type of container. They usually say first in, first out type of arrangement. So basically whatever is in front of the queue, it was going to go out of the queue first. And you can kind of think of this as queuing up to get to the register at a grocery store or something like that. Whoever gets there first is going to get out first. So first in, first out. Now the syntax for this is very similar to the syntax of a vector that we just saw in the last video. But instead of saying vector, we're just going to say queue. Once again in all lowercase. At the top here, we're gonna go ahead and include the header file as well. And that's just gonna be called queue and be sure to keep that there for the rest of the course. So at the top here, and we'll say hashtag include like that. Now, let's go ahead and create and work with acute. So for this example, we'll just create a queue of integers. Here. We're gonna say Q, then R datatype with them the less than and greater than symbol. So we'll say, then afterwards, we'll give this a name. Why don't we just call this something like quiz scores. Okay, then let's go ahead and actually add in some integers to r cubed. So down below here to do this, we have to do is say the name of our queue so that it knows what we're talking about in the first place. Don't want to say dot, push and in parentheses, we just put what we want to add in wireless semicolon at the end and say something like a 100. And let's go ahead and add a few more that we can put in whatever number you want. Like. So with a queue, if we want to print out something, Enter q, we actually cannot do. We have in the past, so we can't say quiz score. And then in square brackets, say like two, for example, with a semicolon at the end is this does not work. And you can see here, and if I tried to say C out here, it would not allow us to do that, we will get an error. So how do we do it? Well, if you want to pronounce your entire queue, you're gonna do the following. So first off, we're going to create a while loop and then we're gonna put the not symbol and our condition here, followed by the name of our queue, quiz scores. And then we're going to say die, empty, all lowercase with open and closed parentheses, like so. So what we're saying here is that while quiz scores being our q, is not empty, so this empty will return true if it is empty and false if it is not empty. But if you remember, because we have this knot here, will get the opposite effect. Dressing well, quiz scores is not empty. We wanted to do the following. And what we wanna do is print out what is at the front of our queue. And if you remember, the Q container uses a first-in, first-out. So because if we look here, 100 was first, it's gonna be the first one we print out. And to do this, we're simply going to say CL. And afterwards we're gonna say quiz scores. And this is going to print out what is at the front of the queue. That was just open and close parentheses like that in a semicolon at the end. So what this front method here does is return the value at the front of the queue. And there's also a backup method to get what is at the back of the queue. But if we wanna be able to get everything, so not only just the front or the back, everything in-between, we're actually going to have to start removing them. So like I said, you can think of a queue is getting in line somewhere. The first-person is gonna go in the second, the third, the fourth. And everything is going to go in order. And that's how it's going to be here. Okay? So right below here, you're actually going to say quiz scores, open and close parentheses and a semicolon at the end. So I, quiz scores dot plot does, is it actually removes the element and the front of our queue just gets rid of it. This will be gone. Now, what's going to happen is first we created a queue saying Q, R datatype in-between the less than and greater than symbol and gave it a name. Then we added elements by just saying her name dot push, and whatever order we added these in the order, they will be in an RQ, and it is also the order they will leave. First one goes first, second, second, third, third, and so on. Then down here in order to print these out. Because like I said, we can only get access to this front one in this back one with either the front method or the method. But here we just use the one called front. And we said, wow, quiz scores is not empty, meaning there's still something in our queue. We're going to print out what's in the front and then get rid of what is in the front using the pop method. And you may recognize the word pop and the word push from when we worked with vectors in the last video, except we had to save pushback in, pop back. Now, before we run our code here, let's go ahead and throw in a new line here. So when a code, and you can see we printed out 18034 of the file. And then before we wrap this video up, I'm just going to show you one last method. So right before a while live, if we say quiz scores, dot size, opening, close parenthesis, and a semicolon at the end. And let's go ahead and actually print that out. So say CL right before that there will also create a new one afterwards and run our code. You can see that it prints out the size, just like we could do with our liked there. So that's always nice. So we have 1803455 for different elements here with the print that out or get that value with the size method where, but check if it is empty with the empty method. We also learned how to check the front of the cube with the front method. And we also know that we can get the back of the queue with the back method, which is written just like you would think, where you say back in all lowercase and set up front. And if you're thinking of this like a line at a store or something like that, then this will be the front line and this will be at the back of the line. So that is an easy way to remember what value you are getting when you say front or back. So you now know how to create and work with the cue, how to add values and remove value. We note that it is a first-in, first-out type of container and we know a handful of the methods associated with it. And as with the other containers, there are some other functions that you can use with this are that are associated with it. And if you're interested, feel free to go and check those out anyways. But that's it for this video. But before you go on to the next one, but I want you to do is go ahead and save everything you have because we're going to use it in the next video when we talk about a very similar container called a stack. Anyways, thanks for watching, and I'll see you in the next one. 50. Chapter 9 (Containers): Stacks: In this video, we're gonna be talking about another container type called a stack. As you can see, I have this code here from our last video because in case you missed it, I wanted you guys to save what we had from them because we're going to use it again in this video. And if you forgot, no worries, just go ahead and copy what I have here on screen. Now, before we get into stocks, we need to add another header file. So up at the top here, we're going to say hashtag food. And then in-between the less than and greater than symbol, we are going to simply say that just like that. And once again, we'll be leaving this for the rest of the course. So make sure you are saying they're now down here with the actual code. Let's go ahead and take a look at the differences between a stack and a queue. First off, the only syntax difference between a stack and a queue, because that instead of saying q, we're going to say stack. How easy is that? So we'll say stack. Just like that. We can leave our name here if we wanted to. We can even leave these methods here if we want, because they've worked with the exact same way. And also it has a push method which puts these one in front of the other. However, a stack works and last-in, first-out type of arrangement. So if you want to think of stacking blocks are putting one card on top of another and then always drawing from the top. Basically how this works. So this here is actually going to be like our bottom card, e.g. and then we'll have our one on top of that, the one on top of that and the one on top of that. And then when we draw from these, we're going to go back in the opposite direction like this. The last one in is going to be the first one. So this here is the top of our stack. Here is the bottom. So just remember that now our size method here even works as well. So we don't have to change anything there. Now, when we get down to our while loop here, you're going to notice this frightened method has an error, and that's because we no longer have the method called front. Instead, we just say top. So the top of our stack is what we'll be getting when we say pop here for you, popping off the top of our stack, alright, so everything else can stay the exact same. Let us fantastic. If you want to work with a stack or queue, pretty much nothing is gonna change except with the methods that we've seen here where the function, you only want to change this instead of the front method that gave us the first value put into our queue, we have this top method was because that's the last value put into our queue, because we work with a last-in, first one out type of arrangement when it comes to stacks. So if we went ahead and run our code here, you can see that everything is working. However, we printed out our numbers and the opposite direction that we did with our queue, as it is a stack and works as a last-in, first-out rather than a first-in first-out. So because 55 was at the top of our stack and was the first one out of our snack suites at 553,480.100. And the reason we have four up here as because we also printed out the size and we have four elements in there. Now, I also want to mention because the front method doesn't exist, neither does the back method. You cannot get this bottom value, and that's pretty much it for this video. Now, just like with the other containers or other functions and our methods out there. And I highly encourage you to check them out if you're interested in using a stack in your program. So when would you want to use a queue and when would you want to use a stack? So when you want a container that works in a first-in-first-out style, use the queue. And when you want to contain it works in a last-in, first-out style. You can use the stack, but just remember, you really only have access to that last value that you put in a stack and a queue, you have access to both. However, with a queue, you can only take out the first value that was put in with a stack is the last value. So you've got to last-in-first-out and a first-in-first-out. But if you do need to work with those middle values, remember, they'll probably just want to use a different container. And we've talked about a few of them already, but there are some others out there. So if you're interested in those, go check that out. Otherwise, we do have at least one more coming up in a future video in this course called a map. So stay tuned for that. Anyways, that's it for this video. Thanks for watching, and I'll see you on the next one. 51. Chapter 10 (Foreach Loops): Intro To Foreach Loops: In this video, we're gonna be talking about a new type of loop. The for each loop, what a for each loop does is it runs a section of code for each element in a container of a specified type will be creating one of these. And the next video, we'll see exactly what it is I'm talking about, but I just wanted to give you a brief introduction video as to what they are and what they do. So the other thing to note about a foreach loop is that they actually copy the values in a container that they are using for their operation. And I'll show you an example of this when we work on these as well. Also, for each loops only run through a container in order. This is something to take note of, that it will go from index 0 up until the end. Now, let me give you a couple of examples of when you might want to use a for each loop. Now, one example might be that you want to print out everything in a container. So you can print out the value of each element and are maybe you want to get the size of an array. For example, well, you could have a variable and then in your a for each loop, you could say for each element in our array, we're going to go ahead and increase that variable by one. So you could also use a for each loop to get the size of a container, just as an example, anyways, that is it for this video. The next video, we're going to be creating a for-each loop and working with it, as well as comparing it to a for-loop which we've learned about in the past. Just so you can see the differences and learn about when you would want to use one over the other. So thanks for watching and I'll see you in the next one. 52. Chapter 10 (Foreach Loops): Foreach Loops: In this video, we'll actually be creating an working with for each loops and comparing them to FOR loop so you can see the difference and know when to use one over the other. Now, the reason I decided to divide a simple loop into two different videos, it's because personally, I found for each lips fairly confusing, at least the way they explained to me when I first learned them. So I just wanted to make sure that could break it down step by step and really make sure that you understand how and when to use them without any issues. So first off, we're going to start by creating an array of ten integers. So we're going to say my array square brackets here. We'll put ten, like so. And we're not going to give this a value here. And then said we're gonna do it in a for loop. So down here, we're going to say for int I equals 0, I less than ten, I plus, plus. Then inside of our for-loop here, we're gonna give each element in our array and initial value. So we're going to say my array at index. I. Remember you just put that in square brackets, like that equals, right? Now, we're going to set this to five. Then above or for loop. Let's go ahead and say sram. And if you remember how to use this inside of here, we're going to say time, and in parentheses will say no. And if you notice when we mouse over S right here and actually takes an unsigned int. So some people like to make sure that they'll get an unsigned int back and just say unsigned like this in parenthesis there. And they'll make sure that you get a positive value out of this time method. Then down here, instead of setting everything equal to five, we'll say rand in parentheses like that, followed by the modulus operator. And we'll put it a 100 plus one. And let's go ahead and put this in parentheses just to make sure order of operations as correct or to explicitly define it. And then down below that, let's go ahead and create a, another for loop for printing out our variable. Yes, I know we could print it out here, but you'll see why we're doing this in a moment. So down here, we're going to say time equals 0, I, less than ten. I plus, plus. And then in curly brackets will say my array index I. And we're actually going to say C out my array index, I will create a new line after each one, like so. Now below this, we're gonna do the exact same thing we did in these two for loops. What we're gonna do it in R for each loop. But first, let's go ahead and run our program and make sure that everything is working. So as you can see, we've got ten seemingly random numbers right here, between 1100. And now. Let's go ahead and comment all of that out real quick. And we're actually going to leave it here, so don't delete it. And then below that, we're going to do the same thing in a for each loop. So down here, we're going to create our first foreach loop. And the syntax for this is going to be as follows. So the syntax for a, for each loop looks like this. We're going to say four. And then in parentheses, we're going to say the datatype of our elements, followed by a variable name, then a colon. And lastly the name of our container that we're gonna be working with. So here we will have our container just like that. And in case you're not sure what I meant by all of this here. You will see in a second when we go ahead and create it, and then I'll go back over it. I'll load at the end to make sure you understand. So now let's go ahead and actually create one that works with our array. Let's say for in our array data type as an int. So that's gonna be the datatype of our elements there. So in here, let's say for int, then our variable name can be whatever we want. So let's just go ahead and put something like eggs. And then after that, put a colon followed by the container name, which in this case is my array, like so. And after that, I have a set of curly brackets. And there you go. We've now created a for each loop. So how exactly do you read this? Well, I know what it links those four here, but just imagine here for a minute. This is for each element in our array, you want to create a variable called x and set that equal to the element. So as it goes through our array here, x is going to be equal to each element as it loops through. So hopefully that makes sense. If not, we're going to go ahead and print out the result of what I'm talking about here. So first, we gotta go ahead and initialize all of the variables in our array. We can actually do that with our foreach loop period. But first, we're going to have to say something like this. We'll say i equals 0. And then every time we run through our foreach loop, which is going to be the same amount of time as far as elements in our array, because our foreach loop runs once per element in our array. So in here, we're going to say I plus plus. And we'll also say my array in square brackets here. We'll say i equals, and let's just go ahead and set it equal to I. Then down below that, we'll create another for each loop through our array. So say for an array. And in this code block will just print it out. So say x line and what our code. So to give you a better view of what's happening here, let's go ahead and move into my array here, down here, and run our code again. Okay, so as you can see, we've created an array, just like we've done in the past with ten integers. Then we created an iterator here by just creating a variable called I and setting it equal to 0. Then we said for each integer in our array, we want to create a variable called x. Then we said for each integer in our array, we want to run this block of code. And when we do that, we want to set x equal to each element as we go through. So the first time through x will be equal to element 0, then one, then 23, etc. Now, we don't have to worry about the fact that this was uninitialized because we didn't do anything with x. Instead, we just said my array at i, i was 0. So the index 0 is equal to I, which the first time through is 0. Then we increased it by one and we went through it ten times, giving our elements the value 0 through nine. Then after that we created another for each loop. We said for every integer and our array, we're going to run through this block of code. I don't want to set x equal to each of the elements as we go through. So because element 0 was 0, x is 0, and then print it out 0. Then the next time through, because element one was one, X0, X1, and then print it out one and so on all the way through. And this is the exact same as if we took our interest here and moved it up top. I'm commented our code here. And I'm gonna go ahead and cut this random they may here and just set it equal to I and on code. And as you can see, we've printed out 0 through nine from both of our for-loops, and we print it out 0 through nine once again with our four each loops. So we did the same thing here as we did here. So when would you want to use a for each loop instead of a for of loop? Well, personally, I think when we are initializing the values in our array, this looks a lot cleaner than one. Our iterator is on a whole separate. Then our variable update here being I plus plus is also on a separate line. It's also not clear how many elements are in our array in this block of code here. So personally, I think this looks a lot cleaner for our initialization. Now, what about when we wanted to just print out everything in our array? Well, this here, we had to, first of all know how many elements are in it. Then we had to create a variable to iterate through. Then we had to increment it by one every time. And we had to also type out the name of our array held by an index colored by our iterator variable. Whereas here we just said for every integer in our array, we're going to set this value equal to each element and bam printed out. So personally, if it were me, I would do something. As I think this looks a lot cleaner. Now in this case, you can do whatever you feel more comfortable with. But personally, I think this does look a lot cleaner and it's also very clear that we just want to run this loop for as many elements that are in our array versus this, where Assad was just reading this here. It just says I less than ten. And then we can assume that that's how many variables we're in our array. This is very clear that there were going to run this for as many elements as there are in our array. And this would still be clear whether this here was a queue or a stack or a vector, et cetera. Now, let's go ahead and do one more thing with this here. So let's put back in our random statement here. So we'll say equals rand modulus a 100 plus one. Then down here, let's create a variable that we'll say the highest value. So we can say highest number. So this equal to 0. Then inside of our foreach loop, we can simply say something like this. X is less than highest number. Then we want to set highest number equal. Just like that. I remember this doesn't have to be x. We can call it whatever we want it. So for example, if I wanted to make this clear, they could say rename element, right? So if the current element in our array is less than the highest number, then highest number equals the current element. And now down here, right after our foreach loop, we can simply say something like highest number. And then before that, let's go ahead and put in some text. Let's say the number space. We're going to throw in another insertion operator right there. And when I code and the, and the reason that printed out 0, because this actually needs to say if the current element is greater than the highest number that we want to set, highest number equal to the current element. And there we go. Our highest number was maybe six. So we said the highest number of 96. So there's just another use case for a, for each loop. Now, we'll be using for each loops again in this course. So if you don't fully understand it, don't worry about it. Like I said, we will be using them again in the future. And let's go ahead and quickly recap adequate one. And basically how to interpret this line here. So you're going to say for all lowercase and then m parentheses, I have the datatype of the elements of the array. So we have an int array, so we said int, then you'll create a variable name, and this can be anything you want. So we just said current element, then you're going to have a colon. And then finally your container name, followed by close parentheses and curly brackets. I'm sorry. Basically saying for each element in our array, we're going to create this variable. And this variable is going to be set equal to each of the values in our array as it goes through. And it's going to start at index 0 and go all the way through to the end. Now, in the last video, if you remember, I said through the for each loop actually copies the array for its operation. Now, what did I mean by that? Well, let me show you. So if I go ahead and remove this code here for a moment, if we were to say something like current element equals ten, and then up here create an iterator. So if you said I equals 0, and then down here we'll say I plus, plus. And then print out our array at index i each time. So let's create a new line after. And then we'll go ahead and erase this line down here and run our code. You can see that everything in our array did not change. And that's because this current element here is just the copy of the values in our array. And if we were to print out current element here, instead, you can see that we're able to set that to ten and print it out. But when we had it, how he did just a minute ago. If we run our code here, you can see that it doesn't print out ten for each of these. And that's because current element here is just a variable that we want it to be equal to. Each element in our array. As I went through, the first time through, it was equal to my array index 0, then my array index one. But changing current element doesn't actually change the value in our array. So this variable here is just a copy of each of the integer elements in our array is not actually have any effect on the elements in our array. So there you have it. Hopefully that made sense. If not, we'll be working with for each loops more throughout the course. Anyways, that's it for this video. Thanks for watching, and I'll see you in the next one. 53. Chapter 10 (Foreach Loops): Auto Keyword: In this video, we're gonna be talking about the auto key word. So basically what that is is the auto keyword is essentially just a placeholder for a type, but it is not a type itself, meaning you cannot cast a different datatype to it. Now, if you are using the auto keyword, the variable must be initialized. And I'll show you what I mean by that right here. So if I was creating a variable down here and instead of putting in the datatype directly, I just sat on them. And then a, like this, I would get an error because auto variables need initialized. Now, let me quickly go over some of the useful things about using the auto keyword. And then we'll get into some examples and see where and when it may be useful. Now personally, I'm not a huge user. The auto keyword. And if you're coming from a another programming language, say something like C-Sharp e.g. and it's basically something like having the var keyword or something like that. Now, one reason you might want to use the auto keyword is because of its robustness. If the expression is type has changed, this includes when a function return type has changed, it will still work. And I know we haven't really talked a whole lot about functions, even though we have used to get handful of them. But you'll see what we mean by this in an upcoming video. Then there's the performance. And what I mean by that is that you are guaranteed that there'll be no conversion and you'll get the right type that you need. And I'll show you that here in a minute. Next, we have the usability. And how that comes into play is that you don't have to worry about type name, spelling difficulties and typos. So if you get into longer type names, and we'll actually be getting into one of these in the next video when we talk about maps, the nice thing with the auto keyword is it's a lot shorter and you're less prone to errors. Next, there's the efficiency or just typing this because it is such a short word, you're cutting can be more efficient in that sense. Now, let's go ahead and take a look at how to use the auto keyword. And like I said, it is essentially just a placeholder for a type, although it is not a type itself. And how it works is that when you write auto, our program here will automatically figure out what type that we need. So let's go ahead and take a look at that with creating some different variables. So if I wanted to create an integer, I could just say auto number equals five. And if I mouse over this, you can see that I created an integer variable. And if we made this into a float like this, you can see it is now a float. And if we made it into something like this, you can see that it automatically turns into a double. So like I said, you can basically just use this keyword to have it automatically figure out what type you need. Let me go ahead and change this back to an ink quick. And we're actually just going to call this directly. And down here, we're going to say something like auto number, Coffee equals number, just like that. And now this will automatically copy this value into here, just like we've seen in the past. And you can see that those automatically an integer variable. And this is one small and very simple example of when this might come in handy. Because now if we went ahead and change the data type of number to say a float and did something like this. You can see that this automatically becomes a float. We didn't even have to mess with our code down here. And then also, let's say we wanted to have some code for printing out an array, which we've done in the past. So let me go ahead and remove this quick. Right here, we'll say int my array. And then square brackets will say something like five. Just like that to create an integer array of five variables. Then let's actually go ahead and go to each of these K value. So let's say something like this. Alright, so now all five of them have an initial value. And remember if you want it, you can actually just remove from this number here, no, automatically figure out that we want five values in list. And then down here, we can print out everything in our array using a for each loop, which will automatically work regardless of how many items are in our array. We could say something like four x colon my array. Then we could just say C out X on the line. And after running code, as we know from the past, this is going to go ahead and print out everything in art right? Now, even though right here in such a small piece of code, it's very obvious that we're working with an integer array. Something we could do is just say auto, and now we don't even have to worry about it. This is automatically going to know what data type is in our array. We don't have to think about it. If we had a bigger piece of code here, we don't have to go back and check what data type we are using. We already know that right here. We want to put the data type of our container. Anyways, lot of times just saying auto is going to be easier, especially if we're working with a more complicated datatype, which you'll see in the next video. When doing something like this, this is probably one of my more favorite use cases for this keyword. I can just say auto, I don't have to think about it. I know I want the datatype from this anyways. And if you remember from up here, we know there's not gonna be any type of conversion. It's just going to get the exact data type we need. And that's what we wanted to put here anyways. And also, let's say down the line, depending on what our array here was for, We were like, You know what, I'm not gonna be able to use whole numbers for what I'm doing anymore. I need to use floats. Well, we could just change those to make all of these a float value like so. And you can put in whatever numbers you weren't here, not really going to make any difference. And hey, look at that. I don't even have to mess with my code down here. So that is fantastic. Now that being said, there is some shortcomings to using auto, and I just want to make you aware of those as well. Also, like I said, I don't use auto a whole lot personally, but I know there are a lot of people out there that do. And a lot of times if you're reading someone else's code, there's a good chance that you will see them use the auto keyword. And I've even seen some people just use auto and they don't even type in there, type at all. Those, just say auto and use that for pretty much everything. Now, personally, let's say we're creating a variable like this and x equals five. Now, I know this is an integer. I can tell that. I'm a very quick glance. I don't even have to think about it, but if I type out of here, well, yeah, I can probably deduce that this is an integer by checking this poll number here. Or if I had something like this where we say auto The equals x, well, I don't even know what b is unless I go over here and mouse over this just to find out that this is an integer because over here is also an integer. And I know that at the beginning I said, well, hey, if we were to change this from an int to a float, then we don't have to change this stem here. However, what if we had a method that required the original type? Now, we're going to have to go through and figure out what methods were all messed up by that. And keep in mind what the new datatype is, et cetera. And it can get pretty messy. So personally, I'd rather just see the type in most cases, but feel free to experiment with it yourself and see what you prefer to do. However, like I said, for cases like this, especially when working with more complicated data types where I know I just want the exact datatype of my container to be right here. Well, I might as well just say auto, it's a lot easier. I don't really have to think about it. And if for whatever reason my array type changed up here, and I'm just printing out those values anyways because I don't have any specific methods in here. Well, I can just say auto. I don't have to think about it. I don't have to worry about changing this later on. This is just going to work anyways. That's it for this video. I just wanted to make you aware of what the auto keyword is and what are this fork? Because I know for a fact, if you get into programming, you're going to see the auto keyword. If you're coming from another programming language, there's a good chance you've seen the var keyword and we're wondering if C Plus Plus had something similar. So there you go. So hopefully you got a good understanding of how to use the auto keyword and what it is for basically, you just throw it in our program here to figure out what data type we need on its own. And you don't want to have to explicitly say it. But if you want your data type to be explicitly written out, well, then you're just going to need to put it in there yourself. Anyways. Thanks for watching, and I'll see you in the next one. 54. Chapter 10 (Foreach Loops): Maps: In this video, we're gonna be talking about another container called the maps. Now, maps and C plus plus are a type of container that store elements and unmapped fashion where you have a key as well as a mapped value. Now the key value is generally used to sort and uniquely identify the elements in your container while the mapped value store the content associated to this key. Now, rather than confuse you with a bunch about maps, let's just go ahead and create one ourselves. And before we do so, you're going to want to go ahead and add in this header file being just map. So let's go ahead and get started. Now the syntax for a map is as follows. First you're going to say map, all lowercase, the less than symbol, followed by a data type which is going to be for your key. So just like this, then you're going to have a comma followed by your map datatype. So when the greater-than symbol, and after that you're going to have the name of the map followed by a semicolon. So as you can see it as so much similar to creating a vector or a queue or a stack. However, instead of just having one datatype, we now have to remember your key data type is usually to sort and to uniquely identify whatever your mapped data type is in these data types can be different by the way. So you don't have to store it, just ends. You could store it in and a string or essentially it whatever you want there. So let's go ahead and create a map. And we're gonna go ahead and base this off of the phone book in our case, just because everyone knows what a phone book is. So it'll be hopefully easier to understand that way. So down here, we're going to say map and then the less than symbol. And then we're going to uniquely identify every one in the phone book by name. So let's go ahead and just say string, just like that comma. And then everyone's number will be stored as a whole number. So we'll say int. We'll call this phone book. Just like that. So right here we have our empty map. And then down here we'll go ahead and add some elements to our map. Alright, so let's go ahead and add some elements to our map. And to do this, we're just going to say the name of our map phone book. Then we're going to use the member access modifier. So we'll say dot there, insert all lowercase by the way. And then in parentheses we're gonna put the type now because our map holds two different datatypes like it Does that are connected. This is actually called a payer. So a parent and C plus plus is essentially just a container that stores two values. And these values may or may not be the same data type, right in here, we're going to say phonebook dot insert. And then after that, we're going to say hair, all lowercase here. And then a less than symbol followed by the same datatypes of our map. So if you wanted, you could actually just copy and paste those. But we're going to take this out, just trying to practice here, just like that. And then in parentheses, we need to put in the values that we want to insert into the string and the string. Let's just put a name here, something like Jack comma, and then the integer value. And we can just throw in any ten-digit number that we want to represent their phone number. Then we'll just put a semicolon at the end, and let's go ahead and add a few more numbers. And instead of typing all this out again, I'll just go ahead and copy and paste that a few times. Then we'll go through each one of these eight different key name. And you're going to want this first value here to be unique to everybody. So you wouldn't want to put the name of Jack and multiple alleles. And if you're actually making a phone book, you would want to have a last name, e.g. but since we're just working with something small like here, you all get the point. So we'll say Jack, John, Pam, and something like that. And I'll give each of these eight different number. And feel free to throw on whatever numbers you want rather than having to copy what I have here exactly as it's not gonna make any difference for our example. Now, what if we want to go ahead and print out map? Well, we could do so using a for each loop. So if we're going to say and then we can say pair, followed by the less than symbol and then strain. And the greater than symbol. And then after that, we'll say person, space, colon space, our map name, there was a phone book and then an open and close curly bracket, like so. And then we can go ahead and print out each one, and I'll show you how we do this. So we're going to say CO, followed by person, because that's the variable that we're using to access each one of these. We're going to say diet. And then notice that it comes up with a first and a second. The first as this first value being our string, so that'll be their name. And then the second would be the integer value, or in this case, everyone's number. So we'll say person dot first, and then after that, we'll put in a space like so. And then we'll say person that second, followed by N line, just like that. And if we run our code here, you can see that all of our numbers are actually messed up. Now why is that? Well, like I mentioned before, an integer only can hold a certain range and then eventually a wraparound and go forward. We've talked about this in a past video there. So how can we fix this? Well, this is a great example of when you'll want to look for a different datatype to suit your needs and the errors that might come up if your data type is too short for it, the value you are trying to hold, in which case here, my highest value that I'm trying to hold his over 8 billion. And if I mouse over it, you can see that it assumes those value is a lung. Lung. And because we aren't gonna give anyone a negative phone number value, we might as well make this variable unsigned. So to fix this up here, we're going to say unsigned, long, long, just like that. And we're going to copy and paste this down for each of them. Like so, make sure you copy this for each element that we added, as well as an R for each loop. So we've now created a map the whole day string and an unsigned long, long, which can hold a very large positive whole number. And we call this map phonebook. Then we inserted multiple elements by saying the name of our map followed by this insert method. And then our datatype here was a pair which like I said, it's two different value types, or it can hold two different value types. I should say, as we could've made up both of these, a string or an int or whatever we want it there. But in this case we set a string and an unsigned long, long to represent a phone book. Then down here, we just created a simple foreach loop where we said for each pair of a string and unsigned long, long in our phone book or a container here being a map. In this case, we want to create a variable called person and set them equal to each element as we loop through. So we're able to print out person dot first, which will be our first value here and our pair, then we throw in a space and then also printed out person second, which is our second value here, followed by creating a new line. And if we run our code again, you can see that this now works just fine. And our numbers are what we typed up here because an unsigned long, long is able to hold such a large value. Now, notice how this datatype takes up a lot of space, and quite frankly, it looks a bit ugly. Well, there's an easy way we can fix this using the auto keyword that we just talked about in the last video. We can just say auto, and now we don't need to put in all that extra stuff. And if we run our code, you can see that it runs just fine without any issues. As auto keyword automatically figured out the exact datatype we needed. So how great is that? And it looks way cleaner than this, in my opinion. However, you can do it either way. However, just to show you something, we cannot put auto up here as this will not work. So here's an example of when you cannot use the auto keyword. And instead we need to put the value directly. So there you have it. There's a basic way to create a map, to add elements, and to use a for each loop with a map to print out the values within it. Now, before we wrap up this video, I'm just going to show you a couple of extra methods, but you can use with your map container. But as always, there are a bunch more. So feel free to look those up if you decide to use a map in one of your programs. So the first one we're going to look at is how to remove an element from our map. But before we do that, I'm just going to add in an extra comment here. Crank out map. And then right here, we're going to remove the amount column. So real quick, if you remember at the beginning of the video, I said that a map is laid out sort of like a actual map. We're going to have key values and then mapped values in this key here is not only to sort out the elements within your container, but also for locating elements within your container. So right down here, if we want to remove a element in our container, we simply need to know that key of the element. And let me go ahead and show you how that's done. So right here, we're going to say phonebook being the name of our map, dot erase. And then in parentheses, we're simply going to put the key value of the element we want to remove. So let's say John, for whatever reason, moved out of town and we no longer need his number in our phone book. Well, we can just put John in-between quotes because we're working with a string and put a semicolon at the end. Now, when we run our code, John will no longer be a part of our phone book. So right there, that is how you remove elements from a. And let me go ahead and show you something else about this method as well. So if we go ahead and copy this line here where we inserted John and or phone book and pasted like this and give this one a different color. Like so. And run our code. You can notice that neither John exists, and that's because this will actually remove every element with that key. There is another example of why you want to make sure that every element in your map container has a different key value. Now, let's go ahead and take a look at one last method that we can use with maps here. Now, this is just the last one I'll be showing you remember there are many more out there if you're interested. So with this one here is going to do is allow us to print out the phone number of the person that we are looking for. Another great example of why you want to give everyone a specific key. And I'm actually going to go ahead and remove this extra John that we added to our phone book here. So down here, we're going to say element based on. Then down below, all we have to do as a CEO phone book. And then in parentheses put her key value inside of quotes here, because we are working with strings for our key value, I can say, Hey, let's get Tams number, just like that. And we will print out Pam's number. And I'm actually going to go ahead and comment out our foreach loop just so it's not taking up space when we run our program. And if I run our code, hey, there we go. There's pimps number right there. So there you go. Hopefully you now have a basic understanding of how map containers work in C plus plus and have a base understanding of their functionality. Should you want to use it in a future program of yours? Anyways, hope you enjoyed the video. Thanks for watching, and I'll see you in the next one. 55. Chapter 10 (Foreach Loops): Iterators: In this video, I'm gonna be giving you a very basic or very brief introduction to iterators. And honestly, I wasn't even going to throw this in C plus plus for beginners course, but I see it so often that I just decided, I'm just going to throw it in there. So at least you guys know what it is when you come across it. However, at the example I'm going to give isn't really the best use case for it. But like I said, I do see it all the time. So I just want to make sure that you guys aren't completely thrown off guard when you see it really early on when you get into making your own programs and looking at other people's code for reference. So let's get started. So first off, what is an iterator? An iterator is an object used to iterate over elements in a container. Anyways, there's the basic idea of what an iterator is. It's like I said, it's just an adjective use to iterate over elements in a container, which is something we've done in the past. But now I'm going to show you how to do this using an actual iterator object. And I'll also show you how we can do the same thing using a for each loop, like we've done in the past, just so you can see the comparisons between them. And like I said, this really isn't the best use case for this. And normally I would just use a for-each loop. And you'll see what I mean here in a moment. So first off, we're gonna go ahead and create a vector. I'll type in like so. And then we'll just call this numbers and set it equal to the following. So in curly brackets here, just go ahead and insert any five numbers view. I'm just going to say 12345 for simplicity sake. And we have actually taken a look at how to iterate through this with a couple of different methods. So if you remember, we did this with a for loop, and we've done this with a for each loop. Now, to do this with a for each loop, if you remember, was simply say something like port number, colon numbers like so. And then an enter code block was print out number like that. And this will work just fine. This will print out everything in our numbers vector. But how do we do this with an iterator? Well, first, we're gonna go ahead and declare an iterator. And to do this, we're going to retype in the container type, which is a vector of type int followed by two colons. And then we're going to say iterator in all lowercase like so. And then after this we need a name and a common naming convention. Very common name if you wipe or an iterator is simply gonna be IT. And this alone is a declaration for our iterator. So I'm gonna come up that out and just call this iterator declaration for our numbers like that. And I'm just doing this for reference. So you guys don't have to copy this part if you don't want to. And let me zoom in here for you guys. But down below this emigrate and initialization for it. So iterated initialization, like so. We can declare and initialize it in the same line. So say iterator initialization in declaration for our numbers vector. And what we would do to do that is say container type, followed by the two Colin's than iterator, the name once again. And then we would follow that up with where we want our iterator to begin. Essentially, the way we would do that in this case is saying the name. We're container being numbers, died. Again. Now, that is the basic syntax to declare and initialize an iterator. Iterator syntax. Remember the syntax is basically just the rule for something, essentially is going to be the container. And then you're going to have to, Colin's like that. I'm going to say iterator, followed by the iterator name. I'm just going to pick here. And then if you want to initialize it, you're going to say equals followed by the actual container name dot. And then if you want to set it to the first value, at least you could say that began. However, there are other methods, so you don't have to start it at the very first element in the container. Anyways, I'm gonna comment out this one as well. And we're just going to start off with the declaration. So down here, we're just going to say vector into Colin's iterator, IT and will need to put a semicolon at the end there. Alright, so there we go. We have declared our iterator. Now, let's go ahead and create a for-loop. Thrill, our vector up here. And I'm actually just going to copy this code and move it down here so that we can keep up on screen here for you guys. And I'll remove this one from up here. And I might comment that out just so we don't have that actually show up on console there. And we'll create a for loop to work with our vector. So we're going to say four. And then inside of here, we're just going to simply initialize our iterator we already created. I'm going to say IT equals numbers diet. Again. Like I said, this is going to set our iterator to the very beginning value of R vector being one. And then after that for a condition, we're going to say essentially while IT, so start off with tackling IT here. Being our iterator is not equal to. Numbers and real quick and iterator actually just points to the location in memory of the elements in our container, which maybe kinda confusing. And we'll get into that a little bit more when we talk about pointers in the future. But for right now, just know that it basically points to the location in memory isn't the same thing as our actual element. Okay? So basically saying we want to create an iterator that starts at the very first value here. And then while our iterator is not equal to the last element or is not pointing at the last element. I could say we want to run our code. Now after this, we can just simply say IT plus, plus the name of a generator plus, plus. And this will work just fine. Now, this end of method here isn't actually the last value in our container. It actually is essentially the value after that, if it existed as we could look at it. So basically what we are not pointing to a value that doesn't exist. So it will start at this one. Here we go, 2345. And then once it is pointing at the imaginary sixth element, then it would be equal to this end. Well, it is not equal to that. We want to keep running our code. So hopefully that makes sense. Basically if there wasn't a imaginary sixth element here, this is what this here is referring to. So what we are pointing out, everything that is in our container and we want to do the following and iterate through it. Then we're going to have open and close curly brackets. And then inside of here, we're simply going to print now I create a new one. Now, notice we got this error and that's because we actually need to put an asterisk symbol before we say i t here. Now, why do we have this asterisk? And we'll actually get into this a bit more when we talk about pointers. But essentially, remember how I said an iterator is pointing to the location in memory where this element is. Well, this asterisk allows us to dereference that pointer. So instead of giving us back the location and memory, we'll get back the actual value. So hopefully that made sense. If not, we'll talk about more when we get into pointers later on in the course. But just know for now, we put this here so that we get the actual value and not the memory location address. So if we run our code here, and as you can see, we print it out 12345 on separate lines there. And if you're wondering how I zoomed in on the console or over in Visual Studio here, you can actually hold Control and use the scroll wheel to zoom in and out. However, on the console window here actually just increases and decreases the size of the window as a whole, which I guess resumes you're in a roundabout way. But anyways, we printed out 12345 on separate lines. Now, like I said, in the specific case, I personally would never do this. And a lot of times you'll even see it written where the declaration and initialization is on the same line. So if we copy and paste that down here, and we don't want to put the name twice. So just like that and remove those here. This will run just fine. And a lot of times this is how you will see it done as with the declaration of the iterator and the initialization of it on the same exact line. So that's how I'll go ahead and now, like I said, in this case, you'd probably never wanna do this because this for each loop that we have right here, works so much better. It looks so much cleaner. But like I mentioned in the beginning of the video, I do see this all the time. So I just wanted to make sure you guys know what is happening here. So basically, in our for loop, we're creating an iterator by putting in the container type here, followed by two colons, and then the keyword iterator, then the name of their iterator, which you will often see as IT. But you can put whatever you want followed by the assignment operator to initialize it. Then we put in the actual container name and give it a place to start. Because like I said, an iterator points to the location in memory where an element is in a container. So we're basically saying, Hey, just start with the very first element. Then we said, while our iterator is essentially not pointing to the element after the last one. So this is our last element. If it was pointing at a imaginary six element, That's what this number is bought and would be equal to. Well, we are not pointing at that. We want to go ahead and increase iterated by one, which will move us forward along our elements here. Then we want to print out the actual value that our iterator is pointing to and on a new line each time. And we've worked with for loops, so forth. So you should understand the basics. Basically, we're able to declare and initialize a variable here. We have a condition here, and then our variable update, which is where we updated our iterator in this case. Now, although this example isn't a great one and that's a very basic use of an iterator, as there are many other methods you can use with it to do different things. There are actual use cases where you would want to use an iterator. And that is because not every single container saves its data in a continuous manner where everything is in order and you could just use a basic four loop. And sometimes an iterator may be necessary. If you are working with a different type of container that doesn't store it to values in a sequential order, like a vector or an array does. An iterator may be something that you'll need to use and look into a bit more. But that is a basic overview of one, as well as a basic example of how to create and use one as an example, you may see quite frequently if you start looking at other people's code, especially anyways, that's basically it for this video. I just want to make sure that you knew what an iterator was and had a basic understanding of what using one might look like so that you aren't completely thrown off guard when it comes your way. Anyways, thanks for watching. Hope you enjoyed the video and as always, I'll see you in the next one. 56. Chapter 11 (Enums): Intro To Enums: In this video, we're gonna be talking about enums. And I'm just going to be giving you a brief introduction in this video. And then in the next three videos, we're actually going to be creating and working with them. Now, normally when learning a programming language, you're going to learn fairly early on. However, I've decided to hold them off till this point because for one, I didn't find it really necessary to throw out you guys too early on into the course. And I found them kinda confusing when I was first learning programming. So I thought I'd let you guys get a basic grasp on the language before we threw them in there. But you will see them very often. They do come in handy. So let's go ahead and get into what an enum is exactly. An enum, short for an enumerated type is actually going to be our first user-defined datatype. And you'll see what I mean by that in the next video when we go ahead and actually create one. And what an enum is, is basically a group set of constant values that have a name associated with them that you can use in your code. Okay, So is the user-defined datatype. And what it is is a group of constants with an integral value. Okay? So that means it's going to pull the whole numbers, or a whole number, I should say, with a name associated to it. And because these constants have a name associated to it, it makes your code a lot more readable, which is pretty much the main use case for it. And you'll get to see an example of this in one of the upcoming videos here. Now, I know we really haven't talked about constants in quite awhile. I'm just gonna give you a brief refresher here as well. So what is a constant? Well, it's just a variable that cannot be changed. So if you remember to create one, you're going to say constant and then you're just going to create a variable as normal. So we might say constant number and set it equal to five. Now something I didn't talk about when we last talked about constants was that when naming a constant, a lot of times you will see people and give it a name in all caps like this for some special prefix just to signify that this is a constant. So if later on in my code, I tried to say numbers for example, and I see, oh hey, it's an all caps or I see a specific prefix. I know this is a constant variable. I didn't mean for this to be changed ever, right? Because the constant cannot be changed. And if he tried to change it, you will get an error. So that is just something to be mindful of. That a lot of times you will see a specific effects or all caps in the name as just a basic naming convention. So that, you know, hey, that is a constant variable, not just my normal average variable that I can change out. Well, anyways, that is it for this video. So the main thing that I want you to take away from this is that an ENM as a user-defined datatype and the it is a group of constants with an integral value with a name associated to it. Now, when I say with a name associated to it, I mean with each constant integral value, not just the enum itself. And you'll get to see an example of that and one of the upcoming videos. As always, thanks for watching, and I'll see you in the next one. 57. Chapter 11 (Enums): Creating An Enum: Hello everyone. In this video, we're actually going to go ahead and create our first. Now, there are two different types of enums that you can create. But in this video, we're just going to be working with the most basic type. And then in a future video, I'll be showing you the other type that you can create. And it's not really that much more complicated. And we'll go over the differences and then as well. So how do we create an enum? The syntax for an enum and, or an enumerated type is as follows. First you're going to say Ina, then you're going to follow it up with the name of your income. And then in curly brackets here, you're going to have each value that you want associated with your ENM. And here you will actually put a string name and then it's going to have a constant associated with it. Now, this equals 0 isn't necessary when creating an anime. I just put this here to show you the constant that was associated with those first value. And then after that, this one is going to have a one associated with it and this one will have a two. And I'll just keep going up in order for as many values as we have. And I'll show you what I mean by that in a second. If you're a bit confused there. So down here, I listen, tax for them again, but I also included the constant value associated with each one. And we can even create variables of our enum, since it is a user-defined type by just saying our EM name followed by a name of our variable and setting it equal to one of the values that we have associated with our enum. Also, remember how I said an enum is a group of constant integral values. They each have a name associated to them. Well, if you remember, constants also need to be given a initial value right at the beginning. So we can create an enum and then initialize values later. We need to do it right away. So let's go ahead and take a look at how to create one ourselves. So down here, we're going to say, and then we're just going to call this one, for example. And a lot of times you may see user-defined types start with a capital letter. So we're gonna go ahead and do that for our naming here as well. And then in curly brackets. And just remember when I let you guys know different naming conventions, you'll just want to use whatever the company or whatever a majority of the people you're working with uses for. If you're just working solo, then feel free to just put your own spin on things. But anyways, so we credit any of them by saying Ina, thank giving it a name. And then in curly brackets, we're going to put in some values associated with our enum here. Instead of putting in actual numbers, we're just going to put it names of fruit in our case. So we might say something like apple, banana, comma orange, just like that. And there you go. We've now created an enum called through with three different values in it. And if you remember, I said that each one actually has a constant value associated to it, which by default is an integer. So if I mouse over this here, you can see that Apple is associated with the number 0, banana associated with one, orange to et cetera. And that will keep going. And afterwards Let's go. Haven't been another one and call this grapes and L1 will automatically be assigned three. You can also give these your own value. So I could say Apple is going to be equal to five, for example, then it will actually just count up from there if I don't change any of the others. So anything that does not have its own defined value will just go up by one from the last user-defined value. So Apple will be five now, banana, orange, seven, grapes, eight. And I could even say banana will now equal ten. And then it will just climb up from there with orange being 11. And someone. Now one thing to be aware of is that if I said banana equal to four, for example, because the orange will still be equal to five and there's not gonna be any issues there. They can have the same numbers associated with them, but just something I want you to be. Okay, So you can go to these each year on value and each one that doesn't have a value, we'll just start counting up from the last user-defined value. Just remember that arrays and other containers, these will all start at 0. Alright, so that is the basic way to create an enum. And one more thing I do want to show you is if you remember, I told you that these are naturally going to be integer values, but we can actually change that if we didn't want to, each of these to be a constant and remember it does need to be an integral value. So we won't be able to change this to something like a float or a double, but we can't change the integral value that these are associated with. So let's do that. After our enum name, we're going to put a colon followed by the datatype that we weren't granted. It does have to be an integral your datatype. So we could explicitly say int if we wanted, or we could say something like unsigned if we wanted to as well, or we could change the type entirely. So we can say char for example. And this will work just fine. It's now these all have a chart value associated with them, but you don't have to do that. You can just leave it as is, and they will naturally default to an integer. Anyways, that is it for this video and the next video, we'll actually be creating a basic program with and without any num so that you can see that true use-case for one and how to actually use one in code. So stay tuned for that as always, thanks for watching and I'll see you in the next one. 58. Chapter 11 (Enums): Using Enums: In this video, we'll be creating a basic program both width and without enums, so that you can see the true use-case for them. In the basic use case for E&M is essentially to make your code more readable and organized. So let's get started. So the program that we'll be creating in this video is going to ask the user to choose a fruit from a list of a number of options than once the user made a choice, we're just going to signify a confirmation of their decision back to them. So let's go ahead and get started. So first off, we're going to start by creating a list of integers named after different fruits and give each of these a constant value. So I'm going to say const apple equals 0. Nina was one. Orange equals two. And we'll just do one more. Let's say constant pair equals three. Then down below here, we're gonna go ahead and print out the list of options to the user. So I'm going to say C out. Please choose on the list options. Then we'll create an integer together the inputs so if they can and then blow that will say cn. Like so. Now we'll create a switch that will relate their choice back to them. And if you remember how to create a switch, we're going to say switch and then end parenthesis. We'll put the variable that we're checking. So we're gonna say switch input, and then we're gonna say tastes 0 colon. And then we'll say C out. And then close with a u, shows an apple. Just like this. And we'll say M line afterwards. And then we get a new shirt that we put on our break statement as well. Now, let's just go ahead and copy and paste those four times. Alright, and then down here, when I set this one equal to one, this is one of the two and this 123 as to match the values associated with our integers, then we just got to change the text inside of each case here. So for case one will say you chose a banana. Then down here, K is two. We'll say you chose an orange. And lastly it will say you chose a pair. And then up here, we still need to give the user a list of options. So we'll say C out and then quotes will say one. Apple. Then we'll create a line like so. Copy and paste this a few times here. Nina, orange and pear. And we actually made this up, this first 120123. And before we run our code, we actually want to create a new line, right appear. Now, if we run our code, we can see that we said Please do a fruit based on the list of options below, we have apple, banana, orange a pair. And if we type a two, for example, say you chose an orange, and there we go, our code is working. Now when it comes to this down here, especially if you're working with a much larger program, this isn't the most reasonable thing in the world, right? We're saying cases 0, and then we have to remember that this is associated with Apple, case one. We're going to have to remember that that's associated with banana. And although our code is fairly small and this isn't that hard to just check right here and read right here. We could make this a lot more readable with an enum. So instead of creating a new constant for each option, and then down here trying to remember the association with it, we'll just create an enum. So up here, we're just going to remove all these options. And then up here we're going to say enum, fruit. And then in curly brackets here, let's say apple, banana, orange. Then down here, we're gonna go ahead and change our integer to something like x right now. And then down here, we're going to say fruit being our user-defined datatype in this enum here that we just created, called this enclave and set it equal to x. Now, we're going to get an error because we can't initialize our variable to an integer like this, but we can actually cast this to type fruit. And this will work just fine. So if I run our code here, you can see that it works just as before. If I pick an option like three here, Let's say you chose a pair, and that's because input here is just equal to the integer value from this. But because this is of type fruit, we can actually just use these named constant values here. So down here, I could just say case, apple. And below that I can say banana. Then we can say orange and pear. And the reason this is giving me an error because I actually spell banana wrong. And if I run my code, and now you can see that this works just fine. And if I pick an option. 0 here, it'll say you chose an apple, and now we didn't have to go and remember any of the association with it. And as you can see, this is a lot easier to read. Now I can just say case, Apple case, banana case, orange case pair. Now, another nice thing about our enum here, having a group of constants is that we didn't even have to actually remember what these were. And I'll show you what I mean. So down here, I could have just said the case through the name of our enum here and then put two colon like so. And it would have actually at least some visual Studio given me a list of our different options here. And it also shows me the constant associated with it. So now I can just say case fruit was an apple. I can see the constant that I have associated with that and just select that there. And I can even just write it out as such. And this will work just fine. So this is fantastic. Now, granted here, we only have four options if we're working with a larger program and had a larger list of fruit, for example, this would have been a lot easier to work with. I could have just said through every single option I have inserted here and just made one for each going down the line. So like I said, the basic use case for an ENM as to make your code more readable. And although we just used fruit as a basic example, if you're making a game or something like that, had an ENM of weapons and gave each one a unique key and value identifier and done things that way just as another example. Now, another nice thing about this here is that if we were to change the value stored behind Apple here for whatever reason, let's say I change this to something like five. And this one too poor. Or what have you is that I don't have to change this down here at all and it will work just fine. And if I have any overlap, so for example, orange is five and Apple is five. It will also warn me down here that I have two cases that asked for the same number. So no problem. I can just write it here. Now down here, how do we actually been creating a list of options? And we didn't want to have to change this every time we messed with our constants up here, we could have done the following. So I'll just go ahead and remove the numbers from these. Then let's go ahead and put all of these in a sequential order. So we'll just remove all of these from here. And if we wanted, just for clarity, we could just say, hey, this one starts at 0, even though that will happen by default. But you will see some people do this. So I'll just go ahead and show you that as an example. And then down here, because it just print out apple. And if we run our code, we'll say 0 and then apple. So we could do this for the rest, like so. And then right here, we could simply say that space pair, as we saw orange manner and they invest we sample. Now if we run our code, this will actually just print that are options for us. And even if we had changed the values here, our code would still work just fine, which is really nice. And we can explicitly see what we are working with right here as well when we print out the different options. So up here, if I were to change this to ten, all my values are different and run my code. My options were dynamically change with it, and it is still very readable in our code as well. Anyways, that's basically gonna be it for this video. Hopefully you guys now have a basic understanding of how to create an enum and its use case being making your code more readable and more dynamic. In some cases, like you saw here, by giving you a group of named constants that you can use in your code and also giving you a user-defined datatype to work with as well. Now, the last thing I do want to show you quick is that because our enum constant names each have an actual value associated with them, you can do something like the following. So down here, I could say true. And I'll just put this in our case. I'll say apple equals through Apple. And by the way, I could have just said apple, and that would have worked as well. Then down here, I'll say fruit, banana equals just like that. And then I can actually create an if statement that works with the math behind them. So I can say if Apple is greater than banana, and then run some code in here, because Apple has a lower number associated with it being a ten versus an 11, then this code wouldn't run. But if I go ahead and turn that round, like so, let's say I bought it is greater banana. This will work just fine. And I'm actually going to go ahead and move this here quick to the top of our code, just like prints out without any issues, but below our enum here. And if I run it, you can see that it says Apple is greater than banana. But if we went ahead and turn this around, it would not. So I just want to show you that you could use enums in math just like you would if it was just a normal integral constant. Anyways, I'll go ahead and get rid of that extra code here. And then what I want you guys to do is go ahead and save everything we have here because we're actually going to use it in the next video when I show you guys the other enum type and the differences between that one and this one. So be sure to save what you have because we're going to use it in the next video. Anyways, as always, thanks for watching, and I'll see you in the next one. 59. Chapter 11 (Enums): Eums vs Enum Classes: In this video, we're gonna be talking about the other type of enough being an E&M class, which just has a few differences from a regular ENM like we created in the last video. Now, if you remember in the last video, I wanted you guys to save all the code that we created for use in this video. But if for whatever reason, you forgot, no big deal, the only main piece that I want you guys to hold onto right now is just this here being the actual creation of our enum. And I'll just go ahead and comment out the rest for now. So let me show you a couple other things about regular enums before we look at how to create an enum class and the differences. First off, if we tried to create a variable with the same name as any of our constants here, instead of our enough, we're gonna get an error. So if I say int orange, for example, equals 0 being tried to run my code, it's gonna give me an error. And that's because for one, a regular enum is considered on scope. So all of these names are essentially used up for our program. And you can see right down here that says fruit is on scoped and that it would prefer if we use an enum class over enough in this case. Now the other thing with regular enums is that an enum variable, we're implicitly convert to an integer. And let me go ahead and show you that right now. So down here, if I go ahead and remove this, okay. I'm not going to through all of my crew and set it equal to prove. Okay? And then down here, I can say x equals five plus minus. Then if I print out x, like so, I will get t plus. I could have just set n equal to my fruit. And that would have worked as well. And it will implicitly convert this value over to this. And if that's what you want, great. But if we made this an enum class, it would not implicitly convert like that. And I'll show you that right now. So how do we create an enum class? Well, it's pretty easy. And so the same enum followed by the name of her enum, enum class, followed by the name of our genome. Now, as you can see, we immediately got an error and it will not implicitly convert from our enum class here being fruit over to an integer value. However, we can still explicitly do it by saying, Hey, go ahead and make that an integer. That is just fine. If we run our code, we will still get ten. So by making it a class, you will not just implicitly do conversions from your enum type, two other types. Instead, you'll have to do it explicitly. Also, it doesn't take up these names. So I could have actually called this apple, for example. This would work just fine. That's because our enum is now scoped essentially. And these names are local to this class here, and we'll get into classes later on in the course. But for now, just know that now we can have two of the same name without any issues here. And this may be useful if we had a nother E&M class, for example, we wanted to have similar names. So maybe I had an enum class called food. Then inside of here, I had some different foods that I wanted to work with. I might say chicken, Apple chips like that. And this will work just fine without any issues, whereas it wouldn't have with regular enums. So there are some of the differences. Basically, if you use a name and an enum class, you can still use it elsewhere. Whereas if you use it with just a regular ENM, you cannot also, regular enums can implicitly convert to other types where as an enum class cannot and you have to do it explicitly, which will help guarantee that that is what you want it to do. So there you have it. There is how to create an enum class versus an E&M and you now know the differences. So as always, thanks for watching and I'll see you in the next one. 60. Chapter 12 (Functions): Intro To Functions: In this video, we're gonna be talking about functions. Now, a function or a method is essentially just a block of code that runs only when it is cold. So like I said, a function is essentially just a block of code, like we have a block of code here. The runs only when it is cold. And we've actually been using a handful of different functions for different things. For example, we just used a bunch of different functions when talking about containers in past videos. Also, pretty much all of our code has been written in this main function, which like I said before, every C plus plus program needs a main function because it is called right at the beginning of the program when your program starts and ends. And this here is the block of code that runs when it is cold. So why would we want to use and or create a function? Well, first off, it's going to clean up your code. One of the reasons a cleans up our code as because anytime you have, say, a block of code that you use, I don't know, three times or more or something like that. Instead of having that entire block of code copied and pasted multiple times, you can just create a function that allows us to easily use code over and over without having to copy and paste it. And another reason you might want to use a function as because for example, let's say we did have a block of code that we used over and over and over. And we went ahead and just copied and pasted it and copied and pasted it and copied and pasted it wherever we needed it. Well, what if down the line, you realize, you know what, I need this to do. Something slightly different now. Well, now you're going to have to go and find all of your code that you've used over and over and over and adjust it everywhere individually. Whereas if you had a function, you could just adjust the function. So it essentially works dynamically. Everywhere it is used. The code has changed because we can just change the function instead of having to change every portion of code that we've used over and over. Alright, so just to summarize a function and our method as a block of code that runs only when it is called into few of the main reasons you would want to use a function in the first place is to clean up your code to make it more dynamic. And so that you can easily use code over and over without having to rewrite it. Anyways, hopefully that gives you a basic understanding of what a function is and when you would want to use it. But if you don't fully understand it, don't worry because we're gonna be creating and working with different examples in the upcoming videos. So anyways, thanks for watching, and I'll see you in the next one. 61. Chapter 12 (Functions): Creating A Function: In this video, we're going to be creating and working with a very basic function. And then in the upcoming videos, we're going to be slowly working with more and more complex functions so that you can get a good grasp of how to use and create functions of your very own. And that's because functions are used all the time in programming. And personally, I find functions and, or methods, whatever you wanna call them. So useful that I won't even touch or work with a programming language that doesn't have this. And that's because it just cleans up your code so much. It makes it so much more dynamic, and it is a lot easier to work with and less error prone than copying and pasting code over and over and over. And in programming, you always want to do everything you can to prevent human error because everyone makes mistakes. And sometimes it can take a long time to figure out what is causing the issue. Anyways, enough with that rant, Let's go ahead and create a basic function of our own. So down here, outside of our main function, we're going to go ahead and create a function of our own. To start off, we're going to put our return type, and this is what you want to get back from the method. For example, our main method here as a return type of an integer. And that is why at the end of all of our programs, we return 0, which is an integer value. So when the main method is called, it has a return type integer and we return the integer back. And this must be done now in C plus plus, if you guys remember, you can actually remove this line of code. And C plus plus will actually do this for you, at least with your main method, without you having to have that line of code that specifically. But I just meant leaving it there so that you guys understand that, hey, we have an int return type, we need to return an integer. And also when our main function returns 0 and C plus plus, we know that all of our code ran through successfully and we were able to reach the end here. And we could have just put a 0 here as well. But we can also have this excellent success here if you want it to be spelled out more explicitly. So let's go ahead and actually create this thing. First, we'll start with a return type. In this case, we're going to say void. And what void means is that, Hey, we don't want to return anything from this function. We just want to run the code within it. And that is perfectly fine. And we will get into working with different return types and an upcoming video. But for right now, we're just not going to return anything from our function. So we'll just run the code within it. Next, let's go ahead and call this print. Just like that. We'll have open and close parentheses after. And then after that, we'll have our block of code, which once again is just defined with open and closed curly brackets. All the code within it is our block of code. Now, this top part here is our method or function declaration. So kind of like with variables, how we can do declare and initialize a variable. Here, we can declare a function. And then in our block of code is where we actually define our function. So down here, or block of code here would be our function definition. Okay, so this is our declaration and what is enter code block is our definition. Now, let's go ahead and actually put some code in here so we can see how this thing works and we can put whatever we want in here. But we're gonna go ahead and print out hello. Since our function is called print hello, then back in our main method, we didn't actually call this function or it's not gonna do anything. So as you can see, if we run our code right now, it is not going to print anything. And that's because, like we talked about in the last video, a function is a block of code that only runs when it is called. So how do we call our function? Well, we're going to say the name of our function, followed by open parentheses, then a close parentheses, and a semicolon. Now this isn't how you always call a function. And we'll get into some of the different ways when we start talking about arguments and parameters. But to call our function here, That's just how it is done. One, we aren't collecting anything from assumption, so I don't need to save anything anywhere and we don't have anything in our parentheses. So we don't need anything in our parentheses here. Let me simply put a semicolon at the end. And if we try to run our code, you can see that we get an error. That is because just like with anything else in C plus plus, you can't use it before it is defined. And one way we can fix this is actually by copying all over a code down here, and I'm putting it up above our main function. And now if we run our code, you can see that it will not work and print out hello. And let's go ahead and broken and M line here. And just to show you this, we can actually copy and paste those much as we want. And we can put this anywhere in our code and it will run just fine. And as you can see, we printed out hello For each time we call upon our function. Now, this is a pretty basic function that doesn't really do a whole lot, but you get the idea for one that's clearly states what we're trying to do. We're just going to print hello because we gave our method. Here are our function a very definitive name of what that function does. Also, if we had a lot more code in here, for example, if we're printing out like a paragraph or something like that, it would start to look very messy if we had a write that here every single time or copy and paste it as it would be a lot of texts for really no gain compared to just two. Something like this where we can just have our function name like sub t has remembered this here is our function. Anyways, that's pretty much it for this video. Like I said in the upcoming videos, we're just going to be slowly working with more and more complex functions until we break down all of the basic things that you'll need to create functions of your very own. So, to wrap up this video, let's quickly recap the main things that you'll want to take away from this first off, to create a very simple and or basic function, you can have your return type followed by the name, open and close parentheses, and then your block of code and curly brackets. And just like with anything else, our code is read from top to bottom. So it needs to know what our function is before we can use it. Also, if we don't return anything from a function, we can simply say void. Then to call a function or actually use it, we need to say the name of our function followed by open and close parentheses, at least in this case, because there's nothing in our parentheses here. And lastly, a semicolon. Now if you're still a little confused on how to use functions, don't worry about it. You're going to see some more examples in the upcoming videos. Now, before you go to the next video, please save everything you have because we're going to use it in the next video when we talk about the function prototypes. Anyways, that's it for this video. As always, thanks for watching, and I'll see you on the next one. 62. Chapter 12 (Functions): Function Prototypes: In this video, we're going to be creating something called a function or a method prototype. Now, to go ahead and show you what this is. If you remember in the last video, when our method, declaration and definition here, we're down below, we weren't able to call our method. And that's because our program had no idea what we were talking about. Because if you remember, code is read from top to bottom. So let's go ahead real quick. Can move this back down to low, our main function. Like so. If we go ahead and try and run this, you can see that we get an error. And it says print hello was not found. It has no clue what we're talking about. Now. We could just do what we did before, obviously, where we took our whole function and just throw it up above our main function. However, a lot of times when you're opening up your code, you just want to check what is in the main function. You should hopefully be able to have an understanding of how the code works and how to scroll past a ton of other methods potentially, depending on how complex your code is, can be really annoying and messy. So to fix this issue, we can use something called the function prototype, and this is pretty easy to do. So we're going to do is copy those were very top line here of our method. And then paste it up above our main method and put a semicolon at the end. And this is a function prototype. That's pretty much all we have to do. And now when our code is read from top to bottom, it's going to say, Oh, okay, there's a function called print hello, and the definition is just down here. Now when we call it, it knows what we're talking about. And if we run our code, you can see that it works just fine. Now. That is a function prototype. And if you want a more detailed explanation of what a function prototype is, it is essentially just like another declaration and it needs to have the return type, if any, the name along with any perimeters in order. And we'll get into bitters in a later video. But because we didn't have any here, we just put our open and close parentheses. And then lastly, you just follow it up with the semicolon. And this lets the compiler, or the thing that translating all this code into binary so the computer can understand it, know what to expect in the future. So when we have a function call later on, it already knows what this function call is referring to, being this function right here, the root declared and made it aware of ahead of time. And all it does essentially as long as to how our definition later on in our code. Because like I said, a lot of times when you're opening up your code, you just want to be able to look at the main function and understand what is going on. You don't want to have to deal with a ton of other texts beforehand. You just want to get to the meat of things. Anyways, hopefully that was pretty easy to understand. If not, we'll be working with these in the upcoming videos as well. Because I would always recommend just using a function prototype over putting your function over your main function, even though it will work the same, it's just a lot cleaner. So basically it just remember if you want to have your function definition later, which I would always recommend, all you really need to do is copy this very top line here, paste it above, and throw in a semicolon and you go anyways, that's it for this video. Thanks for watching, and I'll see you in the next one. 63. Chapter 12 (Functions): Return Types: In this video, we're gonna be talking about return types. Now, if you remember to create a very basic function, you're gonna start with the return type, followed by the name, open and close parentheses, and then your code block. Now, in the past videos, we just went ahead and said We didn't want her to turn anything in, inserted the keyword void. But in this video, we're going to actually be taking a look at how to get a value back from our function and creating a function that does this as well. So let's go ahead and get started. Now, to start off, we're just gonna go ahead and create our function prototype. So to do so, we're going to put a return type, which is going to be int, followed by the name, which is going to be returned ten. And remember, this can be whatever you want. By the way, the typical naming convention for a function is to use camelCase, just like we do with variables, but to start it off with a capital letter, then we're going to have open and close parentheses and a semicolon at the end, as this is our function prototype. Now, if I mouse over this, you can see it that it does warn us that we do not have a definition for our function. But as you can see, the squiggly line is in green, which basically means that your code will still run. But hey, this is just a heads up. Basically, if we run our code, we do not get any errors. The code does run just fine. So this is more of like a heads-up than an actual warning that a code will not run properly. That's why it is in green instead of red, at least in Visual Studio. So to actually create our function, we'll go ahead and copy all of this except for the semicolon there, and paste it down below. Then we'll have open and close curly brackets like so. And we'll put in our code here. And we can have whatever we wanted inside of our code block once again. But in this case, we're simply just going to return a value of ten. So today that all we're gonna do and say wait time, followed by ten and a semicolon at the end. And there you go. And if we mouse over this, you can see that this is indeed an integer value, then we just have our semicolon at the end. And if we want it to be even explicit about it, we could assert something like int ten equals ten and then return that variable directly. But what we had here is just fine. So just like with our main method, where it returns an integer using this return keyword. I remember this here is essentially the same as saying return zero. So all you need to do to return a value from a function. We just put in the data type that you want to return before the name of the function and then use this return keyword and all lowercase there, followed by the value which you want to return. And you'll want to make sure that this datatype is the same as your return type. Then up here, we can go ahead and collect this value. Now, just to show you something here, if we went ahead and call their function by once again saying the name of our function, followed by open and close parentheses. Since we don't have anything here, we don't need anything here. Then putting a semicolon at the end and running our code, you can see that it runs just fine. However, anytime you have a function that returns a data type, you want to store that value somewhere. Because if you aren't going to store that value, you might as well have not returned anything in the first place. So up here, we'll say something like int x equals return ten. And then down below, we'll just print out x, like so if we run our code, you can see that it is now equal to ten. That's because we set x equal to the result of this function. And because it returns an integer, we get an integer back. So all we did was create a variable called x and set it equal to whatever our function here returns. And if I mouse over that function, you can see that it returns an integer value. So x is going to be equal to the integer returned by our function here, which in this case is. So this line here is the exact same as if we said x equals ten. At least in this case, we can simply work with our variable as normal. So there you go. You now know how to use a return type when creating a function. Basically, instead of saying void here, you put the data type you want to return. Then in your function, you say return followed by the value of the data type that you want it to return. Now, whenever you call that function, it will give you back at that value of that type. And that's why we were able to do the following without any issues. For those of you who remember, x here is a local variable because it was declared within a code block, is only available within that code block after it was declared. Meaning we could have said something like this down here. X equals ten, and then said returned X. And this would run just fine because as far as this code block is concerned, x here doesn't exist. And as far as this code block is concerned, this x down here doesn't exist, and therefore they can have the same exact name without any issues. Just something to note. You always want to make things only as available as you absolutely have to. If we didn't have to make an x here, a global variable, e.g. then you wouldn't want to do that. You just want to make it as available as you need. So for one, you don't have problems where you have variable names overlapping. And secondly, you are also minimizing human error. I'm making a variable or whatever it is, only as available as it needs to be, which is always a good thing. Now, to show you one last thing here, because this can be essentially any data type that we want. We could return something like a cool, by the way, notice that if our function prototype here had a different declaration than our actual function, we get an error. So you'll wanna be sure that you give these both the same return type, like so. And then inside of here, we could say something like pool is true, equals true, then return true. Now, this essentially is the same as if we didn't have this and just say return, true. I'm just gonna go ahead and do it this way, just to show you that we could just throw the variable region here. And then up here, we could have had an if statement, if returned ten. And we probably would have wanted to rename this to something like return true. Just to make it clear. This function does when just reading our main method. Because you don't always want to have to concern yourself with the code within the function degree. You just want to be able to, like I said in the past, read through your main function and have a general understanding of what is going on. So over here, we said f return true. Notice when calling this function, because it was an if statement condition, we didn't need to put a semicolon here. And then down below, we can put a curly brackets if returns true, run this code and we'll just say C out if statement or something like that. And if we run our code, you can see that this works just fine. And if we had changed this to false e.g. and run our code, it would not have ran. So when returning a Boolean value, you can actually use it directly in your if statement as the condition here as well. So that's pretty neat. So remember, a function is there to make your life easier. And so this is essentially the same as just having the Boolean value here, since it returns a Boolean and when we returned an int, it was the same as just having the int value there, since it returns an integer. So there you have it. Hopefully you now have a basic understanding of how to work with return types. If not, you can always re-watch the video. Otherwise, you'll be seeing more examples of it in the upcoming videos. So thanks for watching, and I'll see you in the next one. 64. Chapter 12 (Functions): Parameters & Arguments: In this video, we're going to be taking a look at parameters and arguments. Now, in the past, we've been creating some basic functions that looked like this. They had a return type followed by the name, open and close parentheses, and then our code block and curly brackets. But inside of these parentheses, we can actually have the list of what are called parameters. So we could say if parameter one, parameter two, et cetera. And we can have as many as we want it. And what these are our values that we plan to pass into the function. So we can actually send data to the function as well as getting data back from the function. So let's go ahead and take a look at how to do this. So down here below our main function, Let's go ahead and create a function that adds two numbers together and gives us the result of that. So to do this, we're gonna go ahead and cite it in this case. And then we'll say add for our name. And then in parentheses we're going to say int x comma y, just like that. And then we'll have our code block and I'll go over why we did this here in just a moment. And then inside of our code block will say return x plus y. Now, we could have created an integer variable called result and then return the result. But I just want to show you that we could have an expression in our return statement here, because both of these are integers, it is going to return an integer. And so now when we actually call our function, we can do the following. So first, let's go ahead and print out the result of this function. So to do so, we're just going to say C out by our function name here. And then in parentheses, you can see that we need up put in two integer values. And the values that we put in our function call here are called arguments, whereas these down here, we'll call the perimeters. So in here we can say something like 510. And then these numbers will be sent to our function here, then be added together and returned to us as an integer. And then before you run a code, because our method is down below our main function here, we just need to create a prototype. So we'll copy this top line, paste it up above, and put in a semicolon. So now we have a function prototype. We have a function call here, and we have a function declaration and definition down here as well. So if we run our code here real quick, you can see that it prints out 15. That's because we said, Hey, we want to print out this integer value that we get back from here. That was our return type here. Then we pass in two integer values as arguments for these parameters. And all we did was we said, Hey, return the first one plus the second one. And by the way, the arguments that we pass in here are the values that we passed in here need to be in the same order as these down here. So this ten will never take the place of x, and this five will never take the place of y. They will be in order, and the same order that they were in down here, they needed to be in that order up here as well, which is why a lot of times it is just easier to copy this first line and then add a semicolon at the end when creating your function prototype. Also, just to let you know these parameter names are local to this function. So you could still create an integer x up here and an int y, and it will run just fine. Okay? So they are local to the function that they were created. So basically to create a parameter, all you have to do is have the datatype followed by the name. And all this name is as the name that you want to work with for the value passed into your function. So you'll create a perimeter whenever you want to pass something in to your function. And what you pass in is called the argument, and it is associated with the same parameter in order. So we have 510 here, which will be associated with X and Y respectively. So this five goes for x and the time was for why? Like I said, we got to name these whatever we wanted, even if we had created two variables up here. So if we had wanted to, we could have said int x or num one. Num two equals ten and then passed in num one, num two. And this will run just fine. Like I said, this parameter name here, there's just what you want to call the value or the argument passed in within your function. And they could have had the same name if we wanted it to like that and that would have worked as well. But like I said, you can call this whatever you want. So we just had x and y were the names we want to work with for the values passed in so that we could use it in our codebook. Also, another thing to note is that this parameter is a copy of this variable and not the variable itself. So if and here I said x plus plus, and then up here we printed out num one. Num one would not be changed. So we printed out 16 because we passed in 510. Then we increased x by one, which was equal to five originally. So five to 66 plus 1016. And then we printed out num one, which was still fine. And that's because, like I said, this perimeter here being x, just a copy of the argument passed in, which was num one. And loss of case, meaning changing x doesn't actually change the variable, which is also why if this was a constant, for example, this will run just fine because we didn't try to change the constant, we just change the copy of it being. So hopefully that all made sense. The main thing I want you to take away from this is that you can pass values to a function by creating a parameter I just stating the datatype followed by a name you want to work with within the function. And then the value that you passed in is called the argument and will be copied for use within your function. Then you just need to make sure that any parameters you have here are also placed in your prototype, and they are also now required when you call the function. So we could not have said add num one as we would get an error saying, hey, this actually requires more arguments, right? So there you have it. You now know how to work with parameters and arguments in C plus plus. Now I'm gonna go ahead and remove the CEO num one, and remove it for this constant here, like so. And what I want you guys to do is actually save everything we have right here, because we're going to use it in the next video Anyways, thanks for watching, and I'll see you in the next one. 65. Chapter 12 (Functions): Optional Arguments: In this video, we're gonna be talking about optional arguments. Now, if you remember in the last video, I asked you guys to say that everything you had so that we could use it in this video here. Now, if for whatever reason you didn't watch the last video or you forgot to save, feel free to pause the video and copy all of this. Anyways, let's go ahead and take a look at optional arguments and their use cases. So to make an argument optional, all we have to do is down here in our function declaration is set the parameter equal to something. But when we do this, we have to start with the rightmost one and work our way left. So let me show you what I mean by that. So I can give a default value of 0. So we'll get an error here. Same default argument, not at the end of the parameter list. And so that is why you always have to start with the rightmost one. So as you can see, if I set y equal to 0, this will be just fine. And then we can work our way left through any of the parameters we want to have a default value. And this will also make them optional. So now up here where I said add num one, num two, I can just remove these entirely. Then we have to do one last thing before we run our code so that we don't get any errors. And that is we either have to take our full method here and put it at the top, like so. And this will run just fine. So if we run our code here, you can see that we printed out one. And that's because we have this x plus plus right here. But if we go ahead and remove that quick, you can see if we print out 0 and we didn't have to pass in any arguments as the arguments were optional because our parameters have a default value. Now the other option would be to leave our function where we had it. Just like this. Remove this default value for our parameters in our function declaration down here, and instead put it in the function prototype. And no, you can not have it in both. So if you do have a function prototype, that is where your default values need to go. And there are different reasons for that, which I'm not really going to get into. But just know if you want to have a default arguments, you need to give your perimeters aid default value, starting with the one furthest to the right and working your way back for as many default arguments as you'd like. And if you have a function prototype, these default values need to go there and not into our function definition. And now if we get rid of those here again and run our code, we can see that we will print out 0. We can change these values to whatever we wanted, like y equals five, and now it will print out Hi. All right, so let's take a look at a better example of when you might want to use those. So let's get rid of our function here. And then we'll also get rid of everything in here as well and remove our function prototype to start with a blank canvas, like. Then up here, let's create a prototype for this function. And what we're gonna do, let's say void, Text, open and close parentheses and a semicolon at the end. And then inside of here, we're going to take a string as an argument. So we need a string parameter and let's just call this text like so. Then let's go ahead and create the actual function declaration and definition down below. So we'll just paste that down here without the semicolon and create our code block. And then we'll just say CL. And then let's go ahead and give our text here a default value. And because we have a function prototype that's going to have to go up here. So right after texts will say equals, You didn't send a string. The function. So this will allow us to call our function, but give us a warning that we didn't actually input any text. Now, in this case, we could have just not had a default value and then it wouldn't let us call the function at all without inserting text. But we're just gonna do it this way just to kinda give you an example of how you could use those. And then down here, we'll just call our method. So we'll say print text. And then let's go ahead and actually send some text here. So we'll say print text, hello, world, semicolon at the end and run our code. And as you can see, we've printed out Hello world. And if we didn't put anything in here and around our code, it's going to now say you didn't send a string to the function. So there you go. You now know how to create optional arguments. And to summarize, you can do this by giving one or more parameters, a default value starting from the right and working your way up left, either in the function declaration here or if you have your function prototype, then it needs to go there. And by the way, just a heads up the reason we have to start from the right and work our way left outside of the fact that we will get an error and it won't let us run our code is the following. So let's say after this, I went ahead and took an integer and we'll just call it x and set it equal to 0. And then we'll go ahead and remove the default value for text here. And down here we'll say x, like so. Then down below we'll say C out texts, and we'll also print out x like that. And let's just go ahead and rename those to the end. And if we wanted just to be more explicit here, okay? And now if we start putting in arguments here, it automatically goes from left to right because remember these have to be in order. So when I put in a text like low. And so now this hello string argument here will automatically go to our first parameter. But how did allowed us to do it this way? Well, then we wouldn't have any way to give x a value without giving text a value. Anyways, sunset is done in order. Anyways, hopefully that all made sense for you guys. If not, you guys can always talk to me down in the discussion section below, and I'll get back to you as soon as I can anyways. Thanks for watching, and I'll see you in the next one. 66. Chapter 12 (Functions): Overloaded Functions: In this video, we're gonna be talking about something called function overloading, which is where you have multiple functions under the same name but different arguments. And I'll show you why you would wanna do that with an example here. So let's go ahead and create a function for adding numbers together. So down below here, we're going to say int. And then NR parenthesis, we'll say int x comma y, just like so. And then in our code block here, we'll say return x plus y. Let's also go ahead and put our function prototype at the top. So we'll just copy that, paste it up above, and put a semicolon here. And just remember if we wanted some default values here, they would have to go up here. So just copying and pasting, it wouldn't work. We'd have to throw in something like x equals 0 and y equals 0. Now, let's go ahead and overload this function to take a different set of arguments. So to do that, we're going to go ahead and just copy this method and paste it down below, just like that. Then we'll say comma and z. Remember we could call this whatever we wanted. And then we'll say returns x plus y plus z. Then we'll copy this top line here, paste it up above, and put a semicolon at the end. And there you go. We now have two separate functions and their respective function prototypes that has the same name but different arguments. And this is called function overloading. Now up here in our main function, I can say something like CL, add the name of our function. And in Visual Studio here at least you can see that I have two options. And if I mouse over this, it says plus two overloads. But it does warn me that no instance of the overloaded function named add matches the argument list. And that's because I didn't put anything in here. But now you can see that I have one add function that takes two into values, and I have a second add function that takes, meaning. I could say something like five comma five. Make sure to put a semicolon at the end here and run my code. And you can see that we've printed out ten, and that works just fine, no issue. But I could also throw in a another integer value now and look at that. We printed out 15. So this add function is a perfect example of when you might want to do something like this. And I can make as many of these as I wanted. So then I could just use this add function to add as many integer variables as I would like. Another thing we could have done is use a different datatype entirely. So let's go ahead and copy our add method here again and pasted them below in-between the other two, like so. And instead of saying enter all of these, we're going to go ahead and say float. Then we'll go ahead and copy our top line. Paste it up here to create a prototype. Don't forget the semicolon at the end. And now I can put in to float variables in this would work just fine. Like it's like a 5.5 comma 3.3 or something like that. And run our code. Now we'll get 8.8, just like that. So that's pretty neat. And this is once again a perfect example of when you would want to use function overloading, because now I can have one function just called add. I don't have to remember a bunch of different function names. And I can throw in floats, I could throw an inch. And I can also have different amounts of numbers or variables that I'd throw in there. And I'm not going to have any issues whatsoever as long as I've created a declaration and definition for each argument set that I would like to use. And if we wanted, we could have even gave one of these, like our first one here, default values. So up here, I could have said x equals 0 and y equals 0. And this will work fine as well. So now I could have nothing in here, or I could have created another add function that just didn't take arguments in that case. But you do want to be careful when you do this, because if I had done the same with this one here, then it might not know which one I was talking about in the first place. So had I done something like this all the way through? Well, now it no longer is sure what I'm talking about because now this add function call would work for more than one instance of the overloaded function. So this is an issue granted in this case, they're all set to 0. So who cares if we printed out 0? But you will get an error obviously. So our code will no longer run until we fix that problem. So just wanted to give you a heads-up on that. Anyways, that's it for this video. Hopefully you now have an understanding of how overloaded functions work and when you might want to use them. Thanks for watching, and I'll see you in the next one. 67. Chapter 12 (Functions): Recursion: In this video, we're gonna be talking about something called recursion, where something basically calls upon itself. And we're gonna be doing this with a function that will give us the factorial of a number. If you don't know what that is, let me go ahead and show you quick. So factorial of a number is something like this. So let's say we gave it the number five, E factorial 45. Here would be the following. It would be five times the number below it. So four times the number below that being three times two times one. And this would give us the factorial of five. So you're basically going to multiply the number that you put in times that number, times one below that, all the way till you get the one, multiply these altogether and that would give you your factorial. And we'll actually go ahead and use five in the method will be creating just so you know that it is working properly. So if we go ahead and do this in a calculator here and say five times, four times three times two times one, you can see that we get 120. Alright, so let's go ahead and create a function that does those. Now there are a few ways that we could replicate this in code, but we're gonna be doing this with recursion where we have something called upon itself. So let's go ahead and create an example of this. So down below here, we're gonna go ahead and create a method that will give us the factorial of any number we insert into it. So down here, we'll say enter and we'll call this factorial. And then in parentheses, we'll say number. Then in our code block here, we'll say if number is greater than one, we want to do the following, which is return number times. And then we're going to call upon the factorial method again, like this. In here, we'll say number of minus one. And then down here we're going to create an else statement like so and say else return one. Now, this might look a bit confusing, but we're going to create another method that does the same thing without recursion. And then I'll try and break up both of them down the best I can. Hopefully it all makes sense on how this is working in the background there. But first, let's go ahead and create our function prototype before we forget. So I'll just paste it up above and thrown a semicolon. Now down below, this will create a another method for say, n factorial. We'll just call this factorial two, for example, say int number. And this is a perfect example of when you might just want to give it a variable the same name as another, because it is very readable. They're not connected at all. Ended scope is enclosed within the function, so minor. And then inside of our code block here, we're going to say the following factorial. One. Then we'll create a for loop and say int I equals one, while I hear is less than or equal to number I plus, plus. And then in curly brackets will say factorial times equals I, like. So. Then outside of that, we'll go ahead and return. This should do the exact same thing. Then, before I forget, let's go ahead and create a prototype of it like that. So now down here, let's go ahead and call upon both of these methods. So say CL factorial line. And then below that average ACL factorial to N line. Don't forget the parentheses here, like so. And then we'll throw in five for both of these. Okay, so I'm gonna zoom out here clinics, you guys can make sure that your code matches mine here. And let's go ahead and run this. So as you can see, we have 120th for both lump, which was the factorial for HIV. Now in all reality, neither of these functions look too bad, but this is just an example of using function recursion to complete a task. So what is actually happening here under the hood as this may look a bit confusing to somebody you well, first off, we said if number is greater than one, so if we had the example of five here, well, is five greater than one? And the answer is yes, it is. So what we're gonna do is return number being five times the factorial of the number below it. What this is going to return an integer. But when that one runs through, it's going to say is for greater than one. Yes it is. So now we want four times the number below that, and that is going to keep happening until eventually we reach a point where number is one. And then it is just going to return what? You need to make sure that I've eventually this recursion here, we'll stop and that this won't happen infinitely. Otherwise, without getting into too much of the specifics here are getting too into the weeds here. You'll run out of memory and you're gonna get some issues because you are creating a, another function within a function, within a function. And this just builds up and builds up, and then eventually you run into problems. So let's go ahead and take a look at what is happening. So to do this, let's go ahead and start with the very last case. So at the end of all this, we're going to have a function that says, okay, we want to return two times one, because that will be what comes back from the function here. Once this is equal to one, because of number is not greater than one, because it would be one, it will return one. So at the end will be returning two times one. Then the function that was trying to get the result of this equation here, well, I have three times, three times two, which then went down to two, one. Then the function above that. We'll have four times three, like so. And eventually we'll have our five times the result of all of this. So essentially now we have the function that just returned one. Then we have a second function that did this math here, a third than a fourth fifth function. So now created five functions alone by just inserting this five here. And so what will happen is that first function, we'll just return one. The second one will return a value of two, the next one a value of six and then 24. And lastly, five times 24 gives us 120. So there you go. That is how we got our final answer. Now, hopefully that made sense. If not, feel free to just go online and look more into recursion and other examples and work with it and really break down what is happening. Now, like I said, you do want to be careful that you aren't creating too many functions when you do this. And you also want to make sure that this won't happen infinitely for the same reason that you will run out of memory at some point. So in this case, this here was our stopping point where once number was no longer greater than one, we just returned to one. And this stopping point here is called your base case when it comes to recursion. So there you go. Hopefully you now have a basic understanding of what function recursion is and know how to use it and things to be careful of when using it anyway. So that's it for this video. Thanks for watching, and I'll see you in the next one. 68. Chapter 12 (Functions): Arrays As Parameters: In this video, we're gonna be talking about passing arrays as argument. But before I do that, I do want to mention one thing. So we've already talked about return types and functions, but I do want to let you know once you have this return statement and you pass something back to the color, whatever is calling your function, no code password is going to run. So it's kinda like just breaking out of innocence. So right here, I'm going to say CL, hello, like that, and run our code. It's not even going to read that. It's not going to print that out because we've already had a return statement. So when you do create a function that has a return type, note that once you have a return statement, it is going to stop reading the code and your function and just go back to the caller or the place where you call your function in the first place. So if you have multiple if statements or something like that, you could have multiple return statements and that will work just fine. Just wanted to give you a heads up on that, that a return statement could also be looked at as a break statement that returns a value. So let's go ahead and take a look at how arrays are passed as arguments. First, let's go ahead and create an array. So we're going to set my array in square brackets here. It'll just give this a size of five. And let's just go ahead and give it five values right off the bat here. So we'll say 1234. And then let's go ahead and create a function that will print out an array. So we'll say void, print array, like so, then enter parentheses here, we'll create a parameter that takes an array as an argument. So we'll still have to have the datatype like normal. Then we'll have a name here that we want to work with within our function like usual. And then we'll have open and closed square brackets to signify that this is an array and not just an integer. And then down below, we'll go ahead and create our code block here for our function. Then down here, we'll go ahead and print out all of the elements of our array. So we'll say for I equals 0, I less than five, I plus, plus. And then in curly brackets was a CEO. And then our parameter name being ARR than n square brackets will say, I will create a new line. And now if up here in our main function, which you could also look at as our color, since it is what is going to be calling our printArray function. We can say print array and then in parentheses just pass in my array. So I can put a semicolon. Notice that we do not put any square brackets here in our argument or any numbers. Now before I run a code, we simply need to make a function prototype here at the top and go ahead and run it. You can see we print it out 12345. Now, notice that we went ahead and put the size right here explicitly. And this isn't great because isn't very dynamic. But the problem is we couldn't put a size as our argument here. So to fix this, what you'd want to do is say comma in size or something like that. Remember, you can name this whatever you wanted and then put less than size here. And up here, we'd want to change our prototype. And then we put the size right here. And this would work just high. As you can see, we printed out 12345. And this here is the best way to pass an array as an argument, because by having the array and then these size a separate variable as this will work and dynamically now. So now if we set our array is going to have a size of seven, for example. And then I'm here. We've got two more elements like that and changed it here. Our function will still work as we want it to. Now something you can do is put in a size right here, like so. And this will run just fine as well. The problem is with doing this is that if our array size changes here, you won't be able to access these other values because it won't know what you're talking about. So if down here, I said CL are being the name of our parameter here, and then put seven, for example and run our code. You can see that we get some really weird answer. And that's because as far as it's concerned, our array has a size of five. Can we just asked for the seventh element and we're not going to get really into why this happened. But just know that even though this was possible, what we did before where we just had a blank size here. And then a separate size variable is the best way to do it. Now the other interesting thing about using an array as a parameter as that it is actually passed by reference and we'll get into references later on. But basically, when we were working with, with variables as parameters, it was passed by value and we were creating a copy of it. I demonstrated in a previous video, but with arrays, they are passed by reference. And that's because N C plus plus you cannot pass a array by value and create a copy of it. And we're not gonna get into why that is, but essentially because this is passed by reference, we actually aren't creating a copy of this array, and we are essentially working with the array directly. So now, if I said ARR indexed 0 equals ten, like so at the end of our function here, and then print it out. My array indexes 0, like so, and run our code, you can see that we print out ten. And that's because even though we were working with our parameter name here, because arrays are passed by reference. Like I said, we'll get into references in a future video. We're essentially able to work with the original rather than a copy of it. So if we change something here with our perimeter, we're changing the original as well. So basically the main things I want you to take away from this video is that if you want to use an array as a parameter, you're gonna do it the following. You're going to have the arrays datatype followed by a name that you want to work with. And then just use your square brackets to signify that it is an array. You'll want to have a separate variable for the actual size of that array. And also arrays are passed by reference, meaning that when we change something using our parameter name here, we are also going to be changing the original. So doing some planning, this changes the original as this here is not a copy like it as when working with other data types. So just to show you that again, if we said x equals seven and then pass in x as our integer variable, and then change the value of size here by saying size plus, plus, and then print out x down here. Let's go ahead and throw that on a new line so that it's a little more obvious. You can see that we still got seven. And that's because other datatypes when used as an argument are passed by value. And therefore size here is just a copy of that value and will not have any effect on the original. But a race specifically are passed by reference, which basically means we are going to be working with the original and not creating a copy at all. So when we messed with our perimeter here, we were also messing with the original. Hopefully that made sense. If not, don't worry about it, we'll actually be talking about references in the next video. So the only thing you really need to take away from this video is how to take an array as a parameter. First, you want to say that you are taking an array using the square brackets along with the datatype of that array, and you'll give that parameter name as usual, then you need to have a separate variable for the size of it because it cannot be aston with the argument here. So we just need to put it in separately. And lastly, just know that arrays specifically are passed in by reference, meaning that if you mess with the parameter here, you are essentially messing with the original. Anyways, hope you enjoyed. Thanks for watching, and I'll see you in the next one. 69. Chapter 13 (References): Intro To References: In this video, I'm gonna be giving you a brief introduction to references. Alright, so I referenced is an alias or another name for something else. So for example, a lot of people have nicknames. So let's say your name was Jonathan. People might call you John, and they're referring to the same thing. So in this example, John would be in a reference to Jonathan, were talking about the same person. And that's basically what a references in C plus plus as well. So for example, we might have an integer called x, and let's just say it's equal to 0. And then we might have a reference called nickname or something like that. Just as a terrible example, that is equal to x. And now we can just say x is nicknamed here, and we'll be talking about the same thing. And if we set x nickname equal to five later on. So if down here I said x nickname equals five. Now x would equal five because I'm referring to the same exact thing. So a reference is just an alias or another name for something else. And we'll get into some examples, as well as how to actually create one for herself in the upcoming videos and look at some of the different use cases for references so you know how to use them in your own programs. Anyways, I hope this all makes sense and as always, thanks for watching and I'll see you in the next one. 70. Chapter 13 (References): Creating A Reference: In this video, we are going to be creating a basic reference. So let's take a look at how this has done. So the syntax for our reference is as follows. First you're going to have the datatype. Other thing that you'll be referencing. Then you're going to follow that up with the ampersand symbol like SO, followed by the name of the reference. And then you'll set it equal to the referring name, which is a thing that you are referring to. Okay, so let's go ahead and actually create one right here. We'll say int x equals five. And then below that, we'll say int ampersand. And we'll call this X equals X. Okay? So we have the data type of the thing we're referring to held by the ampersand symbol, then the name of our reference. And you can call this whatever you want, followed by the name. Now if you remember in the last video, I said a reference, so there's just an alias for talking about the same thing. So now we can use X-ray or our reference name here to do anything with x that we could by just saying x. And so that means down here, I could say something like x ref equals ten. And if we go ahead and print out x now, like so, it will print out ten. And we could have even just print it out are referenced directly. And this would also print out ten. So a reference as just an alias or another name for the thing that it's referring to. This and this are essentially the same thing. Now, also one thing I do want to mention is that you cannot declare a reference and then initialize it later. Needs to be done on the same line, similar to how constants, for example, work, because you can also not change your reference later either. So if I said y equals ten, I cannot change x ref to be equal to what? Well, I could say extra equals y, but this would actually just change x. This would not be a nickname for y now, and you can see that by if I print out x ref and x down here on the same line, and then we'll create a new line. Then we will change y to be equal to 15. And then down here we'll say C out and we'll print out extra. Why? Like so if I run my code here, you can see that we printed out ten because extra was set equal to x, which was five. And then we set x basically equal to y, now x equals ten. So we printed out ten because extra f and x are the same thing. And then we printed out ten again, because once again, these two are essentially the same exact thing now, then we change the y to 15, but when we print it out, X ref or x, basically it was still ten and y was 15, so we printed out 15 after. So hopefully that makes sense. Once you said I referenced equal to something, you can not change it. After this line, our reference is set in stone. Extra will always the equivalent of x and vice versa, which is y, x change the ten when we change x ref the time. Okay, So there you go. That is how you create a basic reference. All you're gonna do is say the data type of the thing you want to refer to, followed by the ampersand symbol, then you will have the name of your reference and you can call that whatever you would like. And then you'll have the thing it's going to be referring to. And there you have it anyways. Thanks for watching and I'll see you in the next one. 71. Chapter 13 (References): References & Foreach Loops: In this video, we're going to be talking about references once again, but we'll be specifically talking about using references with for each loops. So let's go ahead and take a look at that. First, we're going to create an integer array. So we'll say int my array, then we'll have some square brackets here. And then after that, we'll go ahead and throw in some numbers. So say one, comma two. Okay, so we have an int array of size five with five different values. Remember, if you wanted, you could put it right here too. So it's more easily readable. How many values are in your array, but you don't need to. Next, let's go ahead and print out everything on our right and our foreach loop like we've done in the past. And I do apologize for using the same examples over and over and over in multiple videos. But the point of it is, is to only introduce one new thing at a time so you guys can get a better understanding of everything. Alright, so right here, we'll create a for each loop and we'll say four. Or we could say auto, if you remember, since we want the same datatype of our container anyways, and then we'll say x colon my array, like so. And then in curly brackets, we'll put our code, so say CL x, and we'll create a new line after just like that. Now if I run our code here, you can see that this is working just fine. 12345. Now if we wanted to change the value is normally of the things in our array, it would not work. And that's because X is just a copy of these values here. We cannot say something like x equals five and expect it to change the actual values out these indexes. So if we run our code, you can see that we're just going to print out 55 times. And then if we were to print out our array again, so if we copy this right here and print that out again, and we'll just remove this x equals five lines. You will see that this will not change. So you print it out 55 times, then you print it out 12345 being the values stored in our array. However, if we make this a reference to our array or another name for our rate, if you would, this will work. So all we have to do is after auto here, we're going to throw in the ampersand symbol just like that. And now x will be an actual reference to these values, which is pretty neat. So if we run our code, again, you can see that we printed out 510 times because as it ran through, X was actually the same as each one of these variables here. And although I mentioned in the last video that a reference can't be changed, what is actually happening here is that every time it runs through our code, or for each loop is creating a reference called x, setting it equal to index 0 the first time around. And then running this code, I'm going to create a another reference called x and set it equal to index one. And it goes through in order, just like that, creating a new reference each time which no longer exists after our code block. So don't worry about anything there. And because it is a reference, we're able to change the original values. Now the other great thing about references is that because we are working with the original, we aren't creating a copy which can get really expensive. So for example, if I just initialized every value in my rate to 0 and then I had an array of size a thousand. This will work just fine. And if I ran, this would be printed out a thousand values. But because this was passed by reference, you just change the value and printed it out. How do we not passed by reference though, like we did here. Actually, then we had to create a copy of all 1 thousand values and then print it out. And right now we're working with integers, and this is the only thing in our program, so it wasn't that bad. But when you are working with larger datatypes and more information, this can get really expensive. So doing something like this here is more efficient. So there's another example of when to use a reference and how they can work with for each loops. Anyways, that's it for this video. Thanks for watching, and I'll see you in the next one. 72. Chapter 13 (References): References As Parameters: In this video, we're gonna be talking about references once again, but we're specifically going to be talking about references as parameters. And if you remember in a previous video, we had already worked with arrays being passed in by reference, but now we're going to be passing an other datatypes by reference as well. So let's go ahead and take a look at how that is done. First, we'll go ahead and create an integer variable here. So say int num one equals five. And then below that, let's go ahead and print out num one, just like that. Then down here, we'll create a method and we'll say void, which means we don't need a return type. And we'll say set to ten, for example. And then in parentheses we will take an integer and we'll just call it x. Inside of here, we'll say text equals ten. And then we'll create a prototype for our function above. So don't forget the semicolon. And then all we need to do is have a call to our function here. We're saying set to ten. Open and closed parentheses will put num one inside and a semicolon at the end. And if we run a code, it's going to print out five. And that's because other datatypes outside of arrays are passed by value. Meaning when we put num one here as our argument, x was equal to the value of num one being five. Then we said essentially five equals ten. Well, that didn't change num one at all. So that's why when we print it out in a one, it was still fine. But if we pass by reference by putting the ampersand in front of the datatype here. And we've got to do that in our prototype as well. Well, now x is a nickname or an alias, or a preference, or whatever you wanna call it for num one. So when our code ran, we said, okay, we're going to create a reference called x and set it equal to whatever is passed in being numb one. Then we essentially said num one is equal to ten. So when we run our code and now num one is ten, and that's because x was a reference to what we passed in. So there you go. That is how you can use references as parameters. Now, this has the same benefit that it did with for each loops where that, every time this runs through, it's going to create a new reference and set it equal to whatever it is we pass in. So the next time we call this, if we said num two equals three for example. And then we copied our method call right here and put in two. Then let's go ahead and print out a space followed by two. They will both be set equal to. And that's because every time it runs through, it creates a new reference called x and such equal to whatever argument we passed in. So x isn't actually being changed to a different reference. It is a whole new reference called x, and then that no longer exists once this code block ends. So that is why that works. Now, what I do want to mention is that you can pass something in here for the efficiency and makes sure that you don't change that value. So let's just say for example, we're passing in a whole book of text and this method was to print it out. Well, we wouldn't want to create a whole copy of that book of texts every time we ran this method. Instead, we'd probably want to do something like this where we just work with the same book of text and then to make sure that we don't edit that text through user error in our code block here, all we have to do is say const. And this will prevent us from messing with this variable now by saying, Hey, this is not modifiable. And so that will just prevent any user error on your part or anyone else who might be able to edit your code. It will be a very obvious that hey, I just wanted a reference to my variable, but I did not want to actually change that variable in. So no matter what I put in this code block, that's not going to let me mess with that now. So you can use the const keyword to create a constant even if it is a reference, just to make sure that hey, you can't modify this. I just want this to be read only basically. And that will essentially allow you the efficiency without the ability to change the original. So that's great. Anyways, that's it for this video. Just wanted to show you guys how you can use references with parameters. Anyways, as always, thanks for watching, and I'll see you in the next one. 73. Chapter 13 (References): References As Return Types: In this video, I am so sorry, but we're talking about references. Once again. However, this time we're talking about references as return types. So let's go ahead and take a look at how that's done, as well as the actual use case for this. So what we're gonna do is create an int array to start out with. We're going to call this my array, and we'll set this equal to 12345, just like that. And we'll go ahead and explicitly say, Hey, we got high values in here. Alright, now down below this, let's go ahead and print out everything in our array. It will say for auto x, colon my array, just like that. And ink curly brackets. We'll go ahead and X on a new line each time. So let's create some blank space between those because we're going to be putting something here in just a moment. And then down here, let's go ahead and create our actual function or a method, if you would. So we're gonna do is say, followed by the ampersand symbol. And then we'll say set the value just like that for the name of our function. And then in parentheses here, we're just going to take an integer, so secant x. So then we'll have some curly brackets. And then right here, we're going to say turn my array index X. And let's go ahead and actually call that just stroke for index. Now, you'll notice we get an error because our array is undefined. So what we actually need to do is take our array here and we'll just put it outside of any function like that. Then we're also going to copy our method here, or the top line of it, I should say, for our prototype just like that. And now what we're going to do is actually we're gonna put the text below, are printing here and go ahead and copy our foreach loop and paste it down below, like so. And then in-between here, we're actually going to change the values of r, right? So because our function has a reference as its return type, we can actually use it on the left side of an expression or an assignment statement in this case. And you'll see what I mean here in just a sec. So we're going to say set value. And then in parentheses, we will put the index that we want to change. So let's go ahead and change those second one here. So we'll say one, because remember, indexes for containers start at 0. So we'll be changing this value right here. And then we can simply say equals. Let's go ahead and change another one right below that. So we'll say some value of index four equal to five. Or let's go ahead and change it to something like 33. So let's go ahead and take a look at all of our code here. First, we created an array outside of any function so that it was globally available in this case, then we had our function prototype. And if we go down to our function here, what we did is we had an int reference return type and our function was called set value and it took an integer. And then what we did is we returned a integer reference of the index specified for this specific array. So everything here on the left may as well have said int reference, call it graph for example, equals my array index one. And this would have been the exact same thing, and we'll see that in a second. So if we run our code here, what we're gonna do is print out one through five, and then we printed out one. Our second index was changed to 1034, and then our last index, or our element at index 4. Fifth element, however you want to look at it was set to 33. So there you have it. And it would have done the exact same thing if we went ahead and took this right here and erase that. And then down below that said equals ten because we actually need to change the value here and then run our code. Would you look at that, the exact same result? So there you go. Let me go ahead and undo that real quick. So as you can see, saying, this was essentially the same as saying this, along with saying breath equals ten. The only difference here is as we are able to create a reference to it ever index we want it by just returning a reference and inserting the index that we want it. And then because we had a reference as a return type, you are able to use our function here on the left side of the expression. So we said, Hey, change my array essentially at index 12, which you could have also looked at as saying my array index one equals ten, which in this case at least would have been a lot simpler. But I just wanted to show you how using references as return types work. Basically, it just allows you to actually return a reference equal to whatever you put in the return here. So our integer reference was equal to my array, whatever. So there you go. That is a use references as return types. You'll put the ampersand after the return type datatype, both in your declaration and your prototype, and then you return what you want it to be a reference. So there you have it. Then wrap up this video. Let me show you how I've just created a reference to an integer in our method here. So if we go ahead and remove all of this here, and we'll call our method rent. And we'll do the same down here. And then we'll actually go ahead and remove the argument from both. So we have something that looks like this now, and then we'll return x. And to define next, we'll create a global variable X. So we now have a variable called x, isn't set to anything. But if we wanted to, we could set it to 0. And here, and our main function, we'll say by wrapping the name of our function and then open and close parentheses, since we don't have any arguments to pass and set it equal to ten. And then down below, we'll say x. So what is this going to do? First, we created a global variable called x, and then inside of our main function, we have a call to our user-created function called the rent by wrath. And what this is going to do is return an integer reference to x. Now keep in mind, this doesn't have a name, so we wouldn't be able to use it later really, but it will still work for this use case. So we said to return by reference, which gives us a integer reference to X. So now this might as well be x. So this was the same as saying x equals ten, basically because we created a global variable called x. And then when we call our method here, it returned a integer reference to x. So this may as well be x, and we set that equal to ten. Now, when we print it out, x, x was ten. And if we run our code, you can see that that is the case. So there you go. Hopefully now you have a understanding of how to use references as returned types. It basically allows you to directly return a reference. And the main case for that being that you can use your method here on the left side of an assignment statement. So you've seen how to do that with both a variable and with a container such as an array. Anyways, that's it for this video. Thanks for watching, and I'll see you in the next one. 74. Chapter 13 (References): Memory Addresses: In this video, I'm going to be giving you a breakdown of what references are actually doing in the background as sort of an introduction to our next topic in this course being pointers. And you'll see why I made this video once we get into that. But for right now, let's go ahead and just focus on the breakdown of what is actually happening with references. So to do that, what we're gonna do is create an integer variable called x and set it equal to five, for example. And then I'm gonna go ahead and add some comments here. So this is just an int variable. And then below that, we're going to print out the memory address of our integer variable. And we can do that with this ampersand symbol followed by the name, our variable, and then we'll print out a new line. So this here is going to print out the memory address of x. And if we run our code here, you can see that that is what happens. Or at least just trust me, this is the memory address of x. I know it looks crazy. Don't worry, you don't have to break this down or anything and figure out what that all is. Just know that that is how we can get the memory address of a variable, okay, Now, if we create a reference to x, so if I said ampersand, and we'll just call this href equals x. So and then print out ampersand ref to get the memory address of rep. Like so we'll create a new life for that one as well and then run our code. You can see that these are the exact same. Okay? So right here we created a reference x and then we printed out the memory address. Our next reference. Like you saw when we were in a code, these are the exact same. And that's because like I said before, a reference is essentially just another name for the same exact thing. Nothing is different. They share the same exact memory location and they share the same value. Breath index it printed out normally would just both print out five. And to really drive that point home that they are the same thing. I'm going to show you one last thing here with using it as an argument. And I know we've talked about this somewhat in the past, but I'm just gonna show you one more thing about that. So if you said x equals five, and then below that credit a reference to experts in ampersand, ref equals x. And then let's go ahead and create a method down here, and we'll say void, change int num. And in our block of code here, we'll say num equals 22 and we need to make a prototype of this. I'll just put it up here, like so. And now if we go ahead and color methods don't change wrath. So we're going to pass in our actual reference to x, c out, breath. Like so what do you think is going to happen? Well, let's go ahead and see if it's going to print out five. And that is because like I said, a reference and the thing it is referring to or they're referring to are the same thing. So we can pass it in just like we cut the original variable and it's still not going to be changed. Now how do we take an, a reference as a parameter here, like we have in the past, then yes, it would change to 22. And we even could have just passed an x here and it will still change to 22 because like I said, they're the same thing. You can use them the same white. So there you go. Just really wanted to drive that point home that a reference and the reference are the exact same thing. They can be used in the exact same way, but it's just a nickname for whatever it is referring to. They even share the exact same value with our printout rough or x. It's the same thing and they even share the same location in memory. And like I said, the reason for this video, other than showing you how to get the memory address of a variable. And that you could just throw in the reference here without changing it because you kept the variable, had this not taken that reference was to get you ready for what we're gonna be talking about in the upcoming series of videos being pointers. So hopefully that'll make sense as always, thanks for watching and I'll see you in the next one. 75. Chapter 14 (Pointers): Intro To Pointers: In this video, we're gonna be talking about pointers. Now, a pointer, like a reference, is used to store the address of a variable rather than just storing the value of it itself, like the references that pointers store the address of a variable. Now, what is the whole point of a pointer? Well, for one, it allows you to assimilate calling by reference. And they're also for creating and manipulating dynamic data structures or dynamic memory allocation. Now, we're not going to get into really creating and manipulating dynamic data structures or dealing with dynamic memory allocation. Or he would in this course, just because it's a much more in-depth topic, then I want to dive into, in this course specifically. But that being said, we're still going to be taking a look at pointers, how to use them, when to use them, and the differences between that and references and when you would want to use one over the other, as you aren't gonna be able to go anywhere in C plus plus programming without knowing what pointers are. So it's important to know what they are, how to use them, and when to use them. Anyways, that's it for this video. Thanks for watching, and I'll see you in the next one. 76. Chapter 14 (Pointers): Creating A Pointer: In this video, we're going to be creating our first pointer. So the syntax for a pointer is as follows. First, you're going to have the datatype of the pointed to variable. So similar to how we created references, where we have the datatype of what we were planning to reference. You're going to have the datatype of what you're planning to point to how loud by the asterisk symbol to signify that this is a pointer similar to how we have the ampersand with references to signify that we hear creating a reference. Then you're going to follow that up with the name of the pointer, and then you can go ahead and assign it to something as well. However, you do not have to give it an initial value right off the bat, like we did with references. And we'll get into that a little bit more later on in the course as well. And when you assign it, you're going to assign it the address of a variable. Now, the asterisk and C plus plus, as we've seen now, can be used for multiple different things. First, they can be used to declare that you are creating a pointer. It can be used to dereference a pointer, and it can be used in multiplication. And you'll see some examples of dereferencing a pointer here in a bit. And then one thing I did want to show you with this text here, because whether you have the ampersand when creating a reference right next to the datatype or right next to the name does not matter. These two lines of text are the same and we can even create a reference to a reference, and they will now all be the same thing. So x, ref and rough to here are all now essentially the same exact thing. They're all just nicknames or aliases for one another. And if I run my code here, you can see that this works just fine. So whether you have the ampersand here or here doesn't matter. And you can have multiple references to the same thing, but that's enough about references for now. I just wanted to show you that real quick. And the same goes for when you're creating a pointer, the asterisk here, or you could have it right here. It makes no difference. Personally. I like to have it here, especially because when you are working with functions like we saw with references and you have a return type that looks like this. You don't actually have the name there, and you just have this right next to the datatype. So I just liked that consistency also when creating multiple of the same variable. So let me go ahead and remove this quick. If I said x comma y, and then I can say x equals five and y equals five. And when you have something like this, where you create multiple of a variable on the same line. You can do the same with references where you say int x reference and why reference. Some people like to have the symbol next to the name for the consistency with this. But personally, I don't like to create multiple variables on the same line. I think it just makes your code less readable. So that's why I personally put it next to the datatype. But if you do see it the other way round, don't worry, it's the same exact thing, whether it's a pointer or a reference, doesn't matter. Same thing. Now let's finally get into the whole point of this video, which is to create a pointer and see how it works. So it will say int x equals ten, and then down below, we'll create a pointer. So we'll say int asterisk and then we'll say P, x. Some people like to prefix there pointer names with a peer. So right here, I'm seeing it like this, just so you know that it is a pointer, but you can name this whatever you want. Then we'll set it equal to the address of x, like so, remember ampersand before a variable name like this, because the address of that variable. So in this case you could look at the ampersand as the address of operator. So we created a pointer by giving the datatype of what we wanted to point to, colored by the asterisk, then the name of the pointer. Then we said equals the address of the variable. So this was our declaration and initialization of a pointer to. Now, you don't have to initialize it on the same line as you declare it, unlike with references. So down here, I'll just go ahead and recreate that. And we could have said int asterisk x for our pointer to x, and then just put a semicolon at the end like that to just declare our 23rd. And then we can initialize it later by saying P x equals ampersand x like that. And this will run just fine. So if I run my code, you can see that we don't get any errors and everything works. Anyways, let me go ahead and undo that now. Okay, Let's go ahead and print out our pointer. So if you remember in the last video, I said that a pointer is essentially just a variable that holds an address versus where a reference holds the value. Had this been a reference, it would basically be the same as X, but a pointer holds the address of the variable. So if we print this out as, as, we'll print out the address here. And you can see that right here, and this is the address of x. And to prove that, Let's go ahead and also print out the address of x. So after n line here, we'll print out the address of x by using the address of operator, if you would. And then after that, which is great. I'm like so. And if we run our code here, you can see, hey, there's our address and there's the exact same address. Okay? So I pointer naturally holds the address of whatever it is pointing to. So that's why we just printed out the planar. We printed out the address. Now down below, we're gonna do the opposite. So we're going to do down here is print out the value, help address. Our pointer is pointing to the value of x. And we'll actually just put this code right below it. So we're going to say C out. And then we'll say the asterisk symbol followed by p of x and then a new line like so, I'm going to print out x and the light. So if you remember, like I mentioned up here, an asterisk is also a de-reference are, and what it allows you to do is dereference and address. So our pointer points to the address of x, that is what it holds. And then using this asterisk, we can de-reference that. So instead of just getting the address of the variable, we can get the value held at that address. And then we're just printing out the value of x and these will be the exact same. So as you can see here, we started out by putting out our pointer, which just holds an address, and we printed out the address of x. And that was the same thing since our pointer points to x. Then down here, we use the asterisk symbol to dereference a pointer and get the actual value held at that address. Now we're not going to get too deep into addresses, into how everything is stored. But just note that a pointer holds the address of a variable and you can get the address of a variable with this ampersand symbol. So this address here is where our value of x is stored. So ten, in those cases, it's stored at this address. And our pointer holds that same address because that's what we told it to do. It. And then down below that, we printed out our pointer, which like I said, pose that address. Here. We printed out the address of x, which should be the exact same thing. And then we use the asterisk symbol to dereference a pointer followed by the name of our pointer, which gives us the actual value held at that address. And then we print it out x, just as we've done in the past. So what does this mean about all the variables that we've been creating? Well, it basically means that when we said Hey, we're going to create an integer called x and set it equal to ten, that we're creating an integer that holds the value ten. And this is stored at an address or a location in memory. And we can access this value. Again, we're at that location in memory if you want, by just saying x. So there you have it. Hopefully that wasn't too confusing for you. All I want you to take away from this really, is that a pointer points to an address of a variable. That is why it holds. So just like our variable here equal to this value, a pointer holds the address of that variable and also how to create a pointer. But we'll be getting some more practice with this in the upcoming videos. Now, one last thing I do want to mention is that because you don't have to initialize a pointer when you create it. You can also change what a pointer is pointing to. So e.g. we can say y equals 15. And then down here, after printing all this stuff out, let's actually go ahead and copy all this and paste it down. And I'll create some space here so we can see the differences between these. Then we'll go ahead and remove the declaration. So this int asterisk down here, and we'll just say Px is now going to point to the address of y. And then we will print out the x again along with the address of y. And then down here, we will dereference the address of y, which will just print out why essentially. And we'll print out y as well. And if we run a code, you can see that this works just fine. So there is the address of x and there is the address of y. And then we also printed out the value along with the value of y using the pointer and using our variable directly. So there you have it. That's how to use a pointer and a little bit more about what a pointer is. Thanks for watching, and I'll see you on the next one. 77. Chapter 14 (Pointers): NULL Pointers: In this video, we're gonna be talking about pointers and null. And if you're a member, because we have seen know in the past, null just means nothing or 0. Okay? So what do I mean by pointers? And know what? Let's take a look. So let's go ahead and create an integer variable here called Exelon. Just set it equal to ten. And then we'll go ahead and create a pointer. So we'll say asterisk p x, again, like we did in the last video. And we're going to set this equal to and actually know what needs to be in all caps. Just for clarity, I'll put it up here as well. So null is the same as 0. So whether I had this or this, it does not matter. It is the same thing. So go ahead and do that so that there's the same as this. So what does this mean? And why would you want to do it? Well, anytime you create a pointer, you do not want to do this and then do work with this pointer without initializing it to something. It is incredibly dangerous as you can end up overriding memory or a crash your program and a whole list of other bad things that you do not want to do. So when you aren't going to have this initialized immediately, I would always recommend setting it equal to like this. Or like I said, you could put 0 to say, Hey, I'm not initializing this right now, but I'm not just going to leave this on initialized essentially. So basically what this is saying is, here's a pointer, but it doesn't point to anything in memory. And we didn't actually need this integer variable called x. I just put it here to show you the same example that we had in the last one sentence. And now what you can do before using this pointer is say the following. Down here. Before we did something with our planet, we can actually check to see if our pointer points to anything because we set it equal to null here. Basically it's saying it's not pointing to anything right now. But because we did this, we can do the following. We can say Px is not equal to null. Then we run, to run our code by having no, they're explicitly saying that it points to nothing. We can check to see that it is pointing to something before we do something with it. Alright, and this will help prevent tons of errors down the line because you're going to work with a pointer and you're not going to give it an initial value, always set it to null, and then check to see if it is still equal to null later, before you've worked with it. Because like I said, you do not want to do work. If this looks like this with your pointer and it's very dangerous. So if you're not going to initialize it, always set it to null, basically just explicitly saying this doesn't point to any location in memory. And then down the line, you can just check, hey, it's not pointing to anything and run your code in this here. Why did it not work properly if you just did this, having this check here will not work if you do not say. Alright, so that's basically how null works with pointers. That just allows you to explicitly say that your pointer isn't pointing to anything right now and allows you to check to see if it is pointing to anything before you do something with it. And if you wanted to, you could always do something like this. Three, say else, and then print out a pointer, is pointing to anything just to warn you when your code runs that hated my if statement never went off. Because when your code gets a lot bigger, you might have an if statement like this. And it might be hard to realize that, hey, this code didn't run if you don't give yourself some heads up. So there you go. That's how nature works with pointers and the importance of using null with pointers. And as always, thanks for watching, and I'll see you on the next one. 78. Chapter 14 (Pointers): Pointers & Functions: In this video, we're gonna be talking a little bit about pointers with functions. So just like we could with references, have a return type, be a reference and a perimeter beat I referenced. We can do the same with pointers. And because we've already talked about it with references. And because pointers and references and a lot of ways work very similarly, I'm just going to put this all into one video rather than having it in two separate videos. So let's get into it. So down here under main, let's go ahead and create a function that has a pointer for a parameter. So what we're gonna do here say Lloyd, change value like this. And then in parentheses we'll say int asterisk, and we'll just call this x like so. And then in curly brackets here, we'll say asterisk x equals ten. And then up here, Let's just go ahead and create an integer variable, say x equals five, like so. And then down here, we'll go ahead and call our methods will say change value, and then we'll just insert x like that. And we need to make sure degree a prototype here. So we'll just do that up here. And before our function call, Let's go ahead and print out x and create a line like that. Now, notice we have this error, and that's because int is incompatible with the parameter of type int pointer. And had this been a reference, like so, this one I've worked just fine because, well, we would send X and here, this would create a reference to our variable being x. And we can name this whatever. And I'll just go ahead and actually rename this number here, like so. Okay, so we have int num, we printed it out, and then we sent it in as an argument. And had this been a reference, this would have worked just fine. We would have created a reference who eggs and changed x and we kind of get it out. Down here. I just printing out again. And now what a change to ten. And we also have to change it up here in our prototype for just a moment and see if we ran it, it would work just fine. However, a pointer doesn't store the value directly and stores the address. So if we go ahead and change this back to a pointer now, we can still do the same thing, except we have to send the address of the variable. So we'll put our ampersand symbol right here. And then down here, we can't just say x equals ten because we don't want to change the address of the pointer. And you can see that this gives us an error actually, but we can't change the. So if we go ahead and de-reference this, it would work just fine. Now, we created an integer called num, does not equal to five. Then we print it out num, which will just print out five, obviously. Then we sent the address of num into our parameter here. So now we have a pointer that points to the address of num, and then we dereferenced a pointer. So we're saying the value held at the address of num, we want to change to ten. And so this will also change num to ten. And if we run our code here, you can see that that is what happens. Now. What if we would have created a pointer first? So if we were to set asterisk, we'll just call this point like so, and set it equal to the address of num and then go ahead and send it in. And now if we run a code, this, the exact same thing. The only difference is we just sent our pointer. Then we basically had a pointer that pointed out the same exact address and we got the value that was held there, then change it to ten. So that worked as well. And lastly, let's go ahead and have a pointer as a return type. So let's go ahead and uninitialized our pointer here and just set this equal to null. Remember if you're not going to initialize it originally, you want to set it equal to null. So we'll do that. Then down here, we're gonna go ahead and send the address of num. So we'll say n, numb, like so. And we'll change it from void to a pointer type like that. And we need to do the same up here. Then down here, what we're gonna do is return x, like so now we have an income them that's equal to five. Then we printed it out. Then we created a pointer called point that currently doesn't point to anything. And we explicitly said that by setting it equal to no, then we ran our method here are called their method, which took the address of num, put it in a pointer that we called x, and then we return that pointer, which if you remember a point to just holds an address, and we return the address and an integer pointer, which will get sent back here. So now we have an int pointer that holds the address of num because that is what we sent. But we want to go ahead and store that somewhere. Remember, anytime you're turning something, you should store it in something. Otherwise it is. So right here, what we're going to say is 0 equals that, and this will work just fine. Now, what we can do after a method called is say, we want to dereference. Remember, we're going to put an asterisk pointer and set it equal to ten, kinda like we did before. So now we're saying we want to take the address of num, basically de-reference it. They get the actual value held that none, which is currently five, and set it equal to that, then we're going to print out ten down here. And let's go ahead and take a look at that quick. So there you go. The top 510 and how do we want it to? We could have just put this method call right here, gotten rid of this here. And this would have ran just fine as well, since this is returning a address for us and we need to set this equal to an address. This works all fine and dandy. So there you go. It essentially works the same way as the reference type that, except that with a few caveats, sunset holds an address instead of the value like our reference type that there. Anyways, that's it for this video. Thanks for watching, and I'll see you in the next one. 79. Chapter 14 (Pointers): Pointers & Arrays: In this video, we're gonna be talking about pointers and arrays, as well as going over some of the interesting similarities and interactions between them. So let's get started. First, let's go ahead and create an integer array. So we're going to say int, my array, then I'll open and closed square brackets was put in a number like three here. And I want to set this equal to three different numbers. Let's go ahead and say 1025. And then down below here, we're gonna go ahead and create a pointer. So we're going to say int asterisk, and I'll say PTR, short for pointer. And then we're just going to say equals my array, just like that. Then I'm thinking I want to show you is this value here just needs to be a continent and you don't have to put a number here directly. So if we say times and then call this array size, for example, set this equal to three. We can actually put array size right here in our array size, if you would. And then down here, what we're gonna do is create a basic for-loops are necessary for I equals 0, I less than array size I plus plus. And then inside of our for-loop, what we're gonna do is print out a couple of different things. We're going to print out the addresses of these variables here, as well as the values using our pointer. So let's go ahead and take a look at a few ways we can do this. The first one would be the following. We can say something like C. And then in quotes here, we can say address my array on pane open square bracket. And then after this, we can draw N. And then after that, we'll create another set of quotes followed by a closed square bracket to say equals plus like that. Then below this will go ahead and say CL, PTR being the name of our alibi in mind. So now let's go ahead and copy and paste this down below as well. Army change this one to value like that and leave the rest except for it down here. I'm saying C out pointer. We're going to dereference a pointer here, and this will print out the value. But now, how are we actually going to iterate through and print out one value after the other? Well, the interesting thing is that this line here is basically saying that we went to a sign address of my array at index 0 to GTR. So essentially, this is kinda like saying pointer equals my array index 0 with an ampersand here. And this, and this are essentially equivalent. The reason that this assigns the address of our array at index 0, q pointer is because our array name here actually contains the address of the first element of the array. And this acts like a constant pointer, meaning the address stored in our array name can't be changed. And interestingly enough, pointers actually have some operators we can use in this instance. And I'll be showing you one right down here. We can say pointer plus, plus. And as this moves up through the addresses, if you would, it's going to move up along these elements. And if you remember when we first talked about arrays, I told you that these values were stored in sequential order. And so essentially we can just move our pointer up in sequential order to move up along these values. Since our pointer is starting at index 0 here and every run our code, you can see that we said address of my array at index 0, which was only indicated by this I value that we were increasing equals pointer. And so there's our address. And then we set a value of my array at index 0 equals ten. And that is because we've dereferenced this address here. And then we increased our pointer by one here. And that moved it up one element in the array, essentially. And we're able to print out the next address and value all the way through. Now, let's go ahead and take a look at another way because I've done this. So let's go ahead and remove this pointer plus, plus. And then actually right after the word pointer C Plus Plus allows us to use the same syntax that we would for an array when our pointer is equal to an array like this. So we can just have open and closed square brackets and put it right in there and do the same down here. And this will actually give us the same effect essentially. But instead of the asterisk here, we're gonna put the ampersand symbol like that and run our code. And as you can see, this actually works as a variable now, so it is opposite. So if we want to go ahead and fix that, you can just move our ampersand up here. And now, just as if this was a array, we will put the ampersand symbol here to get the address of the variable. And we would just use it normally to get the variable itself. So if we run a code, you can see that it works just as it did before. Let's go ahead and take a look at one more way. We could work with us, and this isn't the last way you can work with pointers and arrays. It's just the last one we're going to showcase here and most courts, so in select creating a pointer here and setting it equal to my array, then we're actually going to make this an array of pointers. I putting in square brackets like this. And then we'll just put a re-size right in there like that. Then down here, what we're gonna do is leave this as is. We're gonna go ahead and cut this code as we may use it later here in just a moment. And then inside of here, we're going to say pointer ampersand my array index I. So now our pointer array that is uninitialized currently is going to be assigned the addresses of these variables. Okay? And then down here, let's go ahead and create a, another for loop. But it looks the same as the one above. I equals 0. I less than size I plus, plus. And the nice thing about saying array size here and using that for actual array size as that this is very readable and it's very clear when we're stopping our loop and how many times we're running through it, once per element in our array. And then down here, we're gonna go ahead and paste what we had before, except we'll remove this ampersand and put our asterisk back in the bottom here and run our code. And now it works the same way as it did before. So what we did was we created a constant integer called the array size and set it equal to three. Then we created an integer array and set it equal to the size of array size and give it three values. Then we created an array of integer pointers and didn't initialize it. And then we created a basic four loop that was going to run once per element in our array. And we set each of the addresses for these pointers here equal to the address of the values in our array. Then we created another for-loop that ran the same amount of times as the one up here, and we simply print it out our address those as normal, just like we were printing out the value of an array, then the reference to this once again, and just to recover, or we summarize our previous examples as well. Let me go ahead and go back here. So we just created an array and then created a pointer and set the address in it equal to this address here. Essentially, that's because our array name here actually holds the address of the first element. And this was the same as if we said this right here. Okay, So if I ran my code like that, that will work just fine. Now, C plus plus will allow us to actually use the same syntax. We would print out the elements in our array with our pointer here. Okay? And then if we go back to our original example where we had this here, once again, same thing when a code here for the bin here. And it will all work the same except this time, instead of using the array syntax, we simply increase the position of our pointer and it just went up one element at a time. Now, something we could have done to do this in reverse as the following. We can assign the pointer plus equals to move it from here to here, because it was moved up two places. So instead of being at ten and is now pointing at the 50, which is my array at index two. Then we could have said pointer minus, minus here and started our index at three, and then said, Well, I is greater than 0 minus minus, and ran our code. And now it would have gone in reverse order. And actually our inductors are off by just one. So we can fix that by changing i2 to start and saying, Well, I is greater than negative one. And Marina it again, and there we go. So the address of my array at index two, because that's where we started now is the following, and the value is 50, then we get the address of index one and so on. So there you go. There's some of the interesting interactions between pointers and arrays. If you're curious about our array Name holding the address of the first element and not just the value. If you remember back in the video where we sent an array as an argument into a perimeter that took and it was passed by reference. And that's because it holds the address, not the value. Anyways, that is it for this video, I know it was a lot of information. So if you didn't fully get it, feel free to re-watch this or look up some more information on the subject. Otherwise, don't worry, we're not going to be really dealing with us throughout the rest of the course. Anyways, that's it for the video. Thanks for watching, and I'll see you in the next one. 80. Chapter 14 (Pointers): Pointers vs References: In this video, we're gonna be talking about some of the differences between pointers and references so that you know when to use, which. So let's get started. First off, references are used as aliases for existing variables, whereas pointers are used to store addresses of variables. Also, references can not have a null value, but pointers can, and pointers can be changed later on. Also, on a smaller note, as pointers hold addresses, they need the referenced with the asterisk symbol. Whereas references can be seen as a constant automatically. By default. Let's just one way to look at a reference as a pointer that can't be changed. That by default is the reference. And then you have it. Here are some basic differences between the two. And there's even a lot more that you can do with pointers that we didn't even touch on. For example, you can have pointers to pointers. There are multiple other containers that we didn't cover in this course that you need to use pointers to work with and pointer arithmetic also, you can use pointers when doing things with dynamic memory allocation as well. And because of those, pointers are much more versatile and allow you to have much more complex code to do various different things compared to references. So instead of giving you all the use cases for each, I'm just going to tell you when you should generally use a reference and then as a general note, you will use pointers for everything else. So when should you use references? Well, generally speaking, you'll want to use references when working with function parameters and return types, as we saw earlier. Now, obviously this can vary depending on what data type you're working with, but this is just a general note. Now, another good time to use them is when you want to work with the original elements in a for each loop. And this may change depending on what datatype and container you were working with. So these are just some general rules. Like I said, this can definitely change depending on your situation. And then you would only use pointers for other things. And we've only touched the surface of pointers and there's a lot more you can do with them. So if you're interested in and all that, definitely go ahead and do your research on that. Anyways, that's it for this video. I just wanted to touch on some of the differences between pointers and references, as well as give you a general idea of when to use references. So that by proxy you know when to use pointers instead, as well as touch on a. Another way to look at references compared to pointers being this right here. Anyways, hope you enjoyed the video. Thanks for watching, and I'll see you on the next one. 81. Chapter 15 (Classes): Intro To OOP: In this video, we're gonna be talking about what exactly is object oriented programming, also referred to as o p. Now, object oriented programming is all about creating objects that contain both data and functions that represent real world objects. This type of programming has several advantages over procedural programming, which is mainly about writing functions that perform operations on the data rather than creating objects or classes. And we're not going to dive too much into the details of procedural or functional programming. But real quick, let's just go ahead and talk about some of the advantages of object oriented programming. Well, first off, object Oriented Programming is faster and easier to execute. It provides a clear structure for your programs and it prevents you from having to repeat yourself, making the code easier to maintain, modify, and debug. And the last advantage that we're going to mention here is an object oriented programming allows you to create reusable applications that are made with less code and are developed. Pastor, now, up until this point, you've probably heard me refer to objects and classes as the same thing. And oftentimes you'll hear people use the word object and classes interchangeably, but in all technicality, they are different things. So remember how I said that collapses represent real world objects using functions and data. Well, let's go ahead and talk about the differences between classes and objects and what a class would be in relation to a real-world object as an example, and what the objects of that class would be. And you'll see what I mean here in a second. So for an example, let's just go ahead and use fruit, some sweet used fruit with our enum example in a previous video. So we can create a class called Pro. And this is not the syntax for a class. That's just how I'm writing this out here. That would have different sets of data and functions. So for example, you may have data to represent the sweetness, size and maybe the seed color. He may have functions for the growth or the lifespan of the fruit as an example here. And then we would have objects are fruit class or instances of it that inherit these variables and functions but have their own instance. So some examples of objects of our fruit class that we may have would be things like an apple or a banana, orange, grapes, etc. So essentially, our fruit class would be like the blueprint for everything that inherits from it. Or if a is an instance of it. So for example, or Apple would have a sweetness level of size, a seat count, and a lifespan. And so what our banana and our orange, and our grapes, etc. So that is kinda defines what a fruit is and are objects of our class, are instances of the blueprint that is defined in our class. Another thing to mention here, while we have this example written out as an apple would be a fruit, and a banana would be a fruit, et cetera. But an apple isn't necessarily a banana. The banana isn't necessarily an orange, as each of these would be different in their own right, but would all be a fruit in this case. Anyways, hopefully that all makes sense. I know it was kind of a bit of information to take in all at once. But if not, we're gonna be creating an working with classes and breaking them down piece by piece, just as we did with functions. So don't worry, stay tuned. Thanks for watching. And I'll see you in the next one. 82. Chapter 15 (Classes): Creating A Class: In this video, we're going to be creating our very first class. So if you remember, a class is a user-defined datatype which has both data and functions, which are called members. And these members make up a blueprint for objects or instances of our class. So in the previous video, we talked about fruit and then having different fruit names for the objects. But another example would be if we had a class called cars that held the variables such as they make, model, and year. And then we might have functions for driving forward, backwards, going into neutral, et cetera. And then the objects of our class are the instances of it would be different vehicles. They would each have their own and make model and year, and would also have their own instances of the functions for traveling as well. So hopefully that made sense. If not, we're gonna be creating an example of a class here in just a moment. But before we do that, I have some enums here, another user-defined type that we worked with. And the reason I have these here as the kinda show you the similarities to the syntax for creating am working with a class, which we'll get into here in just a moment. And just to show you a couple of extra things with enums, now that you have a bit more programming knowledge. So if you remember, we just said enough fellow by the name of our enum. And then in curly brackets, we actually had the different enum values. And then in the past, we just created different variables of our enum type by saying our datatype, which was our enum, followed by the name of the variable, and then setting it equal to one of our possible values there. But a couple of things that I didn't show you where the following for one, instead of creating a separate variable on a new line down here, like this, we could have just put it after the curly brackets, but before the semicolon. So you can create as many variables as you want it like this, just separate them out with commas. And so in this example here, I created an extra variable called flavor one and then one called flavor too. And I initialize flavor one down here. And I simply have the declaration for flavor to right here. And this one up above, I did something very similar, except I left out the name for this one, meaning I cannot create a variable later on like this, since I have no way of referencing it and our code will actually run just fine like this. So to make variables of this one, I had to put it afterwards like I did down here. You can also initialize them up here, like so. And I could have done the same down here. Otherwise, I can also initialize it down here like I did with this one. Anyways, I just wanted to show you this quick. Basically the only differences being that you could remove the name of your ENM entirely, but keep in mind you wouldn't be able to reference it. And that you can have the variables declared and initialized in the same line there by just putting it after the curly brackets and before the semicolon. Anyways, let's go ahead and remove that. And then we'll go ahead and create a class or just created in a very similar fashion. So the syntax for a class is as follows. First, you're going to say class, followed by the name of the class. And then in curly brackets, you'll have your actual code, or I should say, the members of that class. And then after the curly brackets, you can have your objects, if you would like, separated by commas there. Then lastly, you're going to close this up with a semicolon. Then down here, let's go ahead and create a basic class. And what we're gonna do is create a class for animals. So we'll say class animals like that in the normal naming convention for our class as camelCase, starting with a capital letter, open and close curly brackets. Like so in inside we'll have our members. So you might have an integer variable called age. Then below that, we'll have a variable called height, and we'll just leave it at that for now. So there you have it. We've now created a very basic class called animals. We have two different data members within it. So your variables that are within a class and your functions within the class are called your class members. And some people like to prefix all of their members with an underscore like this. So let you know these are members of class and not just regular variables. So let's go ahead and actually do that. And then, like I said, it also declare different objects right here, after a curly bracket and before our semicolon if we want it. So we might have a dog object and a cat object, for example. And if we run a code here, and you can see that this runs just fine. Now, obviously I didn't do anything, but I just wanted to show you that we didn't have any errors. And right now, this class only exists within our main function. And after this point here, however, we can actually cut this like that and put it outside of our method so that now it's scope is global rather than local here. Anyways, that is it for this video. But before you go on to the next one, what I want you guys to start doing is saving your code. Because in the upcoming series of videos, we're going to be just slowly adding to our class to make it more and more complex and look at different examples of things we can do with it. So like I said, make sure you save your code so that we can use it in the next video. Anyways, thanks for watching, and I'll see you in the next one. 83. Chapter 15 (Classes): Class Objects: In this video, we'll be creating instances or objects of our class. And before we do that, we're going to actually go ahead and just remove these two object declarations, just so we can see how to do this from scratch. So let's go ahead and remove these dog and cat objects. And we'll actually go ahead and create two different objects or instances of our glass down here. So remember, objects are instances of a class or object syntax. We're gonna do is we're gonna say our class name, followed by the name of the object, this pet name there. And lastly, a semicolon. So let's go ahead and create two instances of our class. And this is all will be doing in this video. We're going to say, is animals being the name of our class. And then we'll say dog for example. And follow that up with a semicolon. And then we'll also create another instance or object, if you would, of our class called cat animals. And we'll put a semicolon at the end of that. And then let's go ahead and do one more thing before we wrap up this video. So up here, instead of hi, Let's go ahead and give each animal a name. So I'll say string name. And then just to show you something quick, and we try to create an instance of each of these variables for our objects here. And we can do that by doing the following, by saying our object name, dog, in this case, followed by the name of the variable that we want to access. So you'd say dog dot name and try and set it equal to a string here, you notice that we'll get an error and that's because animal name is in accessible. And we'll talk about why in the next video. So go ahead and say that you have thanks for watching, and I'll see you in the next one. 84. Chapter 15 (Classes): Access Specifiers: In this video, we're gonna be talking about something called the access specifiers or come from another language. You may have also heard this referred to as access modifiers. Now, there are three different kinds, four numbers. First off, you have public and public members are accessible from outside the class. Then you have private and private members can't be viewed or access from outside the class. And lastly, you have protected, which can only be accessed and viewed by inherited classes. And we'll get into inheritance in another video. But the one thing that I want you to note here, classes by default have private members. So if we wanna be able to access these members, like our string name here outside the class, we need to make it accessible by changing this from a private member variable to a public member and, or a variable. And remember it because these are part of the class, they are called members. And I'm gonna go ahead and put an underscore before our string name here, just to signify that it is a member. And we'll go ahead and change this down here as well. And if you remember, if we mouse over this, it says animals underscore name is accessible and that's because it is private and can only be viewed and access from inside the class and not from outside. And by the way, you may also hear members of the class referred to as the class attributes. Now, how do we go ahead and make these public? Well, it's pretty easy. So all we have to do is above our members here that we want to make public, public, colon, just like that. And there you go. These members are now public. And if we look down here, you can see that we can now access the member. Now, we can go ahead and get our dog. I mean, you can say name equals something like spot. And we can go ahead and give her dog in H as well. So you can say dog underscore age equals two, then we can do the same for our cat now. So we can say underscore, name, something like Garfield. Then underscore age equals four. So now we've created a class called the animals that has two public members or attributes, if you'd prefer. One being a string called underscore name, being called underscore age. And this should have actually just been an integer, not a string. And then down here we created two instances or objects of our class by just saying our class name, Hello by a name for it, the object, and a semicolon at the end. And then each one of these has their own instance of these variables, okay, so they each have their own versions. And to access these, we just use the member access operator for the period symbol or the dot there if you want to call it. We said dog dot underscore name and set it equal to spot and dark dot h equal to two. Then we basically did the same thing down here for the cat. Anyway is to wrap up this video. One more thing I do want to mention is that for these other access specifiers, we could adjust, spelled them out as they are listed here. So the public or private or we could have written just like that. And that would have worked just fine. But I'm going to go ahead and change this back to public. So right now we have some public members, but we could have also created private and protected members if we wanted, by simply pasting that after or before. So we could have said private e.g. and then had some private members below that and that would have worked just fine. So that is how to use access specifiers and the importance of them. And I'm going to go ahead and tab in these variables here just to make it a bit more readable. Now, how do you know which access specifier to use? Well, just like when working with variable scope, when working with access specifiers, you only want to make things as available as you need them to be. And that is just once again to prevent any kind of human error. And normally you want, you want your members to be public like this. And we'll get into that in a later video. But for now, we're gonna go ahead and leave from public so that we can access some time here. Anyways, that's it for this video. Don't forget to save what you have. And as always, thanks for watching and I'll see you in the next one. 85. Chapter 15 (Classes): Class Functions: In this video, we're gonna be talking about class functions or how to create a function within a class. So in our case, we're going to go ahead and create a function for speaking. It's right up here inside of our class, we're going to say something like void. And it will say open and close parentheses. And then in a code block here, we'll say C out. Just like that. And we'll create a new line and put a semicolon at the end. And the sound is actually going to be a string variable that will take as argument. We need to make a perimeter for it. So we'll say string sound. And this will work just fine. And this is what the basic way to create a function within a class. But you can also define it outside of a class. And the way this is done is very similar to creating a function prototype. So down below here, let's go ahead and copy and paste our current function. Now to do this, we have to change a few things. First, we'll get rid of the definition of our function here inside of our class. And then we'll put a semicolon at the end. And then down here after our return type, we need to say animals colon, colon. If you remember, this is the scope resolution operator. And now this method definition or function definition in this part of our animals class, and we declared that it was gonna be a thing here. So if you want to define it outside, you need the class name followed by the scope resolution operator right after the return type. And then you need a declaration very similar to our function prototype up here, where you just have the return type followed by the name, any parameters and a semicolon at the end. So you've got, now, let's go ahead and use this function with our dog and cat. So down here, below both of our objects. Let's go ahead and throw in our function calls. So say dog, speed, and in parentheses will say art or whatever you want to, but they really don't really matter. And then Dumbo that will say cat, speak in parentheses. We'll say, okay, and don't forget the semicolon at the end. And if we run our code here, you can see that this works just fine. And we can print out these variables just as we would any other variable. We just have to specify what we're referring to. So we can say something like c out, cat, name, the new line, like so, and this will run. So let's go ahead and add some code here to print out our different animal variables. And we can actually create a function for doing so. So let's go ahead and remove these function calls as well. And then down here we'll say someone like the animal. And then in parentheses, we will take an animal. And our class name is actually called the animals. And maybe to make that sound a little bit better, we'll go ahead and rename it. And you can either go through real quick and just remove the S, or you can right-click and hit Rename here in Visual Studio, changed it to animal and preview. So now our function takes an animal and I'll just call this animal e.g. then in curly brackets here, we'll go ahead and print out all of the values inside of here. Because we are working with an animal object, we can actually use all the functions that we could normally, how do we created an animal object? So we can say something like CL, animal name, colon space. Hello, I am a mom. That underscore name like that will create a new line, can put a semicolon at the end. Then let's go ahead and copy and paste this one time. And we'll say CL, animal age. Then it will just change this to animal but underscore age. And then lastly, we'll say C out. Then we'll print the animal's name. So say animal underscore name, just like that. And then in quotes here we'll say space, says colon space. And I'll put a semicolon here. And then down below, we'll say animal, open and close parentheses and a semicolon at the end. And then we'll take a string of here. So we'll say string down here to speak. And now back in our main function, Let's go ahead and call this function for each of our animals. So say plant animal. In parentheses will say dog comma. Then we'll create our string. So we'll say, well, we'll put a semicolon at the end alone, and then we can just copy and paste those that would make change with the cat. And say, Yeah. And then if we go back down into our animal function here, let's go ahead and create a couple of lines just to make it look a little cleaner for each time we call this socialize C out and line. Then we'll just go ahead and do that one more time like that. And there we go. Anyways, I'll go ahead and zoom out here. So you guys can look over the code and copy it if you need to. And then we just need to do one last thing. And that is create a function prototype for our print animal function here. So remember, we have to do is copy this line here, paste it like that, and put a semicolon at the end. Now, if we run our code, There we go. You said animal name as spot. Animal age is to spot son's wife. Then we did the same thing down here. Anyways, that is up for this video. Don't forget to save what you have. Thanks for watching, and I'll see you in next one. 86. Chapter 15 (Classes): Class Constructors: In this video, we're gonna be talking about constructors. Now, a constructor is a special method in C plus plus that is automatically called when an object, I'll make class is created. And we'll go ahead and take a look at what that is, an adequate one and the use cases for it. Now, just to kinda clean things up here, I might go ahead and minimize our method here just so I can kinda keep everything on screen. And because we already know what this does, we don't really need to mess with that right now. And just zoom in here. Just know that I still have all this code here. And I'm not actually getting rid of it. And I'm also going to just go ahead and create some empty space here between our main function and our animal class just to kinda keep things separated. Now, let's go ahead and create a constructor. And C plus, plus, there are three different kinds of constructors. Now the first type of constructor we're going to talk about is the default constructor. And that looks like that. So when a constructor is graded, you're going to have the name of the class. Like that. Then you're going to have a setup parentheses with potential perimeters and then your code block and curly brackets like so. And this is what the default constructor looks like. And your class is going to automatically have this constructor if you don't create a constructor of your own. But once you do create your own constructor, the default constructor no longer exist. So in case I have confused, you are lost you at this point. Let's go ahead and just create a constructor around. I'll take a look at how constructors work. Red Bull or a speak method declaration here, we're gonna go ahead and create our constructor. So we're going to say animal being the name of our class, open and close parentheses. Then we'll have our code block below it. And inside of here, normally what you want to deal with the constructor as initialize all of your members so that you don't use them before they get initialized in the way that this is guaranteed because the constructor is called automatically when you create an object of your class. So if that here, we said underscore name equals 0 and then underscore age equals 0, like that. And actually because our name is a string, we just want to make that an empty string. So this will work just fine because it doesn't take any parameters. It isn't going to affect our code down here when we created our dog and our cat object, for example. So if we go ahead and run our code here, you can see that it works just as it did before. But now what we're gonna do is go ahead and define our constructor here outside of our class, like we did with the method. Okay, so to do this, we're going to do is copy or constructor and paste it down below here, like so. And then only have to do is declare it up top. So if we remove the definition here and just have what is on this top line and put a semicolon like that. And then down here, we have to do the same thing that we did here. So we'll say animal, followed by the scope resolution operator. Then it'll say animal again being the name of the constructor and also the name of our class followed by our parentheses. And lastly our definition there. So all we have to do is basically the same thing we did here, except we didn't have a return type, so we just put it before the constructor name that, and this will work just fine. So that is how you can declare a constructor either within your class or outside of it, just as we could with our function here. Now, one thing I do want to mention is that a constructor needs to be public. And that's because like I said, a constructor is a special method that runs when you create an object of the class. And since we create our objects of our class outside of the class, the constructor needs to be public. So do not forget that. Now, what if we wanted to add some parameters to our constructor? Well, we have a couple of options. One, we can add them to this constructor here, or we can overload it just like we can overload functions by creating a another constructor with the same exact name that takes different parameters. But let's go ahead and just change the one we have here. So what we're gonna do is say string name comma h, just like that. And we'll copy this down into our actual constructor down below. And then instead of just giving these a blank value to start off with, let us go ahead and put in what does our parameters here. So we'll say underscore name equals name, and underscore age equals age. Remember, these are actual class members. And these are the parameters that we got passed in. And that's another reason why it's nice to prefix your members with an underscore. Let you know that it is a member. When you work with it in the future, you don't have to go checking out things. So now if we go down here, you can see that we have an error. And that's because the only constructor now for our animal class requires two parameters. Because if you remember, since we created a constructor, the default one that would have looked like this, no longer exist. And you could have whatever Cody, one of them here. So I could say maybe print out a new animal was created or something like that. And all of this code would run every time that we created a new animal and use these parameters to call this constructor specifically, now, right now we only have one, but if we overloaded it, then we'd have to have these parameters to call this one here specifically, as we've seen when we overloaded functions and the past. So now if we go back down here, if we read what this error says, as soon as there's no default constructor for animal, meaning we now need parameters and we can fix this. I like I said, overloading this and having one that doesn't take parameters and looks just like those here. Or we can leave this and say, Hey, now if you want to create an animal, you need to put in these two values, and that's what we're gonna do. So down here, all we have to do if the dog is have two parentheses and then we need to insert our parameters. So instead of saying spot here, I'm just gonna put it right here. Mostly comma Minnesota sank to here. We'll put it right here. Then we can just get rid of this entirely, since we've already done that here, when it initializes, it's going to give the dog's name, the name that we inputted in the dog's age, the age that we inputted, and that's going to do the same with our cat. No, this is not changing the variables in class here. This is changing your specific instance of these variables. So doing something like this in our case here, is the exact same as that. So let's go ahead and do the same for the cat. So I'll have an open parentheses start with. And then if you see here, I can actually click on these arrows. And it will say, Hey, the animal takes a string name h. So we'll put in a name. I'll put an R comma and put an H. And now when we run our code, and it's going to run just as it did before. And that there is actually our second type of constructor. So we've already talked about what a default constructor is, which is basically just an empty constructor that doesn't take any parameters and doesn't run any code in the constructor we created here as actually the second type of constructor, which is called a parameterized constructor. And a parameterized constructor is basically just what it sounds like. It has a constructor that takes parameters and don't forget, the main purpose of a constructor is to usually give your class members here an initial value. So there you go. Then we have one last type of constructor, call the copy constructor. What a copy constructor is as a function which initializes an object using another object of the same class. So let's go ahead and take a look at what this looks like. And we're not going to take a look at the detailed version of this under the hood. And we're not going to be creating one ourselves as well as created by default. That works just fine in most cases. So how does this work? Well, for example, we have an animal called dog, in an animal called cat. But we can also have an instance of our animal class and just set it equal to a another instance that already exists. And this will essentially copy it. And that's where your copy constructor comes into play. So we could say animal, cat to liberals, cat and cat tail is now a copy of cat. And then down here, we can print this out by saying print animal. Then here we'll say cat two and then let's say yeah, like that, okay, and run our code. And there you go. We now print it out dog with the name spot H02 and it says spots says what? Because that's what I put on here than it did the same for Garfield than we did the same for cat two. And notice all the values are the same, but it says something a little bit different because we sent in a different argument with our print animal function that we created in a past video. So there you go. Those are the three different types of constructors. You have your default constructor, which is just completely empty and doesn't require you to have any parameters at all. You have a parametrized constructor, which is a constructor with parameters. And then you have your copy constructor, which like I said, there's one created by default. And a lot of times that works just fine, which basically lets you take an instance of a class and create a copy of it. And don't forget the constructors need to be public. And that oftentimes their main use is to initialize the members of the class. And here I just want to go ahead and delete this cat to animal here along with the function that goes with it. Don't forget to save what you have. Thanks for watching, and I'll see you in the next one. 87. Chapter 15 (Classes): List Initializers: In this video, we're gonna be talking about initializer lists. But before we do so, we're gonna go ahead and just remove some of the code here that we don't really need for future examples. So first off, we're gonna go ahead and remove our print animal method here that takes an animal as a parameter. So we'll go ahead and just remove that entirely. Then we'll remove R2 calls to that function as well. And lastly, it will remove the function prototype. For. Now, we just have our two animal instances or objects along with our animal class, the speak function and our animal constructor. And also you may have noticed, I just added some comments here, just kinda remind you guys what everything is. So an initializer list is what you want to use when you want to give the data members in your class. For example, our two variables here, an initial value. So in the last video, we went ahead and did this with our constructor here. And this is also how you might see it done in other languages, but in C plus plus, this is not the proper way to initialize your members. And there are multiple reasons for this. For one, certain member types can't be initialized this way. Also, it is less efficient in terms of performance and various other reasons. So let's go ahead and take a look at the, the proper way to do this. Also, if you guys remember, one way of creating a variable is a say, int x. And then instead of having an equals sign followed by a value, you can just say int x. And then in parentheses have the value, can put a semicolon at the end. And this is the exact same thing, right? The reason I wanted to remind you of that syntax option, that's because we're gonna be using something very similar when we initialize the members and our class here using an initializer list. Anyways, let's go ahead and take a look at how to create one. So right here in our constructor, all we have to do at the end of this parenthesis here, I say colon and then underscore name, for example, because this is the variable or the member that we want to initialize. And then in parentheses, we can just put the parameter that we're going to pass into it. So we'll set it equal to name, just like that, then any other members was a separate with a comma, so say comma underscore age and then in parenthesis, equal to age linger on their parameter here, and that's pretty much it. So this here is going to essentially do the same thing that we did here is we can actually go ahead and remove this code and our code block here. And also this cleans things up as well. So now let's say we had a ton of members here, instead of having to initialize them in our code block here and having x equals 0, y equals 0, z equals 0, and so on now is just all up here out of the way. And everything in our code block is actual code that we want to run on top of the initialization of our members. So this is the proper way to initialize members in your classes. And you should do it this way every single time. So you don't have to think about when do I do it this way and when do I do it the other way? I always do it this way. And remember to do this. All you're gonna do as after your parentheses and a colon followed by the member. And in parentheses, you'll have the value you want to set it equal to. And then for each extra member that you want to initialize, you'll just separate it out by a comma like so. Now this does not have to take a perimeter. We could set it equal to whatever we wanted to. But in this case, since we had parameters and our constructor that were made to initialize these values, we went ahead and just use our perimeter names also. One last thing that I want to note is that these need to be in the exact same order as your members are listed in your class. So because our name comes first and then we have our integer for age and needs to be initialized. Same exact order. This is incredibly important as they are multiple errors that can come up if you were to do these out of order. So make sure to always do it in order just as you have it here from top to bottom. And now, if we run our code, you can see that it ran just fine without any errors. And actually in our constructor here, let's just go ahead and add some code for printing out when an animal was created and specifying the variables within it. So say CL and animal was created. The following. Remember attributes as another name you might see for the members or variables in your class. And then lines like this. And then down here, we'll say name, colon space. We'll follow that up with me. And we could have also put underscore name here. It's really not gonna make any difference. And we'll create a new line after that as well. And then below that it will say page space like that. And we'll follow this up with h. And we'll create a new line after that as well. And just to show you that it makes no difference, we'll go ahead and put underscore age for this one. And if we run our code here, you can see that it worked just fine. And if we wanted to clean that up a bit, we can go ahead and create a couple of extra blank lines here. And there you go. And I'm just gonna go ahead and set this back to our parameter name here, being age. Anyways, that's it for this video, you now know the proper way to initialize members in your class, being an initializer list, which to summarize, all you have to do is at the end of your constructor here, right after the parenthesis, you're going to put a colon followed by each member in order and in parentheses, you'll have the value that you're going to set each of them to n. You could do this for every single constructor that you have. Because if you remember, you can overload constructors, meaning we can have multiple different ones for our animal class here, which will all have the name animal, since a constructor has the same name as the class. Anyways, thanks for watching, and I'll see you in the next one. 88. Chapter 15 (Classes): Encapsulation, Abstraction, & Properties: In this video, we're gonna be talking about a couple of different things. The first one is a concept called encapsulation, which has one of the four main principles of object oriented programming. But we'll also be touching on the other three main principles later on in this course. And then the other thing we're gonna be talking about in this video as a getters and setters, which will help allow us to put encapsulation into action. So what is encapsulation? Encapsulation in a nutshell is basically the process of wrapping or combining data and code that acts upon that data together into a single unit. And this single unit would be something like a class. And oftentimes what this leads to is hiding your data behind code that can interact with that. Let's go ahead and take a look at how to implement that properly into our animal class here, as well as the use case for it. So you know when and how to implement it into your own programs in the future. So let's get into it. The first thing we're gonna do here is up here in our animal class, we're going to go ahead and create another integer called health. So right below or int age, let's go ahead and say M underscore health. And then down in our constructor, Let's go ahead and add this variable to our initializer list. So after underscore age with age and emphases there, we're going to say comma underscore health. Then in parentheses, we're just going to set this equal to a 100. Okay? So this is just going to have a default value of 100 represents a 100% or however you want to look at it. Every animal is going to have a 100 health to start with. And we're not even going to take this as a parameter. This will just be the default for every single animal since this is our only constructor. Now, currently, if we wanted to change the health of our dog here, we can say something like this. So down here in our main function under where we created our dog, we can say dog dot underscore health minus equals 50. And this will run just fine. There's nothing to stop us from changing this variable to whatever we want it to be. And if we go ahead and print out health here, so we'll say C out dark dot health and just create a new line. So let's go ahead and also print out that this was the dog's health. So we'll say dogs equals alibi or dogs health and create a new line. Now, if we run a code, you can see hey, and animals created with the following attributes, name is Spot ages two and dogs health was F D. And I'm gonna go ahead and add a few extra blank lines here. So we'll just say M line a few times. Just so when we run our code here, it's a bit more readable. Okay? Alright, sorry, dogs health is 50. Well, what if we went ahead and subtracted another 50 health from our dog for whatever reason. But now if we run our code, you can see that his health is 0. Well, what if we do this one more time? So let's go ahead and paste it down below, just like that. Well, now this value is negative 50 and we might not want that to happen for one. And even if we forgot to program in a week, didn't want our health to be below 50 if we wanted to go and change that later. Currently, we'd have to do something like this. First, we'd have to find all of the times that we changed dogs health. And then we'd have to put this in like an if statement or something. You say f dog dot underscore health is greater than or equal to 50, for example. Then we want to do the following. And then we could paste that in. Or we could say something like that health is less than 0. Then we want to set a dog that health equal to 0. Like that. And this would work, but this could get pretty annoying. And then if we wanted to do anything else with this later on, now we got to find all these spots again and change them all. Once again. Hopefully you can see that this could become a really big issue over time, especially as our program grows. And we might have multiple sections of code or a dog's health would be removed. Or if we're making a game here, there can be multiple things that affect our dogs health or something like that. So what can we do instead? Well, let's go ahead and leave this here for now. Then let's go up to our animal class here. So what we can do is have a couple of functions for each of our members here, for getting and setting the values respectively. We can have all of the rules for getting and setting them within those functions. And if we make these members here only accessible through those functions, then we know that nowhere in our code can there be something like this that will change the variable in a way that we didn't specify. So let's go ahead and do that. So these functions that we'll be creating something called getters and setters. And before we create these, what we're going to do is make all of our members here private, so they are only accessible from within the functions for our getters and setters. So down below our constructor declaration here, we're going to say private colon. If you remember, this was our access specifier. Now we have our public members and we have our private members. And what we're gonna do is copy all of our members from up here. We're actually going to cut them and then we'll paste them down here. And now they are private and freight. Now, instead of creating one for each of these, we'll just say that name and age can't be changed once the object is created using these parameters here. And so we'll just create one for health. And although we could have created one for these, this will just save us some time and you'll get the idea of how this works regardless. So up here, we're going to create a couple of public functions. 1 first setting health, and one for getting help. So let's go ahead and do that first. We'll say Get out. And then in parenthesis, we'll have no parameters followed by our code block, like so. And what we're gonna say is returned underscore. And then down below, this will create a function for setting health. So we'll say void set health. And then in parentheses here, we'll actually take a argument. So we'll have to create a parameter here and we'll say int. And then in our code block will say underscore health equals. And this is the most basic syntax for creating a getter and a setter. And if you come from another programming language, you may recognize these as properties. But anyways, this top one here is our getter because this gets the value for us and this bottom one here is our center. Now, you might be thinking, how is this any better than if we just made it public? Now, currently it's not. But we'll take a look at how this can become more complex and come in handy in just one moment. So now if we go back down to our main function here, you can see that we have an error saying, Hey, health is not accessible and that's because we made it private. So what we're going to have to do now is the following. So right here instead now we're going to have to say dog dot set health. Then we'll put in a minus 50 like that. And we'll go ahead and paste that three times to replicate what we had before. And then right here, when we wanted to print out the dog's health, will say dog dot get health. And this will return the integer value for us. And now if we run our code, you can see that this ran just fine and it works the same way as it did before. And you might be thinking to yourself, Well, how is this any better? Well, let's go ahead and take a look at the benefits of what we just did. So if we go back up to our animal class here, I want to show you one thing quick. First off, notice that we are able to access underscore health here before we even mentioned what underscore health was. And by the way, real quick, normally when you have public members and private members, you'll want to have the public ones first so that when somebody looks at your class, they get all the information they need right here. As normally the private members really aren't as important if they'll need them at all and they should be able to tell everything that's going on from right here. But anyways, back to what I was talking about, why are we able to access this when this is down here. And that's because what we have here currently, when it comes to classes, is not actually read the way that it looks and how it has actually read out is as such. So what we're gonna do to show you how it actually looks to our program when it reads through our code here is, we're going to just go ahead and copy these two functions and paste them down below here. And we'll just paste them at the very bottom right now. And let's also go ahead and move our speak function down there as well. Sorry constructors at the time, like so. And now we're going to change these up here back in our class. Let me zoom out a bit so you can kinda see Britain change these up here to just be function prototypes. So we'll put a semicolon and then we'll remove the code block. Each of these lines here. And then down here at the bottom, we're going to go ahead and add our class name, followed by the scope resolution operator, like we did with our speak function. So let me just copy that and paste it into both of these. This is how our code was actually read before. So that is why we can have the variable declaration after our method definition is because it was actually read. That's where we have our prototype or the declaration of our function here, and then the definition after the end of the class. So that's something that doesn't work normally. But when it comes to classes, for example, it does. So I just wanted to give you a heads-up on that. She weren't super confused. Why did that was working as that never has worked for us in the past. So now let's go ahead and take a look at how these getters and setters are actually useful. And also what we've done here by having this Health member be private and having it encapsulated within these two functions. Here is a prime example of encapsulation. Now what we can do in our setup here is the following. So after we say underscore health. People's health. We can also go ahead and set a Min and a max value for this. So now if we wanted, we could say F underscore health is greater than a 100, for example. Then underscore health equals a 100. And below that we could say f, or we could just create another if statement here. So we'll go ahead and say F underscore health is less than 0. Then underscore health equals 0, just like that. And now when anything affects our dogs health or any object or instance of our animal class. So other, that was the dog or the cat, or another animal that we might create anytime that we try and affect the health, we now have to go through this set health function and we can add any code to this later without breaking our code down here. Notice we didn't have to mess with anything down here to add a minimum and a maximum value now, and we can either print something out anytime or health was set to display our new health value if we want it or whatever you want to do. And we don't have to change anything down here. So because we have these getters and setters, our code is much more dynamic and we don't have to worry about all the times that we tried to get and set this variable. Also, we don't have to worry about anything bypassing these functions and just changing this variable directly because it is private, meaning it is only available within our class, which is why our function here, which is a part of our class directly, has access to it, but something like this main function would not. So after these two if statements, I'm just going to say max health equals 100. And down here, I'll say minimum health 0, like that, just to remind us of what these are doing here. Now, if we run our code, you can see the dog's health will only go down to 0 now because of this extra functionality that we added. And notice once again, we didn't have to change anything done here. Anyways, that's it for this video. But let's go ahead and recap what we've learned here. For one, when it comes to classes, you can actually have a method, declaration and definition before any variable declarations that it uses as it is actually read out like this. However, that being said the proper way and the way you should do it from here on out to write a method definition is to have your declaration inside the class and the definition outside the class. For one, this helps with cluttering up your code and in the future, if he ever separate this class and do its own header file and CPP file or C plus plus file. This will make things much easier. So you should always have your definitions outside of the class. So as a general rule, your class should only have declarations and outside of the class should be your definitions. So just keep that in mind for the future. Now, the other thing we learned is how to create a getter and a setter. And although we already knew how to create functions, we now know the use cases for creating specific functions, for getting and setting private variables within our class. And we also learn the proper way to list out our public members versus our private members being public on top and private down below, you can do this whichever way you want it. But this is the proper and probably the most preferred way to do it. Because like I said, as someone looks at your class down the line breathing, if you look at the code in your class, it is nice to have all the public stuff up top here because this is usually everything that I care about. And oftentimes the present members are kind of like a in the background thing that you may or may not care about. Actually taking a look at. If I class was larger, there may be even more private information to have to sift through to get to the stuff we care about if these were written the other way around. So as a general rule, you should have your public members up top. And then we learned how to use our getter and setter and the use case and some of the benefits of creating those. So the main things to take away is how to utilize encapsulation. So you have all your declarations within your class and your definitions outside of it are written outside of it. And then when you can, you should always have your members private so that they are being accessed in ways that you don't want them to be slowed down the line. You want to change how they worked with. You don't have to go looking for it everywhere in your code and looking for how to fix it, especially for an errors or something like that. All you have to do is look right here. So what we've done here is actually work with two of the four main pillars of object oriented programming. One being obstruction and the other being encapsulation. And we'll talk about these in a little more detail in a later video. But they quickly summarize what these are and how we practice or worked with both of them. Abstraction as the method or process of hiding unwanted information. When we took our members are our variables here and hit them from the outside world or from the rest of our code, we were practicing abstraction. Whereas encapsulation was the actual process of containing the information. And so these two work hand in hand. So we abstracted the information or a hidden from the outside world or the rest of our code by making these members here private, for example. And then we encapsulated this information within these methods or functions as well. So there you have it. Anyways, that is it for this video. Don't forget to save what you have. Thanks for watching, and I'll see you in the next one. 89. Chapter 15 (Classes): Inheritance: In this video, we're gonna be talking about inheritance. Inheritance is actually going to be our third pillar of object oriented programming. Now, these are in no specific order. This just happens to be the third one that we've talked about thus far. So what is inheritance? Well, it is the ability of a class to inherit or derive properties along with characteristics from another class. And we'll go ahead and create and take a look at an example. So let's get started first things first, let's go ahead and remove some stuff from our code just to kinda clean things up here. So what we're gonna do is outside of creating our dog and cat, we're not move the other lines of code and our main function just like that. And then I'm also just going to go ahead and minimize our setter and getter since we already know what they do. Then I'm also gonna go ahead and minimize our animal constructor as well. Then I'm gonna go ahead and move our speak function. So I'll just cut that real quick down below our center here. Then I'll also change it up here where we have our declaration of it. So I'll just put that down below like that. And there we go. Right now, this is a lot less code to look at. So what we're gonna do is instead of having our animal class and then just stating the name for our object or an instance of it as a dog and a cat. Marxism and create two subclasses and or derived classes. One for a dog, in one for a cat that are both going to inherit from the animal class, but the both have something unique about them. And what this does for us is allow us to get everything that we've created as part of our animal class and put it into another class without having to repeat. It cannot only lead to human error, but it also just makes things messy when we know something like, Hey, every animal is going to have this functionality and something else. So what we can do is just create some subclasses were derived classes that inherit from this. So let's get started down below our function definition here, we're going to do the following. We're going to say class dog. And then inside of our curly brackets, we'll have our code for our dog class, but we don't just want to create a class called Dog. We want it to inherit from our animal class because a dog is an animal and we've already created a definition for what every animal has. Alright, so what we're gonna do to inherit from our animal class is simply after the name of our class, say colon, followed by an access specifier. In this case we're going to say public. And I'll get to that in just a moment. And then the name of the class that we want to inherit from public animal, just like that. And there we go. We've now created a subclass or a derived class. You often hear these words used interchangeably that inherits from our base class or our super class. So you may hear this referred to as super and sub or base and derived. Now, why did we say public here? Well, let me tell you. So if you put public before the base class name, here's what it's going to happen. So if you remember, you have three different access specifiers. Public, protected. And private. Public makes all of the members available everywhere. Protected, makes all of the members only available in the class and any class that inherits from it. And then private makes all of the members only available within the class specifically. And so it happens here is if you say public here, all of the members of animal that we're public are public for our dog class. Everything that was protected is protected for our dog class. And everything that was private is private for our dog class. That being said, private members from a base class cannot be directly accessed from the derived class. Whereas protected members can, meaning anything that was private, will not be accessible directly from our dog class here. So what would happen if instead of saying public here, we changed this to protect it? Well, essentially, anything that we inherited that was public would be protected. Anything that was protected would stay protected. And anything that was private, which stay private. So now we're basically just cutting out anything that was public and moving it up one in production. And if we said private here, members that we inherited would all be private. However, only the things that were private in the base class would not be accessible directly from our dog class. And anything that was made private from putting private here will now be private in our dog class and how that affects us. If you're a bit confused there is, you can inherit from a subclass like this. We could create a another class that inherited from dog, which inherits from animal. And you could do this over and over if you want it. And just like we could have other classes that inherit from dog. Dog can also inherit from multiple classes as well. And to do that, all you'd have to do is say animal comma. And follow that up with the other class or classes you'd want to inherit from each separated with a comma. And then you would put the access specifier first, once again for each one and follow that up with the name. So everything that we have here, you'd have to put after this comma for every other class you went to inherit from. So pretty simple. Now we're not going to be diving into all the different forms of inheritance that you might see in this course or work with. But just know that you can inherit from multiple classes. And you can even inherit from subclasses. And you could even inherit from those as well if you want to, and the list goes on. But for us here, what we're gonna do is just take a look at the most basic form of inheritance, being having a class that inherits from a single base class. So anyways, let's go ahead and move forward. Hopefully that all made sense to you. So because we said public hear everything that was public, would've stayed public in our dog class because it inherits all of our animal class members. Anything that was protected, which we didn't have anything would've stayed protected and anything that was private to our animal class will not be directly accessible from our dog class because it was private, only accessible to this class here. And now what we're gonna do is we're going to create a constructor for our dog class. But one thing we need to be aware of is that anytime a dog is created and animal is created in the background. And if you remember, we actually got rid of our default animal constructor, which didn't take any parameters, meaning currently to create an instance of an animal, animal here needs a string for a name and an integer for an age. So how do we go ahead and implement that? One thing we could do is create a default constructor that doesn't take any parameters. And although that would work and allow us to create a dog without having to have any parameters put in to initialize our name h here, it's not really what we want as this would bypass the way we, we're initializing our name and age by allowing us to give it a name and age down here. So instead, we could just go ahead and inherit all of the constructors from our animal class by saying using followed by the name of the class being animal to Colin's being our scope resolution operator, followed by the word animal again, and a semicolon at the end. What this would do is inherit all of the constructors. So now if we came down here and said something like dog, my dog can put our parentheses. You can see that one of these options, and by the way, I just scroll through those and down arrow keys says that, hey, we need to have a name and an age now, and we can just throw in a name and an age like we've done in the past. This would work just fine, but we're not gonna do that. And I'll show you why here in a second. So we'll go ahead and get rid of that. But this is how you can inherit all of the constructors from your base class. Instead, we're gonna do the following. We're going to create a new constructor for dog by saying dog, open and close parentheses. And then we're going to put a semicolon at the end as this is just gonna be our declaration. But this one here is going to take the same parameters that our animal class did. So we're going to say dog name, comma, int age, just like that. Then we'll go ahead and copy this line and paste it down below our class to actually implement the definition. So we'll meet our open and close curly brackets as always. And then up here we're going to use a list initializer for the constructor of our animal class. So we'll say colon animal. And then in parentheses will insert the parameters that are animal constructor requires. So we'll take this name being our perimeter name here, and just insert that as the name or the string variable for this constructor up here, then we'll say comma h, like so. And then if you remember, we have to go back here and say dog being the name of our class, colon colon, to specify that this is the definition for this year. And now everything will work just fine. So now when we create a dog, this is our only constructor for a dog and it takes a name and an age it is going to do now is insert this name and age into the name and age for the constructor for our animal class. And we'll go over all of this here at the end. So don't worry, if you're a little bit confused, just following along. And then what we're gonna do inside of our constructor here. And let's go up to where we have our animal constructor and just remove all of the code within it like that. We're going to paste it in here. And then we'll say C out. A dog was created with the following attributes, name, name, age, age. And if we wanted, we could even say something like c out health. Colon space 100, since we know that our health is always gonna be 100 at the start there. Then we'll just go ahead and throw in a semicolon. And then what we're gonna do is actually copy the n lines from up here or cut them, I should say, paste them down here, right? So now anytime we create a dog, and it's going to say a dog was created with the following attributes. Name being whatever name we put in when we create a dog, age being whatever age we put in when we create a dog in, health will default to 100. And that's because up in our animal constructor, when this is Ram here, health is automatically set to 100. Then let's go ahead and copy all of this here. So everything from our dog class to our dog constructor and definition here. And paste it down below. And we're going to say class, cat, change it from dogs string name is Cat, String name. And down here will change both of these two, cat as well. I would say cat on colon, cat, and we can leave the rest as is. And then down here in the actual code block will say a cat was created with the following attributes. And there we go. Now down here, instead of creating two animals and calling on a dog and when a cat, let's create a dog and a cat. So let's go ahead and remove this. And we'll say cat. And we'll just call this my cat for now. And then in parentheses, we need a string and then one for the name and one for the age. So we call this Garfield once again. And we'll set the age to two missing comma t like that. Don't put a semicolon at the end. Now notice right here, it says that cat, cat or the constructor for cat is inaccessible at line 81. And that's because we didn't make our constructor public. Remember, members late class are private by default. Constructors need to be public for them to work. So we'll just say public. So in put a colon at the end, and I'll just go ahead and indent this to make it a little more readable and we'll do the same for our dog. So up here, we'll say public colon. Then I'll go ahead and enter that in just to make it more readable. And there we go. Now the error has gone. Whom are all good to go? Then down below this, if I said my cat dot, notice that I can get the health because I have access to that public function. And I can set the health because we also have access to that public function. And we can also speak, which is the function we created up here for just printing out whatever string you input. So let's go ahead and mess with that just a little bit. So we'll say my cat died. Set Health. And we'll just go ahead and say something like ten and put a semicolon at the end. And this will set our health to ten. Because if you remember back in our function here, SAT health just takes the health amount that you input and sets it equal to that. And if we had wanted to, we could have created a function for changing health by a specific value. And let's just go ahead and do that quick. So up here, we'll create a new, another function for changing health. Want to say void, change health. And we'll take in hand health. Just like that. I don't put a semicolon at the end. This will be a changer function for help. Okay, I'll just add a little bit to these comments here. So you have a getter for health, a setter, and a changer. And if we copy this declaration here, and then go down here, and I'm actually going to go ahead and minimize our cell function for just a moment as well as our speak function. And basically between our set health function in R speak function. I'll just go ahead and paste that in. Then we'll create a new function like so. And inside of here, I'm gonna go ahead and copy all of the code for our set L function and paste it in here. And now we'll say underscore health equals health, then we can leave the rest. And what this will allow us to do is say change health and then insert either a positive or a negative value and add that directly to our health member. And notice we have this error unless because we still have to add this here right before our function name. And that will clear that write-up. Then we'll go ahead and minimize those. And now if we go down here, we can say something like my cat, get health, to get our health value. And we'll go ahead and print that out. So say CL health, then we can go ahead and say something like my cat, change health and we'll change it by negative 15. Let's just say our cat got hurt or something like that. Then we'll print out health again. So we'll just say C out, my cat died. None. Let's also go ahead and create a dog. So down below here and we'll say dog. My dog will change its name to spotty. And its age 23. I'll just put a semicolon at the end. And let's also go ahead and add in a couple of blank lines here. So let's say in line and line like that and run our code. And as you can see, it actually looks a little bit weird. So let's go ahead and fix that real quick. So up here inside of our dog and animal constructor, we need to put in an N line after H and one after age and cat. Now, if we run our code again quick, we can see that that portion that fixed up. Now we just have this weird hundred and 85 here. And the change that all you have to do is add in some text here. We could say something like c out. Garfield. Health is colon space. We'll add in another insertion operator like so. And then we'll just copy this down. And lastly, it will just create a blank line after we say Garfield is health is the first time here. So at the end of the second line and run a code, and there you go. Now said a cat was created with the following attributes named Garfield H2, Health 100. Garfield is health is 100. Then we changed it with our change health function, which we were able to access by just using the name of our instance or object here because this function was public. And then we were able to mess with the private member being health of the animal class that is derived from, because we had these getters and setters available to us. And then we printed out his health, again being 85, since we just took out 15 from it, we just created a dog which did the same thing. If we print out a dog which created with the following attributes name spotty age 300. So there you go. Anyways, I know that was a lot that probably take in. So let's just go back and go over all of our code here quick. So I'm going to go ahead and zoom out here a bit and minimize everything that we have thus far. So let's go ahead and take it from the top. As I know, we've been working with those code for quite awhile now as we've learned and add it to it step-by-step. So we created a class called animal, then we give it some public and private members. Now, let's start off with a private members. So first, we said essentially that every animal is going to have a name. We're going to store this in a string. They're going to have an age that will store an integer and a health value. There will also be stored in an integer. And every time an animal is created, we're gonna go ahead and initialize these values. And we did that with our constructor, which if you remember, there's just a function that's gonna be called automatically anytime an animal has created. To do this, we use our animal name here, followed by some parameters that we wanted to take in, in the definition for that we have right here down below. So it doesn't run any code on its own, but it takes a string and an integer, one for our name variable in one for age variable. And we used a initializer list right here with this colon followed by each member that we wanted to set in order. So he said underscore name being our member name, they're going to be set equal to name or the argument that was passed in for this parameter. Then we did the same for H right here. And then lastly, we went ahead and gave health and initial value of 100 every time we didn't take any user input for this one. Here. I'll go ahead and minimize that now. Then we created a getter for health, or just a function that allowed us to get the value of our private health variable. And this was to encapsulate it so that it can only be worked with within the perimeters that we decided, which is why we have a getter and setter for it, as well as this change method which we just added. So let's go over those right now. So get health doesn't take any parameters. All it does is returned back to the caller or the code that is calling this method AND OR function. So their returns an integer because our health member is an integer and it just returns health. So if you want to know what health is, you can call this function and we'll give that to you. And we could have thrown any other code in here we wanted to, but this was just a basic gutter that we decided to create. Then we have another function for setting health. This allows the color to insert any health value they want and we will set Health equal to it. How ever we throw in some extra code here to say, Hey, we're going to have a max health and a minimum health. So if health, after being set to whatever the user inputted here is greater than 100. Hey, we're gonna go ahead and put that back 200. And if it is less than zero, we'll go ahead and set it equal to zero. Then we just created this new function called change health, which allows the color to change the health value by whatever amount they put in. So they send our function here, an integer and we add that value to health. And this could have been a negative or a positive number, and it'd be added to. Then same thing. We said, Hey, if health is now greater than 100, we'll set it back to 100 and health is less than zero, we'll set it to zero. And we could have thrown in any extra code we wanted here so we can actually print it out back to the user health was this, Andrew was changed to this or whatever we wanted, but we just created a basic function for changing health by whatever and then making sure that it didn't pass the max that we want it and the minimum amount we wanted to. We could have created some getters and setters for the name and age as well. But we just did this one here for simplicity sake. And lastly, we have one last function for our animal class, which just allows any object of animal to say speak, along with inserting a string. And it will just print out that sound there. And we might even come back to this and do some more with it in a later video. So anyways, hopefully, that all made sense to you. Let's go ahead and move it down to our dog class. And we're just going to ignore the cat class since it will be the exact same essentially. So we said, class dog is going to inherit from class animal and any members that we inherited, we wanted to leave at the same exact accessibility that they were before. So any private members stayed private. However, we don't get access to them because private members are only available within the class they are created. But we did get access to the public and protected ones, and they stayed public and protected here. And how do we set protected? All the public would have been made protected. And how do we set private? Everything would have been made prior to that. But just remember that we would have had access to these variables except for the ones that were originally private from animal. So e.g. what I mean by that is if we said prior to that animal health public function e.g. would have now been in here essentially what just made private. Okay. So anyways, we just left the members as they work. And then we went ahead and said public here and created a constructor for dogs. So to do that, we said dog followed by some parameters. So we took a name and an age to match what our animal took here. Okay. That's because anytime at dog is created, an animal is also created essentially because a dog is an animal and because we didn't have a default constructor that doesn't take anything. We need to have a constructor that matches the only available constructor for anima. And remember, we could have created multiple constructors for animal and multiple for dog, but we just did it this way. I can make sure that anytime a Douglas created a nameless given and an age was given, and we could have put in as many things as we wanted to. We could have put in a name and age, a breed or something like that back in our animal constructor in London. The same here, but we just did it this way for simplicity. And then what we did is create another initializer list to call the constructor essentially for animal directly from here. So whatever name and age were inserted when a dog is created, will now be inserted into the name and age for our animal constructor. Because like I said, every time a dog is created, an animal is created and we just did it this way directly. But in all technicality, how do we want it to? We could have taken nothing, created a variable called string, name, equal to spot, and just santa into our animal constructor every time a dog was created. Then we just printed out a bunch of stuff. And this is pretty self-explanatory. We've worked with functions before, alright, and then we did the same exact thing for cat. Obviously this class was called cat and we said a countless graded instead of a dog, What's graded? And then lastly, if we come down here, we created a cat called my cat, and we gave it a name being Garfield and an age of two. Then we printed out Garfield is health is, and then use the getter function, right? And we were able to access that directly from my cat because that was a public member. And when we inherited it, it was still publics were able to access that just like this and get the Health value. Then we changed this health because this was also a public function and printed it out again when we just created a dark down here. So there you go. Just to show you something here. If I were to go up to our cat class here and change public animal to protect the animal and try to run our code. You'll see that we get an error. And that's because now these functions, as far as a cat is concerned, because that is the class we're working with is protected, meaning it is not going to be available to anything other than itself and any classes that inherit from it or derive from it. In same if we had said private, it would have made those functions private as far as cat was concerned, and they wouldn't have been available to us here at all. Okay. So just wanted to show you how that works there, since we had a reasonable example available to us. And so now I put that back to public in our code works once again. However, just to show you one more thing with that, if I did go ahead and set that back to protected or even private for that matter, they would only be private as far as a cat class is concerned. So down here, I could still say something like animal, my animal. Give it a name. Let's call it like Jeff or something like that in an age of one. And our semicolon at the end. And we can still say my animal health like so. In this will work just fine. That's because as far as an animal is concerned, get health is public, right? And if I comment out this code here momentarily and run our code, you can see that this work just fine. We cleared it a dog, the name of spotty, age of three and health was 100. And then we also created an animal. Notice we didn't print anything out though. And that's because our Animal constructor no longer prints out any text. And the main reason we did that is because otherwise, every time we created a dog or a cat, not only what our cat and dog constructor be called, but the Animal constructor would have been called as well since when a dog or cat is created, an animal is created. Alright, and this is our only constructor available. So there you have it. And just to show you that this does work, right here, I can say my animal health and the pKa value is 100. Anyways, I'll go ahead and remove that and uncomment these lines of code. And go ahead and change our cat class here back to inheriting public animal, not private. So there you go. Everything is working once again. And I'll also remove this animal creation down here. And I know that a lot of our previous examples worked with some very small code. But now that we've learned a lot of the basic stuff and have a lot more in our tool belt. I wanted to go ahead and create a larger program with you slowly over the course of a handful of videos, just so you can see what that might look like in the process behind that and how to put everything together piece by piece here. So hopefully you've enjoyed them. Anyways. There you go. That's kind of a summary of everything we have currently and there will be using in the upcoming videos anyways, if you understood everything so far, congratulations. You're not going to have any problems in the videos going forward. However, for those of you who are having trouble, Don't worry. In the next video, I'm gonna be taking everything that we've been talking about with classes and breaking it down into a smaller form or example for you to see and work with. Anyways, that's it for this video. Once again, be sure to save where you have. Thanks for watching, and I'll see you in the next one. 90. Chapter 15 (Classes): Classes Recap: In this video, I'm gonna be taking everything we've learned in the last handful of videos about classes and breaking it down piece by piece in a more basic form. As I know, there was a lot going on, on screen. So hopefully this video will help you out if you got a bit confused with all that we had going on, That being said, don't delete the code that you have because we're still going to be using it in the upcoming videos. So if you do want to follow along, I recommend saving that in opening a new project if you want to follow along here. Otherwise, you can just watch what I have on screen and just make sure that you understand each topic as we go through. Anyways, let's get started. Alright, first off, let's go ahead and go back to the beginning. How do we create a class? Well, let me say class followed by the name. So you might say something like animal, then we'll have a set of open and closed curly brackets and a semicolon at the end. And there you go. That is how you create a class. Then in the following video, we talked about how to create an instance of a class. And well, how do we do that? Well, we just say the name of the class followed by the name of the object or this instance of the class. So I might say animal, my animal if I wanted. Simply put a semicolon at the end. And now we've created an instance of this class here. In this instance has access and its own version of any members and or variables of this class. So if we said string name, like so, and for right now, we'll just give us a default value like this. And now my animal here, or instance of our animal class here, now has its own version or instance of this member or variable here. And that's because it is its own animal object as its own instance or version of the class that it is made up of. So down here, We could say my animal being the name of this version or object or instance or whatever you wanna call it dot name and set it equal to spot or whatever we want it. However, if you guys remember, when it comes to classes, every member or variable or attribute or whatever you wanna call it of the class, is by default private, meaning it is only available to that class. So if we want to make it available to the objects or instances of the class, we need to make it public entity that we can simply say public colon space like that. And there we go. Now, everything under here will be a public member. And if we wanted to make some private members, we'll just say something like this and we can put anything we want it below there. And these would only be available to the class. And then there's one last access specifier called protected, which protected members are only available to the class and any subclasses or classes that inherit from it. And we'll get into that here in just a minute. So name is public. We can say my animal dot name now equals spot because my animal has its own version of this name variable in, we can prove that by doing the following and create another animal and call this my animal kill. Say my animal to mean is equal to Solomon. And if you noticed, we just use this like we would any other variable, except we have to specify which one we're referring to since each one has its own instance. And then below that will print out both. So say C out my animal diet name. And then we'll say CL, new line, my animal to me. And like I said, because each of these have their own name. My animal's name is Spot and my animal 2s name is Solomon. So there you go. The other thing we talked about was creating a method in a class. And I'll just go ahead and show you the proper way to do this. So let me go ahead and remove our second animal and printing out the name here. Alright, so now we just have our class that we created held by a public string so that objects are instances of that class, have access to that variable. Now remember they're each going to have their own instance of it. And then we don't have any private variables or functions, but we have a space for it here as well. So far all we've done is create an animal called my animal and change its name to spot or it's instance of the name variable. Okay, so now how do we create the class method? Well, first, we create something that looks very similar to a function prototype because it is basically the same thing. So we'll say something like void, open and close parentheses and a semicolon at the end. Just like that, remember, inside your class, you just want your declarations and outside your class, you'll want to put your definitions. This is the proper way to do things. Like I said in the past. This will help when transferring our class here into a header and CPP file. And it just makes things cleaner and we'll get into that in a later video. But right now, don't worry about it. Let's go ahead and move on to creating this function. So what we have right here, then, we'll create the actual body or definition of the function. And we'll just say C out. My name is colon space like that. Given the line afterwards. And then to specify that this function we just created as part of this animal class here, we'll just copy the class name and put it right before the name of our method. And then we insert the scope resolution operator right there. Now we've declared and defined a function in our class. And because of the public, our object of a class or an instance of it has access to it now too. And actually up here, we want to print out the name as well. So say my name is followed by name and then like, alright, so down here we've created an instance of our animal class, changed version of the name variable. And then we'll just say my animal print name, just like that. Open and close parentheses and a semicolon at the end, because we call this just like we would any other function. And if we run our code here, there you go. My name is Spot. And another thing we've talked about is that this animal type works just like any other types. So you can actually take this as a parameter. So real quick, let's just go ahead and remove this animal function and warm it right here too. So now we just have our one variable here again, n, or move that line there. And then I'm just going to say void name. And in parentheses will take an animal, an animal. Then right here, I'm going to have a code block and I'll say CL. My name is one space. I'm going to print out the animal's name. So we'll say animal dot name in because this is creating a copy of whatever we pass in, this value will be the same as the one that we pass in. So up here, I can say print name and then parentheses. I can insert my animal just like that and put a semicolon at the end. And we do need a function prototype up above. So I'll just copy that. I take that and put a semicolon at the end. And if I run my code here, and there you go, we printed out my name is, but you could also use this as a return type. We can pass this by reference, et cetera. So you can use this just as you would pretty much any other type. The only differences is that this type is user-defined. We created it, our self essentially, alright, because we defined what an animal is. Alright, anyway, so I'm gonna go ahead and get rid of that. Uh, yeah. So we just have these few basic things here. Then next we talked about constructors, which are just special functions that run automatically. When an instance of the class is created. By default, you have one that looks like that. And by the way, all constructors must be public as a reminder there. So the default constructor looks something like this. Animal being the name of the class, open and close parentheses and open and close curly brackets. And if you create a constructor of your own, this one no longer exists. And you can have multiple constructors allowing you to overload it in multiple ways just like you can overload functions. And by just having even two constructors, you are overloading it. Sing as the name is the same as the class name. Alright, so let's go ahead and create a constructor. And we'll just start off by creating a copy of the default one. So I'll just say something like this. And there you go. We now have a default constructor. If we wanted to do this properly, we could declare it right here and then the finite bound. Well, so don't forget, we need to save what this little part f. So we'll say animal held by the scope resolution operator animal, and then our curly brackets. So here's our default constructor. And this is what allows us to create an object or a version of an animal without having to take any parameters. And that's because this doesn't take any, but we can create another constructor that does. And then before I forget, remember anytime we have a variable that is a member of the class, we just want to put an underscore before the name there, just so we know, hey, this is a class member while to change it down here as well. And like I said, we can go ahead and overload this constructor here. I just creating another constructor. And this one is going to be what you would call it, a parameterized constructor. Because it's going to take some parameters. So right here, we're going to say the animal. And then in parentheses, we'll say string, color. Like that. We don't put a semicolon at the end. And then we'll create the definition for this envelope. I'm just going to copy this and paste it down here. Then we'll have our curly brackets for the code block. And then we'll say C out. A space conduct color. Space animal was created just like that animal. Put a semicolon at the end and we have to specify where this constructor is coming from or what it does for. So we'll say animal before it, followed by the scope resolution operator again. So there you go. We now have another constructor. And now if we want to use that one instead of the default ones, I'm going to just go ahead and erase all this code. We can say animal. Animal. In parentheses, we'll put in a string. So I'll say personally that put a semicolon at the end. And if I run the code here, and I'll say a blue animal is created. And that's because that's the code that runs when we created this animal, since these are the parameters that we used and another use for a constructor or one of the main uses for it is to make sure that your class is ready to use. And what I mean by that is you want to make sure that everything has an initial value. As normally, you don't wanna do this right here, all your declarations here and your definitions down here. Alright, so what's the best way to give name and initial value in our constructor. Well, right after the parentheses, we're gonna do is say colon, underscore name in parentheses will put the value. So we'll just say double-quotes and we'll put nothing there. And we'll do the same down here, except that down here, we're going to say underscore name. And then in parentheses, we'll say name. And to get this value here, we'll just add another parameter. So we'll say string, color, string. We need to make sure to add that to our declaration of this method up here. So now we've created another constructor that takes a string for the color and a string for the name. And it's going to insert whatever name we put in into our name variable. The reason I had the error here is because I just didn't have that colon right there. And there you got it down here. We got an error because we no longer have a constructor that just takes one parameter. And if I remove that and put it in an open parentheses again, I'll see my different Let's do options. And it says I need a color and a name, or I could just do nothing since we have this default constructor. So like I said, the main purpose of the constructor is to make your class ready to use by initializing all of the members within it. And then you can also have any code you want to run. An instance of your class is created using that constructor. Like I said, the best way to initialize it is with an initializer list. And all you have to do is say colon followed by the variable and the value you want to give it in parenthesis. And if we had more than one variable, we would say comma. And then put it in the other variable name, and then put that value and parentheses. And do remember that they need to be in order from top to bottom, and you just go left to right here. So I'll just go ahead and remove that since we just have the one variable. And there you go. Now, I can go ahead and put in a color. So I could say blue again. And then our name will be spot. And now that we have done here, we can say a blue animal was created, named by the name. And there we go. If I run my code here, that should say a blue animals created named spot, and that's what it does. Then another thing we talked about was encapsulation. So let's go ahead and take a look at that. Basically, right now, I can go and mess with my string variable. I could say my animal dot underscore name and set this equal to whatever I wanted. And there's nothing to stop me. And that may not seem like such a big deal here, but trust me, you don't want to have your members public. If you can avoid it, you want to have everything inside of a getter and setter function, which we'll get into here in just a moment so that you can define extra rules and put an extra code anytime something wants to mess with a variable. And if you decide to do something with that later, your code is now more dynamic because you only have to look at those two functions rather than trying to find everywhere that that variable was adjusted. So let's go ahead and change those. First, we will take our string variable here and put it under private here. So now this is a private member, which is what you want. And then up here we'll create two functions for a getter and a setter. And before I do that, I'm just gonna go ahead and minimize this here. So it's not in our way. So we'll say set name as the name of the function, and we'll put a semicolon at the end, and this will take a string name. Then we'll say getName down below it. And this will take nothing. However, it will return a string. And this one up here will return nothing. Okay, So we'll say void, setName, string name, and String getName. And that'll be it there. Then it will create the definitions for both of these down below. So if I copy this one here and add in our block of code, the most basic code for a setter would be to do the following. I would say underscore name equals name. And now this will allow anything that calls this function to change this private variable. Then we'll do the same with our getName method here. Or function will create the definition down here, like so. And we'll say the term underscore name. Then we need to make sure that we specify that both of these functions are part of our animal class. So we'll say animal followed by the scope resolution operator on both. And avoid needs to come before that. Right, because it's gonna go right before the method name and seamless string. So I'll move up to the other side, right? And if we run our code now, so the blue animals created named spot, so everything is still working. And then down here now, if I want to change my name, I have to say in my animal die setName and then put in a string value here, like Solomon, e.g. put a semicolon at the end. You may be thinking, Hey, how is this any different? Well, currently it's not that much different other than it being the proper way to do things. But if I had this in 100 different places in my code where I was changing the name or whatever variable this was over and over. And I decide, you know what? I'm going to go ahead and print out, hey, the name was changed when I got to change it in 100 places. But because I have the center here, I can just say CL underscore name. Space was changed to follow this up with a name like that and create a new line. And then down here are critical for plane. And if I run my code now, now it will say I've learned the most creative name spot and then Spy, which changed to Solomon. Okay, so there you go. That is why a getter and setter are useful and important. Especially if I did have this in multiple places. Now, I don't have to deal with it. I can just look right here and change my thing right there. So if I ended up with some kind of error from changing it, all I have to do is look at my code here and see what was causing that. Rather than looking at all the hundred places that I messed with it. And I can have restrictions here are some code here to make the first letter capital or make it so the name only holds ten characters or whatever I wanted to do, right? So there you go. That is how you create a basic setter and getter. And what we did here where we had the functionality of our private variable in these public functions is called encapsulation. And the very last thing that we talked about was called inheritance. So let me go ahead and remove everything here real quick. And say the following. Let's say class animal on my code block, and I'll say string name, and I'll also say age, just like that. So every animal is going to have a name and an age. Remember, these are both private, so this is the exact same as saying privately. And that's because members of a class are private by default. So just like that. And then we'll go ahead and create a class that inherits from our animal class. So this will be the base class and the class that is inheriting from it will be the subclass. So down here, I'll say class dog, e.g. inherit from animal. And I need to have an access specifier here. And in this case we'll say public. And basically, if you remember, if I say public, hear everything that was public will be transferred over to dog and be public. Everything that was protected will transfer over to dog and be protected and everything that was private. Well, it's not accessible outside of this, so it doesn't really matter if I said protected, everything that was public would be transferred and be protected. Everything that was protected would be transferred to stay protected and everything was private. Well, it doesn't matter, couldn't access it anyways. And then if I said private, everything that wasn't already private would be transferred over and be made private. Anyways, let's go ahead and put in some code here. And actually I'm gonna go ahead and change the access specifier out with us to protect it so that dog has access to these variables. And obviously this is just a very basic example. So don't take what I'm doing here to harp on how to create things. I just want to show you the basic use of inheritance. And remember, we can inherit from multiple classes by following this up with a comma. We could say public and then another class name and other classes that inherit from dog, right? And then down here, let's just go ahead and right-click and create a public variable for simplicity sake, and add a brief. So every animal is going to have a name and an age variable, and every dog is going to have a breed variable along with these here. Okay? And now we could say, alright, we're going to create a new dog. My dog, just like that. And since these both have a default constructor, I don't need any parameters here. And if you want to know more about that, just go back to the last video where we talked about inheritance and go more in-depth. And then let's go ahead and create a couple of constructors for initializing all of these values. And I'm actually just going to set this one up here to private. And I do apologize. I did forget to put an underscore on all of our member names here. So let's go ahead and do it. Well, then to initialize this will just create a couple of constructors. So we'll say animal. And this one will take a string, name and age. And we'll put the definition down below. And we'll specify that this part of the class, we'll have our code block like that. Then after our parentheses here, we'll just say colon underscore name. And then in parentheses will insert name. Say comma underscore age, and then parentheses will insert age. And then I just wanted to add them. Our curly brackets like this, just so it was out of the way. And then we can create a very similar constructor for dog, except we'll add to it. And don't forget that we need to make our constructor up here, public Jose Public colon like that. And just to make it look a little nicer. And then down here for our dog, will create a constructor. But before we do that, we'll say private coin like that to keep our string. Then we'll create a constructor. So we'll say dog and m parentheses will take a string for the name, for the age and a string for the brief. Then we'll create the definition down below. And don't forget, we need to specify the Liz's front of the dog class. And I'll just surround some curly brackets like that. Then we'll put in our initializer list. Then because anytime a dog is created in, animal is created, since we derive from that, we need to go ahead and use this constructor as well when we use our only dog constructor here. So to do this we'll say animal, colon, colon animal. And then in parentheses we'll say name, comma H plus like that. And we're saying, we're referring to you as part of the Animal class. Then we want the Animal constructor. And we're going to insert these two values here, which is what our animal constructor takes. And then we can add in our breed to be initialized by after this constructors and comma underscore brief. M parentheses will insert the brief, and this is all going to work just fine. And then all you have to do is change our declaration or creation of our dog object. So we'll say dog, and then I'll call this my dog. And in parentheses, you can see that we now need a three constructor values meet the name. So we'll call him spot, followed by its age. So we'll just say something like two. And then we'll follow this up with a breed. So we can say boxer or something like that and put a semicolon at the end. And all of this will run just fine. And we can either print it all out down below or we could even put the code to print it out inside of our dog constructor. And that would work as well. And don't forget, both of these constructors are running when we create this here. And just to show you that if I said CL, animal colon space, and then in here if I sub c up dog, like so and ran the code, you can see we've printed out animal and dog. And that's because both of these constructors are running. Because when a dog is created, an animal is created. And we just have the one constructor right here that takes three values. Two of those get used for animal constructor and then we also added on a breed. Alright, so there you go. That is a basic example of how to use inheritance. Dog now essentially has access to the things that animal, as well as the things within the South. And to drive this point home real quick up, an animal will create a public method called shout. And we'll create the definition down below. So I'll just copy those down here. Say Chow and specifies this is from an animal. And inside of our curly brackets here, and we'll say void shelf, by the way. We'll say void animal shelter, like so. And then inside here, we'll just say hello. And because dog derives from animal, they can use this function, especially since it is public. So now, right below here, I could say my dog died, shout. If we run a code, you can see that it works just fine. We shout it out. Hello. And I'm going to go ahead and remove printing out dog and animal here. Already. Got that point across. And I run it again for you. And as you can see, it just says hello. And even if our method there are, our function was protected. Like so we would have had access to it within the class itself. So if I remove this line of code down here and inside of our constructor, e.g. I. Could have used shell. So the code with interclass will have access to this protective variable since it's available to the class and to anything that derives from it. So I can say shout right here, now go ahead and move that just so it's a little more readable for you guys and run our code. And now when we create a Dog is shouts hello. And we could have put that in a function or something for our dog as well. And that would have worked just as well. And if we had other variables or functions that were protected or public, we would have had access to them within the code of our dog class. So just to show you an example, I could say x and then this isn't a great example of how to use those. But you'll get the point down here. I could have said something like x equals ten and then print it out x on the line. And if I ran my code, it would have ran just fine because our class here has access to the code. However, even though are code has access to a here, we would not have been able to say something like my dog, x equals ten, e.g. this would not have worked, but it would have if this was public. There you go. Code runs just fine, or we can even just printed it out like this. And this will work as well. So let me go anyways. Hopefully this video helped you a little bit if you were getting a big cut up and all the extra code that we had on screen before. Thanks for watching, and I'll see you in the next one. 91. Chapter 15 (Classes): Static Keyword: In this video, we're going to be taking a look at the static keyword in C plus plus. Now the static keyword can be applied to things like variables, functions, and objects. And exactly what it does is going to change depending on what it is connected to. And will be briefly taking a look at four different examples of how and when and might be used so that if you were to ever see it in the future, you wouldn't have an idea of what it is now generally or broadly. While you can think of when you see the static keyword as that whatever it is tied to is now permanent and going to be the only copy of it. And we'll go ahead and take a look at exactly what I mean by that here in a moment. So first let's go ahead and take a look at the static keyword when it is used on variables and functions. As you can see here for the definition or how that works, I basically said it creates a permanent and only copy of a variable. So let's go ahead and just create a function above main here to see an example of this. So what we're gonna do is say Boyd, count time, dram, like so, open and close parentheses. And we'll create the code block inside of here. We're going to say static. In college, I set it equal to 0, and then below that will say I plus, plus like so. And then inside of here, Let's also go ahead and print out. So put it on a new line each time. And then down here in our main function, we're just going to call this method five times. Then I'm just gonna go ahead and comment out our other code here. For now. We're just going to happen is the following. When our method first rungs here, it's going to run through and create a static integer variable called i and set it equal to 0. And if you remember, I said this variable is now going to be permanent and the only copy of that variable. And what that means is this, when it first runs through, it will create it and set it equal to 0. And because it is static, this will be the only copy of our integer variable called i within the scope here. So we still can't say something like I equals ten right here without getting an error because it's still only within that scope. Then it's going to print out I, which will be 0, and then increase I by one. Now normally, if this wasn't a static variable, it will create a new variable called the I. Each time this method ran, print out 0 increases by one. Next time around, create another integer called Die set equal to 0, printed out and crystal by one and so on. And we would always get the same result. And if we ran our code without the static keyword here quick, you can see that that is the case. So right there, we printed out 05 times since we call the method five times. But if we put static here and remember that's just all lowercase there. And before the datatype, when we run our code now and actually increases by one each time. And that's because when we call our method again, because I was already created after the first time through, it's just going to use the same variable. So essentially the second time through here, this top line will be ignored. L printout, I increase it by one. And then when it comes back around L, ignore this line again because I is already created and there will only be one copy of it. So every time we call this method now, this declaration and initialization here is ignored and we'll print out i and then increase it by one. So there you go. That is how the static keyword works with variables in functions. It creates a permanent and only copy of the variable. And you might be wondering what if we did something like this and ran a code? Well, now, because only this top line, it will be ignored where this was declared, I will be set equal to 0 each time, and then it will print out 0. So if you are going to do something like this, you want to initialize it on the same line so that this can be ignored in the future. Any ways we can go ahead and delete that function now, as well as delete the calls to it, and move on to this second use case for static, which has variables in classes. And what this does is create a permanent variable that is shared among all of the objects of that class. So what I'm gonna do is uncomment our code here just like that. And then we could either put this in dog or cat or animal class, and it would work all the same. So let's just go ahead and put it in our dog class here. Just as an example, what we're gonna do is say static. I will put a semicolon at the end. And then what we need to do to initialize this here, I say static. And then dog being the name of the class, colon, colon or the scope resolution operator. And then say, I, set it equal 0, for example. And we'll actually remove the static keyword this second time around. So we'll just say dog colon, colon I equals 0. We're taking this static integer variable, which we've already created. And we don't need to use the static keyword after this point. And then we're initializing it outside of the class by saying int being the type held by the class name with scope resolution operator, then the name of the variable, setting it equal to 0. So just note here that after you create the variable, you no longer need to use the static keyword when referring to it in the future. And the only reason we use anterior is just because of how this variable gets initialized when it's put into a class. It is a static variable, and this is the way to do it. But normally we can just refer to this as I am feature. And we're just gonna go ahead and make this public so that we can easily take a look at how this works. So down here, we already have a object or an instance of our dog class called my dog. And let's go ahead and save my dot and set it equal to five. Then let's create another dog, my dog too. And we'll just give this a name like sparky and give it an age of two, for example. Then we're going to print out my dog to see how my dog too. And we'll put this on a new line, like so, our create a new line after it, I should say. And if we run our code, you can see that down here, my dog to die was five. And that's because this static integer variable, as written here, is a permanent variable that is shared among all the objects of that class. So it doesn't matter how many instances of the class we make, they will all share the same variable. And so if we were to say my dog to I plus equals five and then print out my dog. I would get ten and I'll go ahead and show you that now. So if I said my dog, two dot plus equals, Well, we originally said I was equal to five and now we just increased by five. And because they share the same exact variable, if we print out my dog, I will print out ten. Okay. So there you go. Like I said, we actually could have put, I created this static variable in animal here. And then not only would every instance of cat has shared that variable since it is an animal. But every instance dog would have as well on top of every instance of animal that we would have created two. So everything that is an animal would have shared that variable. So that you've got, anyways, I'm going to go ahead and remove that from dog here. And we'll remove this extra code as well. Let's go ahead and take a look at the third use case for the static variable. So if I create a static class object, what it does is creates a permanent and only copy of that object. And probably the easiest way to show you this would be to create another function. So right here we'll say void, create object, open and close parentheses. And then we'll have our curly brackets like that inside of here. We'll go ahead and create a dog called my dog. Pretty sad. Then we'll give it a name. So we'll say sparky and give it an age like to. And then after that, we'll say my dog died, change health. And we'll say minus five are considered value of negative five. And then we'll print out my dog got health, like so. And we'll print that out on an angle. My temp dog dot get help. Line, just like that. Then we'll go ahead and call this function a few times. And our main method. Now, when our code runs and it gets to this function, L, create a dog, my dog, and give it the name sparky and an age of two. Then we're going to say my temp dog dot change health minus five, which is going to change his health variable, which if you remember, has a default value of 100 and then it's going to print out 95. Then the next time through, it will ignore this. If we make this static here, that is, we say that a dog like that, then the next time through it will ignore this, just like it did with the static variable that we had in a function, because my temp dog is already been created and there can only be one copy of it and it has permanent and then we will lower its health I've had again and print it out. So if we run our code here, you can see that a set it at 95 months because it started out a 100 subtract 05 printed out and then it's gonna go down by five each time we call it. And then just to show you that that only happened because of the static keyword, if we remove this and then run our code, you're going to see is that our dog constructor is actually going to run four times. So after our dog's body was created this first-time here, we went ahead and ran this function which created a new dog called sparky H2, printed out its health. And then down here, after it's subtracted it's health and printed it again. And then we did the same thing for more times. So next time through and read this code again, credit another dog called sparky H2 Health a 100 printed out again, and they go and just kept repeating. So when we add static here, this whole line gets ignored. So with that static there, this creates a permanent and only copy of this. And every time this line has ran again, it gets. So there you go. Static class objects and static variables in functions work the same way. Now let's go over one more use case for this. This is static functions in classes. But before we do that, let's go ahead and remove this code here along with the function itself. Alright, so let's go ahead and take a look now at static functions and classes. So if we go up to our animal class here, for example, and inside public will create a static function. We can do this like so. We'll say static, void, print, hello, open and close parentheses and a semicolon at the end. And then we'll define it down here just for good practice. So we'll say animal, colon, hello, open and close parentheses. Then we will create the code block down here. Then before animal, we still need to say avoid that because that is our return statement. Once again, notice we don't need to use the static keyword again. And then down here, I'll, we're gonna do and say hello afterwards, just like that. And now any instance of our animal class, we'll only have one version of this function. And also, because there's only one version of this function, is not allowed to access non-static variables. So for example, we can't say underscore age equals five. If I mouse over this, you'll see that a non-static member reference must be relative to a specific object. And what that means is, is that because our function is static and that there isn't a new instance of it for every object of the class animal that we create is that they can't use specific instances of this variable as there's an underscore age for every object of our class. However, it can use static variables and functions. So if we had something like static, I like so it could use, so there you have it. And also remember that when using a static variable, we have to say animal colon, colon, and then initialize it like so. And we would still need to say in here as well. So there you go. Static functions and classes are shared among every single object of that class. And they can only use static variables and functions since it is not specific to each object. Now, if we change this to something like i plus equals five and then went down here and created a couple of animals. So if it's an animal, animal one will have to give it a name and age. So we'll say Jack, age one. And I'm going to say animal to say Karl B is one as well. And then we can say animal one kilo, just like that. Animal prints hello. In both of those will work just fine. And they are sharing the same function here. And just to show you a couple of things with that, we can say something like x equals five. And then we'll say x plus like so. And then we'll say I plus equals five. And we'll print out the following. We'll say x equals. To print out x. Mostly Cl I equals. And we'll put both of those on your lines as well. Now, we've called that function twice. Remember they share the same exact function. And before you run a code, we actually do need parentheses here, just like that, just as we would with any function call, by the way, in case I forgot to mention it earlier in the course. Sometimes when people are talking about calling a function, they may also refer to this as invoking the function, right? So just a heads up on that. That means the same thing to invoke in the call a function are the same. Now, if we run a code here, you can see that it printed out Hello because x was not a static variable. It was the same each time this code RAM, however, I was a static variable. So each time this function ran, it was working with the same variable. So the first time we print out five and the second time we printed out ten. And the basic thing to remember about a static function is that every instance of the class shares the same function and that it can only use static members and functions. And there's one more important thing to note about static functions in classes. And that is that because the function is static and there isn't a new version of it per object or instance of the class. You can actually call that function without creating an object at all. So what we can do down here to say animal colon, colon or the scope resolution operator. And then just say print hello. And that will work just fine. And there you go. I was increased by five again because it was a static variable and we were able to use the function without having any objects at all. So that is really cool. Now, I'm sure you can already think of some different use cases of when you might want a permanent variable in a function, as well as when you would want a static variable in a class. For example, we could use this to have a function count how many times it was used. And then same for here. We could just increase our static variable by one and each of our different constructors for a class. And then we would know how many objects were created of that class. And that's just one example there for you, for each of those. And then we already saw an example of when you might want to use a static class object, where we were able to create a function that created an object within its scope specifically, and was able to do something to the object each and every time it ran, without it being outside of that scope, and without creating a new one every single time. Then lastly, static functions and classes. One of the main use cases, reasons for using them is this right here, where we can call that function without creating an instance of that class. That can be really useful. For example, if you had something like a math class, well, you probably don't need a instance of your math class. Instead, you might just want to use all of the functionality available within it. And that would be a perfect use case for this here. Anyways, there are four different use cases of the static keyword and some examples to go along with it as well as some use cases. So if you do see this in the future, hopefully you're not completely lost in what it is and why does there, and who knows? Maybe we'll even use it in one of your very own programs. Anyways, before we wrap up this video, I'm just gonna go ahead and delete this extra texts that we created within this video. Let it is how it was when we first started it. Anyways, don't get to see. We have thanks for watching and I'll see you in the next one. 92. Chapter 15 (Classes): Class vs Struct: In this video, we're gonna be talking about the differences between structures and classes in C plus plus. Now, I know we haven't even looked at structs or structures, but trust me, if you know what classes you basically know what a structures. So let's go ahead and take a look at the syntax for both of these. And then we'll talk about the actual differences and why you should use one over the other. So first two credit class, as we know, you're going to say something like class. You'll have to open and close curly brackets and a semicolon at the end. And awhile to create a struct, you're going to say struct, or they open and close curly brackets and a semicolon at the end. And there you go. That is the syntax for a struct. And this is the syntax a class. So what is the difference? Well, as far as our program theory is concerned, or the compiler, it doesn't really matter. These might as well be the exact same thing other than one key difference. And don't let anyone tell you any different because really the small minor differences under the hood aren't gonna make any difference. So what does this one key difference? Well, class members, if you remember, default to private like so, and struct members default to public. And there you go. That is the difference between a class and a struct. So just to clarify here, onscreen, the members of a class effect access. The members of a struct, default do public. And by the way, when I'm talking about the difference between these, I'm only referring to C plus plus and that other programming languages. So there you go. That is really the only difference between a class and a struct other than the key word here being struct or a class accordingly. So why would you ever use a struct over a class or vice versa? Now, in all technicality, you can use whichever one you want. Cool. Like I said, they're basically the same other than the default accessibility of its members, which you could always change anyways. But generally speaking, if another programmer or something were to look at your code and see a stroke, they would assume that you're essentially not going to have anything that is private or hidden and that pretty much everything within it as going to be public. So if you want to create a class of some kind, maybe it's a class for math functions or something like that. And everything is just gonna be public and available and you have no reason to hide any information, then go ahead and use a struct. Otherwise, if you do plan on having any members our private or protected, really, I would recommend using a class and that is just like a conventional type thing. But like I said, and all technicality and C plus plus, they're essentially the same thing, clear than this one difference. So use whichever you prefer, but that is just the conventional way a struct is used, is when you're not going to have anything private or protected. Anyways, that is it for this video. So I'm going to go ahead and erase those coat. Thanks for watching, and I'll see you in the next one. 93. Chapter 15 (Classes): Prevent Base Class Creation: In this video, we're gonna be talking about how we can make our base class on instantiate or credible. So currently in our code, we can create an Animal Crossing and then giving it a name. And then following that up with a name for the animal like sparky example and giving it an H. So this will run just fine. But what if we wanted to make it so you can only create an instance of a dog or a cat being our classes that inherit from animal or the subclasses of the animal base class. Well, it's pretty simple, but we have to do is go up to our animal class here. And if we open that up, you can see our constructor right here. And remember how I told you guys that a constructor needed to be public. Well, that was so that we could create an instance of it as the constructor needs to be available to do so. But in this case, because we already have subclasses being cat and dog that we want to work with instead of animal directly, all we have to do is cut our constructor declaration here. And then we'll add protected as an access specifier. So we'll say protected coal mine, just like that. And then right below that, we'll just paste our constructor. And now the only thing that has access to this constructor is the class itself and its subclasses or anything that inherits from the class Animal. And now if we try to go down here and just create any animal, we will get an error. And that's because there's no longer a constructor available to us. And so there you go. That is how you can make a class on credible. But keep in mind, you really wouldn't want to do this unless you have a subclass or something like that to work with. Instead. Anyways, we're gonna go ahead and leave our code like that for now. So just go ahead and save what you have. Thanks for watching, and I'll see you in the next one. 94. Chapter 15 (Classes): Member Access Operators: In this video, I'm gonna be showing you a different form of the member access operator. So the one we've been working with so far has been the dot, but there is another version of it. And one example of when you'll see this as when working with pointers to classes or structs. So let's go ahead and take a look at how this works. And by the way, the operator looks like this, which is a dash and a greater than symbol. So to see this, let's go ahead and create a pointer to a class. And in this case we'll just have it point to my cat. So we'll say asterisk PTR for planar and set it equal to the address of a. Alright, so now if we printed out cleaner, like so, create a new line. And we know that this will print out the address of my cat wherever object here. However, we can also use this pointer to get access to the members of what is pointing to. And like I said, this is just one example of when you might see this operator. And we're not gonna get into all the different times you'll see this. But if you're interested, feel free to look into that. I just wanted to be sure to show you one example of one. You'll see this, see you aren't completely thrown off when you see those operator. And I decided to go with this example because having pointers to objects isn't all that uncommon. And then down here, if we say PTR and then have our arrow here, or a member access operator, you can see I immediately have access to all of our members of my cat. Let's go ahead and use those skills, get help on. And what this member access operator does outside of giving us access to the member is it's also going to de, reference this here. So rather than giving us the address now, it's just going to run get help, which as we know, we'll just return the health value of my cat, which started at 100 and we subtract it by 15 here. We already have printed that out to the screen as 85. So this here will also print out 85. If we just go ahead and say See like this. And if we run a code here, you can see that we printed out the address of my cat. And we ran this function here as normal. And that's because this member access operator, when you used with pointers, not only gives us access to the member, but at the references that. So instead of giving us an address, we just use this member normally. Anyways, this is just one example of when you will see this operator. And if you're interested, you can look into other use cases for it. But this is the one who will go over in this course and the one you'll likely see most often. Anyways, I'm gonna go ahead and remove this extra code. Don't forget to safely. You have. Thanks for watching and I'll see you in the next one. 95. Chapter 15 (Classes): Polymorphism: In this video, we're gonna be talking about our fourth and final main principle and or pillar of object oriented programming, polymorphism. And what polymorphism means is to take many forms. Anyways, let's go ahead and take a look at a couple of examples of polymorphism. So if we go up to our animal class here, you guys remember, we created this speak method. And if we look at the definition of that, all it does is it takes a sound and prints that out. However, currently we don't have that specific version of this for our dog and cat or any other child classes and subclasses, we might want to create that inherit from our animal class. Now currently having to insert a specific string anytime we use this function and then putting in the sound that animal makes specifically every single time we use it isn't very efficient. So let's go ahead and fix this using polymorphism. So the first thing we're going to do is just remove this perimeter entirely. Alright? And it's just going to print out animal sound just like this. Alright, and then the reason we're getting this error is because up in our declaration here, we also have to remove the parameter. One way we can fix our original issue now with our dog and our cat class is to do the following. First, we can just go ahead and copy this entire function and paste it into each of them, like so. And now on both of these, we just need to remove this animal portion right here. So now it just says void speak and I'm just gonna go ahead and remove those comments as well. Remember, normally what you're going to want to do. Have the declaration here and the definition outside of the class. But we're just gonna go ahead and leave it like this for right now. And our example anyways, what we can do now is in our dog class here, just change those to say Park and our cat class here, we can just change this to say, Yeah, that would work just fine. So if we went down here, let's go ahead and just print out a couple of blank ones. Then down below that will say my cat speak. And if you remember, before we overrode these functions, this would have simply used our definition that was in the base class. But now that we have them overridden, we can do the following. So say my dog not speak now and run our code here. You can see that we printed out and bark. And just to remind you of a reshow you how that worked before, if we go ahead and just remove our implementation of it for the cat class and run our code again. You can see that it just said animals sound and then mark, right? So although this here does work, the problem is it really allows for human error. Because now anytime we have a child class that has animal as its parent class, we have to remember to have an implementation of the speak method. Otherwise, it's just going to say animals speak, which isn't ideal at all. We want every single animal to have a different sound built-in to the class. And normally, if you do override a function like this and have the base class being animal in this case has its own implementation. What you wanna do is actually make those implementation a virtual function. And I'll show you what that is and why in just a second here. So if we go down to our main code here and create a pointer to our base class. So if we say animal Astros, we'll call this animal blinker. And we'll set it equal to the address my cat. The reason this works by the way, and you can even request an animal in a function perimeter and use my cat as an argument is because of the following. And this is another form of polymorphism actually, because our cat is an animal, then we know that everything that animal has access to, cat has access to. So if we requested an animal in a function for example, and we ran some different animal functions and work through some of the different variables within animal. Well, we already know cat has an implementation of all of these things because our cat is an animal seeing is that inherits from animal, and therefore it takes different forms. A dog is not only a dog, but it is an animal. Cat isn't just a cat. It's an animal as well. But we can't use animal as an implementation for dog or cat, for example, because not all animals are cats, but all cats are animals. So it only works the one way. So that's why it down here, we're saying, alright, work, can I create a pointer to an animal and a catch the animal? So that's works. Alright, but the problem with this is if I were to say animal crater, then use our member access operator. Remember what pointers you're going to use a dash and a greater than symbol. It looks just like an arrow and then say speak. And run our code. You can see that a set animal sound animal, some bark, which makes sense as of right now. But even if our cat here had an implementation of speak. So let's go ahead and put that back in our cat class here and change it to meow. Then if we run our code again, you can see we get a weird result. And that's because our pointer here that accesses speak will actually reference the base class version. Whereas this down here, where we used our cat objects specifically, we'll use the derived class or our child class version, and that might not always be what you want. So let's go ahead and take a look at a way to fix those. So if we go up to our base class here, being animal and go to our speak function here. What we're gonna do is put the virtual keyword in front of it. And we only need to do this in the declaration. And what this virtual keyword does here is basically say explicitly that we want subclasses of our animal class to have its own implementation of this function. And we can still leave our basic implementation here. However, we're letting our program now here. Hey, we want every class that derives from animal to have its own implementation of that function, which is why we made it a virtual function. So that's what that does, is just explicitly say, Hey, we want subclasses of our superclass or child classes of our parent class to create their own versions of this function. So now if we run our code, you can see that now it prints out meow and our pointer here. And then as referencing our speak function now works with the subclasses reversion of it. And that's because we've explicitly said, Hey, we want our subclasses to have their own implementation of this function. And I'm not going to get into the nitty-gritty of why this works this way. But just know if you're planning to have a function in the base class that gets overwritten and your subclasses that you'll want to add the virtual key word. And remember all we had to deal with ritual in the declaration and our base class, but not in the definition down here. Just to show you this quick, FOR cat, for example, didn't have its own implementation. This would still work just fine. Okay, So does not forced upon him, but we still have an issue in the case that we wanted our base class not to have its own implementation. And we wanted to force each subclass to have its own implementation and not have to worry about us forgetting about it. This simply won't do. This might not always be the case. In some circumstances, you might want to have the base class have its own implementation like we did here. But in the case where you don't, let me show you how we can fix that. So instead of having just a virtual function here, we can actually create a pure virtual function. So let me show you how that's done. First off, we're just going to remove the definition for speed entirely religiously can get rid of it. Then up here in our declaration, we're going to say after the closed parentheses, but before the semicolon, we're going to say equals 0. And now this is a purely virtual function. And now that we have seen a purely virtual function in a regular virtual function, there's just a few things I want you to keep in mind. First off, ritual functions cannot be static, okay, So this can't be the only version of this function because you're saying, Hey, I want my subclasses have their own version of it, okay, so therefore, it doesn't make any sense. Also, a virtual function can be a friend function of another class, and we'll get into Friend functions and classes in an upcoming video. Just something to note. Don't worry about it right now. So what is a purely virtual function? Well, we may say that we want to set it equal to 0. We're saying this function has no definition. It might as well not exist. Therefore, every subclass needs to have its own version. And you might be wondering, why didn't we just get rid of a void, speak here entirely and just throw it into every subclass like our dog, cat. If we created another one like a noose or horse, etc. Well, for one we could forget, and then every animal might not have a speak function. Also, for example, if we created a method or a function that took animal as a perimeter, we wouldn't be able to use the speak function since our function no longer knows if every animal can speak, because that's not defined within the animal class, right? So that's why you would want to have this here in the first place. Then, like I said, it just removes human hair. So now if we go down to our dog class, we already have an implementation of the list. However, our cat class does not. And as you can see, this immediately gives us an error. This is object of an abstract class type. Cat is not allowed because there is a pure virtual function. Now, an abstract class is a class that you cannot create an object of directly. So we can no longer say the animal. My course, for example, and give it a name like Jack. And an age. This will not work because this is an abstract class. And the reason it is abstract is because there's a function within it that has no definition. It basically doesn't exist and subclasses need to create the definition for that. Therefore, we cannot create objects on it in an abstract class is just any class that has a pure virtual function. As that function has no real definition, then we can't create an object of that class because it now has a function that isn't defined. Alright? So having a purely virtual function for one makes the class on credible, which we already kinda dead by having our animal constructor protected. But even if this was public, It's still would not be creatable just because of our purely virtual function here, it is now considered abstract in one thing to note, for those of you who come from other programming languages, especially for you guys, as well as that if every member of the class is purely virtual and it has no implementation of anything on its own. That class is now considered an interface for other things because it has no definition of anything. It's just declarations. So it has what is called an interface are essentially just a blueprint for anything. And Haritsa with one pure virtual function, it's an abstract class. And if everything and it was purely virtual, it would be an interface, are considered an interface anyways, as you saw, because we have this purely virtual function, there is no room for human error when creating different animals. So when we created our dog here, that we said as an animal or our cat that was an animal, etc. Every animal we create will now have to have a version of the speak function, right? Because there's no definition on its own. And technically you could leave it like this. And this is now considered essentially an abstract class because it has a function with no definition. And then you can have a class that derives from cat or inherits from cat. And eventually one of them would need to have a definition for speak, because you cannot have an object of a class that has a purely virtual function or method because it won't let us do this now, speakers now implemented properly. So he said, every animal has to speak and have its own way that it speaks. So down here in our class, we'll go ahead and paste in our speak method. And just to do things properly, Let's go ahead and fix this up now. So we'll say void speak, semicolon, just like that. And it will copy this envelope, a dog class, for example here. Then for the words beak, we'll say dog, colon, colon speak, and there to the semicolon, just like that. And get rid of our definition up here, just like we've done with all of our other functions, like I said before, and that is the proper way to do it. So we'll copy this again, semicolon at the end to create the declaration. Cut this out and put the definition down below. And I'm actually going to put it down below our constructor here. This will be cats erosion of speak. And by the way, in case I forgot to mention it before, in our constructors here where we had our animal constructor, here are the base class constructor. This does need to come first before anything else in our list initialize it here. But like I said, everything in the list initializer needs to be an order from top to bottom, and a base class constructor needs to come first. So just wanted to give you a heads up on that in case it's something I did forget to mention. And then down here in our caps definition for speed, let's change it to run our code. There you go, everything is working properly. Now, our animal pointer, which is pointing to our cat, is using the proper version of it. And we could even change our pointer to my dog, and that would all work just fine. And on top of that, it is now not possible for us to create a new animal or a subclass of animal that doesn't have its own version of speak. Alright, so that's fantastic. Everything's up and working. And this was the last example of polymorphism that we're going to go over as now our speak function has multiple versions of it, right? Remember polymorphism means that take many forms, so speak, is not only a purely ritual, a function with no definition, but it has its own version inside of our dog class, and it has its own version inside of her cat class. So there's a few examples of polymorphism for you. Alright? We've learned what a virtual function is, which is just a function that you've explicitly said that you went the subclasses to have their own version of that. We also took a look at how to create a purely virtual function, which was created by setting that function equal to 0, are basically saying it's equal to nothing. It doesn't exist right now. And remove the definition which Not only makes the class abstract automatically, but if everything was purely virtual in the class, it would be what is called an interface are basically a blueprint for everything that inherits from it in a purely virtual function compared to a regular virtual function, which would look like this, is just a function that now needs a definition to be created within a class that derives from it. And if our dog class didn't have its version of it, then we couldn't create a dog either. It needs a definition at some point in time. So then we'd have to have dog breeds as classes or something and give it a definition there. At some point it needs a definition for you to create objects of any class that derives from animal, right? We also learned that every subclass or child class is also considered a base class, but not every parent class as a child class. And just a reminder in our example, that's basically like saying every dog is an animal, but not every animal is a dog. And just to quickly show you another way we could I have an example of this. We can have created a function like so, saying void animal or something like that. And then as a parameter, because they can be an animal and animal. Inside of here, we could have said animal, die speak. As you can see, we got an error. And that's just because animal is currently an abstract class. So let's go ahead and fix that quick up here and change this from a purely virtual function to just a regular virtual function. And we'll give it a quick definition like so, just like that. Now, if we go down here, let me say misspeak and passion, either our cat or dog. So let's just gotten passionate cat. And as you can see when we run our code and actually it doesn't print out anything after bark. And that's because this actually used animals version of this function, since we created a copy of cats values and put it into an animal essentially. But to fix this, all we have to do is take a reference to the animal Peston, and now this will work just fine. So if we run our code again, there you go. It prints it out. Yeah. And like I said, because a dog is also an animal, we could have also passed in my dog. And our code and our function would have worked just the same except now it prints out bark. So there you go. So there you go. Hopefully now you have a basic understanding of polymorphism in some of the different ways to use it, along with some different keywords that go along with it. So we looked at abstract classes and interfaces. We also looked at creating virtual functions which have their own implementation and explicitly say it that way. What subclasses to create their own versions of this in purely virtual functions, which basically say there is no definition and subclasses have to create their own version of it. And the subclasses, our base classes, that base classes aren't necessarily subclasses. So we couldn't have taken a cat as a parameter and sent them an animal. Anyways, that's it for this video. Thanks for watching, and I'll see you in the next one. 96. Chapter 15 (Classes): Four Main Principles Of OOP: In this video, we're gonna be revisiting the four main pillars or principles of object oriented programming. And these are in no particular order. And also another note is that we didn't cover every possible aspect of each of these four main pillars, but we did cover the basics and the main gist of them split, you know what they are and how and when you can implement them into your own programs. So the first one we're going to talk about pollution. And if you remember, encapsulation is when data is hidden from the outside by being encapsulated within accessible functions. So an example of when we did this, it was when we had our getter and setter functions that help all of our private variables within them are private variables were encapsulated within our getter and setter functions. Alright, so that's our first one. Then we're going to talk about abstraction. And basically you can think of this as almost an extension of encapsulation and abstraction is when you only show the user relevant data or information. So for example, when you power on your phone or open an application, you only see the relevant information. And an example of when we did this is once again, when we had our getter and setter, you didn't need to know what was in that code block, how exactly it affected our private variables. All you need to know what's the function name and the parameters. Everything else has just happened within the background. For example, when we had our health variable and it didn't let you go past a 100 or below 0. The user didn't need to know any of that. When we call the pawn our functions to change or set our health variable, we just need to denote the function name and what the insert as the perimeter and everything else was abstracted from us. It was hidden in the background. So that was abstraction. Next, we have inheritance as basically when something inherits from something else. So for example, in programming, inheritance is when you inherit members of something else, this allows you to find similarities in a parent class. So for example, our animal class, which kinda defined what an animal is, we were able to have things like a speak function, a health variable named variable. We were able to set the basis for what an animal was. And then things we're able to inherit from that to kinda stay, hey, this is an animal. So it's going to have everything that all animals have. And then it can also have its own identity, its own uniqueness. So for example, our cat and dog animals printed out different things. So when our cat spoke out and when the dark spoke at barked, and when it was created, it had printed out slightly different things saying either a catalyst created or a dog who was created specifically. But they all had one thing in common being the parent class or the animal class that we created, right? And that was an example of having two classes inherit from a base class. And remember, there are bunch of other things you may see child and parent classes called, but a lot of times parent and child class is used more often. And it's just an easy way to think about it. Because, for example, children inherit things from their parents. So those are easy real life connection right there. And lastly, we have polymorphism. And what polymorphism means is to take many forms. And we just looked at some examples of this in the last video. So some examples of when we use polymorphism is overriding functions. Also, we used subclasses as a base class. For example, we used a cat and dog as substitutes for an animal perimeter because we know that anything and animal can do. A cat and dog can also do because they inherit all the same members plus more. And then some other examples of this that we took a look at where our virtual and a pure virtual functions, which explicitly stated, Hey, if these are gonna be overwritten and there's gonna be other implementations of this function. Anyways, there you go. Those are the four main principles or pillars, rather than a column of object oriented programming. So you have encapsulation, abstraction, inheritance, and polymorphism. I know a lot of beginner courses don't touch on all four of these. But personally, seeing is these are the four main principles of object oriented programming. I just can't see leaving these out of any beginners course, especially because a lot of times, once you've finished a beginner's course on programming, you're gonna go out and just start working with your own programs and just trying to do as much as you can with what you know and how are you going to go and create good quality code in an object-oriented programming language if you don't even know the four main principles of object oriented programming, anyways, hope you enjoyed the video as always, thanks for watching, and I'll see you in the next one. 97. Chapter 15 (Classes): Friend Functions & Classes: In this video, we're gonna be talking about Friend functions and classes in C plus plus. First off, I'm going to tell you what a friend function and a friend class is. I'm going to go ahead and look at it and create an example of both. A friend function is a function which can access the private and protected data of a class. And a friend class can access private and protected data or members of another class. Anyways, let's go ahead and create and take a look at an example of both a friend function and a friend class. Alright, so up above our main function, Let's go ahead and create a class, and we'll just call this class. We'll have our code block like that. And don't forget the semicolon at the end. Now foo is often a generic name that you'll see quite frequently in examples. It's just kind of a common place holder name. So we're just gonna go ahead and use that just to show you an example of what you might see this as. Right now, I'm showing you an example of something. I will say class. And inside here with pretty high the variable. So we're going to say private, just like that. And then Dumbo that. We'll just say int x and we're actually just going to go ahead and initialize this. I know normally you want to have this separate, but just to keep this example to the point, we're not going to worry about it. And then outside of the class we'll create a function that prints out x. So say void, print x. And then in parentheses here we're going to have a parameter that takes a Foo class object. So save, and we'll just call this lowercase there. Then inside of our code block will say foo got x. Now, as we know, private and protected variables are not accessible outside of the class. And friend functions, as well as friend classes are neither members of the class that they're friends of, nor are they subclasses of it, they're completely separate. However, to declare our function as a friend function, we need to specify that within the class itself. So in our Foo class here, we're basically just going to have what would look like a declaration or a prototype of our print x function. So we'll just copy this whole line and paste it right here. Don't forget our semicolon here at the end. Now, as you can see, at a still unaccessible, to fix this, all you have to do is make this a friend function by adding the friend keyword before a void here, just like that. And now, as you can see, this works just fine. So if we go down to our main function and then simply call upon our print x function, print x and y. I have to insert and implementation of foo here. So we'll call it. Then, we'll just send that. Right here. There we go. We were able to access this private member with our friend function. And notice that we only had to put Friend and this line right here and not the actual function declaration there. By the way, a friend function could have multiple friends. So to show you this, let's go ahead and remove everything. Don't you? Remember our function and our friend function declaration. Then let's change class foo to class, copy it and paste it down below. And we'll call this one class a, class B, a private integer. We'll just call this one y, just for the heck of it. And we'll set this equal to ten. Then down below, we'll create our function, so say void, and then the pay class a object and we'll call this class be object and call it b. Then we'll make this a friend of both of these classes. So just copy this top line here, paste it in, put a semicolon at the end, and add the keyword. Then we can just copy this and paste it down, like so. And then inside of our function here, a x plus b y, just like that. Now notice that this one is inaccessible, and I'll show you why in just a moment. But before I do that down in our main function, we'll create an instance of both glosses. And remember we can name this whatever you wanted. It doesn't matter. Our perimeter names are gonna be separate. So I could say a b and a little color add function. As it's not part of anything. We don't need to specify where it's from. And then just say, don't put a semicolon at the end. And this would run just fine once we fix this error here. The reason we are getting this is because class a does not know what class B is when it reads through our code here. And we can fix this with something called a forward declaration. And we'll put this here at the top right above our class a. And all we're gonna do as a class and put a semicolon at the end. And this is going to fix everything. And like I said, this is called a forward declaration. We've seen examples of this when we had a function prototype and then the definition down below, except this was just doing the same thing with a class. We're basically saying, hey, it's gonna be a class called B and R code and now are a class hasn't confused. As you can see, the error went away and we can run our code, prints out 15 because our add function and access to the private variable x and to the private variable y. We can call these both X. It really wouldn't have mattered. So there you go. There's some examples of friend functions, which are just functions which have access to private and protected members of the class or classes that they are friends of. And then we have friend classes. I will show you that right now. And to do this, let's go up to class I, and let's just remove our friend function here entirely, like so. And then we're going to say friend class, just like that. And now class B has access to the private and protected members of class. And we're going to go ahead and just remove our friend function altogether now. And we'll move it down here in our main function. And then in class B here, we'll create a function to add x and y. So say void add i. Now normally we wouldn't wanna put the definition inside of the class here. But to keep things simple, we're gonna go ahead and do so. Then in here we'll say a color. I'm going to say y plus x. And because Class B as a friend, say, as stated here. And once again, a class can have multiple friends, just like what their functions. It will have access to private variables within class, as well as protected, even though it is not a subclass of class a or part of class a at all. So we created an instance of class, say, within class B, just like we did with our friend function, basically, as we still needed an instance of the class to work with. And now in our main function, we can go ahead and get rid of all of this here and just say B and call it B or whatever you want. And then down below that would be like so with the semicolon at the end. You have to do to fix this error here we're at is inaccessible, is make our function public opinion, public. I'll just clean that up a bit and there we go. Our function is now accessible by our instance of the class, since it is public and we can go ahead and call upon it. And there you go. Comes out 15 because y and five added together because those Team, anyways, there you go. You now know what a friend function and a friend classes and how to use them. So as always, thanks for watching and I'll see you in the next one. 98. Chapter 15 (Classes): Namespaces: In this video, we're gonna be talking about name spaces. Now as some of you may remember, some of our code comes from the standard namespace, such as C and C out. And the reason we haven't had to do much with it as because we specified or explicitly said that we're gonna be using code from the std namespace. However, if this wasn't here, and I'll just go ahead and comment that out for a minute and we try to say something links to C out. It has no idea where SEO is coming from. And that's because it is part of the standard namespace std here for short. If we didn't want to have this explicitly at the top, we would have to say std colon, colon followed by C out, or the piece of code we're using from this namespace and seamless CN down here. And some of the other things we've used in the past as well. Number a namespace is basically just a named space or a named portion of code to help specify what you are talking about. So normally, if you had two functions with the same exact name and the same parameters, it would be hard to distinguish which one you are using. However, if each one was part of their own namespace, then our compiler will know which one we're talking about and referring to where the code is coming from. And an example I used in a previous video, as you can think of this like someone's last name. So if I were to come up to you and start a conversation and I just said, John, you might not know who I was talking about. But if I said like John Silver or something like that, then because I've associated it with a last name, you would know who I was talking about from here on out. And that's kinda similar to us saying we're gonna be talking about the code from our standard namespace in our code here. Alright, so anyways, if this wasn't here, just note that we have to specify where this code is coming from. As you create larger programs yourself, you might want to create your own namespaces to help differentiate where your code is coming from. And I'll show you how we can do that now. So let me go ahead and just remove this stuff here. I actually have the syntax for creating your own namespace right here. So you're going to have the namespace keyword followed by the name of your name space. And then inside the curly brackets, you'll have the code that isn't part of it up here. Let's just go ahead and create two functions. And this is gonna be a pretty basic example, but I should get that point across of how to create and use a named space or a namespace. So we'll say void. Print hello. Inside the code for that. I will just say scale. Hello. Just to help differentiate those because we're going to create a another function with the exact same name, same Hello, namespace. One. Just like that. Then we'll just create a blank line. Then let's go ahead and copy this whole thing and paste it down below. And we'll change this one to say, I mean she's two. And currently, if we were to try and just call this function by sending hello and run our code. You can see that we're going to get an error. That's because the function that has a void return type called print hello and has no parameters already exists. So we can't have two of the same thing. And as your code expands out as possible that you will run into the same issue where you might have multiple functions that have the same name just due to their readability or something like that. And this is a great example of when you would want to create a namespace. Up here, we're going to say namespace. Call this namespace one just for simplicity. Then we'll have our open and close curly brackets, like so. Then we'll just cut this top function here and paste it inside, just like that. Then we'll copy this whole thing and paste it down below. And we'll just go ahead and get rid of this extra function and n here. We'll just change this to Hello namespace to. And now, as you can see, it doesn't know what we're talking about at all. And it says print hello is completely undefined. And that's because just like with our CN or a C out objects there has no clue what we are talking about and will actually change this, the namespace to down here as well. So we have to let it know where this print hello function is coming from. And we could do the same thing we could with our std namespace here, where we just put it right before. So we can say namespace, one colon, colon, like this, print hello. Then if we copy this whole line and paste that down below, like that, change lists the namespace to and run a code. You can see that it works just fine and it knows exactly which one we want to use because we specified where that code is coming from. Now, this example here we just used a basic function, but you got to have all sorts of code within a namespace. And just remember, a namespace allows us to separate out our code and be more specific about where that code is coming from as to help prevent clashes and naming. So for example, here we have two functions with the same name because they are a part of a different namespace, we're able to specify where that is coming from and these names will not clash at all. Now, just like before where we said we are using namespace std. Up here. We can say something like to use them. Namespace, namespace. And don't forget the semicolon at the end of that. And now if I wanted, I could just get rid of saying namespace one and run our code here. And this will run just fine because I've already specified that I'm gonna be using code from those namespace. Now you might be wondering, what if we did the same thing with namespace to run our code again? Well, as you can see, we got an error and that's because once again, it doesn't know what we're talking about right here. However, notice that we didn't get an error down here. And that's because we have two namespaces that we said we're going to use. And there's a clash in the naming these two functions, right? Because we have two functions with the same name in two different namespaces that we said we're using. Now. Other code within these namespaces that didn't have a clash in the name or the name wasn't the same. We wouldn't run into an error. We'd be able to use them both separately without any problems. So for example, if we just copy this function here and paste it down below that and do the same up here. We'll just call this one until this one. And this one down here, print hello to, well, I can say print hello one, just like that. And it's going to know what I'm talking about because there's only one function now with that name in any of the namespaces that we said we're using in our code in general. Now, it's going to work just fine. Can weight them to change that right here. So go ahead and do that. And if I run it again, you can see it prints it out namespace one because that's where this code was coming from. So if you are using multiple namespaces and you've explicitly said that at the top here. And you have a function that has the same name as another function, either in your code or within one of the namespaces here, you need to specify where that comes from, like we did here. So print hello to, we could use normally and print hello one we could use normally. However, print hello now, because we have two of them with the same name, it just needs to be specified. So just like we did down here, up here, the namespace, one, colon, colon like that, run our code. As you can see, everything works just fine. So there you go. That is how you create a namespace. Just simply going to say namespace followed by a name, and then a set of curly brackets with your code within it. Then if you want to use code from that namespace, you can either just explicitly do it like this or explicitly tell your code at the top, hey, I'm going to use encode from that namespace. And this will just help you to sort out your code so you know exactly where it is coming from, as well as prevent clashes in names with functions, variables, etc. So there you have it. Anyways, thanks for watching, and I'll see you in the next one. 99. Chapter 15 (Classes): Arrays Of Objects: In this video, we're gonna be talking about creating arrays of classes and taking a look at how we can work with that as well. So first, up at the top here, we'll go ahead and create a new class. And let's just say something like class. Item. Inside of our code here will say something like this. We'll say string, name, then gold, like that. And let's go ahead and make both of these variables private. And remember this was done by default. But we're just gonna go ahead and have it. They're explicitly because up above with us, we're going to create some public methods to work with. So we'll say public, like so. And then we'll create two functions, will create one called item. And this wasn't gonna take any parameters and it's not going to have a return type either. So we'll just say void item. Then for that we'll say void. And we won't have any perimeters for that either. So our class now looks just like this. And then we'll create the definitions for both of these functions down the lower class. So I will copy this here, paste that down, and copy and paste that as well. Then we need to explicitly say where they're coming from. So replica name, we'll say item colon, colon, just like that. And we'll do the same down here. Then we need the curly brackets for the definition of each method. Then inside of set item, we're going to go like this. We're going to say CL, item name, colon space just like that on that one there. And then below that, what they see it. And then after that we create a blank line, like so. And then we'll print out item cost space. And we'll put a semicolon at the end of that as well. And then we'll say C and go like that. And then down below here, we're going to print out both of these variables. So we'll say CL name, followed by a space. Space. We'll follow it up with Coke costs. Then in quotes again, we'll say space gold pieces and put a semicolon at the end. And actually it will go ahead and create a new line at the end of that as well. Make yourself, alright, so make sure that your entire class matches mine here. And down on our main function, we're gonna say const int size equals three. Then down below that will create a for loop. So say for int I equals 0, I less than size I plus, plus. Then let's go ahead and actually copy that and paste it down below. Now we have 24 loops though. We'll run through three times here. And then down below a constant here, we'll create an array of type items. Item, I'll call this items. Then n square brackets. We'll say size. What does put a semicolon at the end like that? Then in our first for loop, we're going to say item at index i. Since this will go from 0 to two, which is the same index of our items array currently, because this is a size of three. And then after it closed square bracket that here we're just simply going to say dot, set item, open and close parentheses, and a semicolon at the end. So every time it runs through our for loop here, it's going to run through the different items in our array of type item and run this code here from our set item function. We're just going to ask for an item name and then we'll insert a name, and then I'll ask for an item, gold costs, and we'll insert that as well. Then in our second for loop, we're going to say items at index I, print item. Then let's just add a bit of context text in each of these for loops, right? So we're gonna say item space and we print out I plus one. And I'll follow it up with space. Steps. Will say colon and then create a new line afterwards. So we'll just say backslash n. Ok, so now it'll work just fine. Copy that and paste it in our other for-loop as well. Then if we go back up to our set item here, we're just gonna go ahead and remove this extra and mine sent. It's already going to go down a line after we put in the name here. Alright, and then let's go ahead and run our code. And as you can see, and that's for item on stats. And I'll just call this apple. Give it a cost of gold item to call it orange. Cost of three. And lastly, I'll say something like coconut and give it a cost of five. And then afterwards, you can see that we printed out each item stats. So I didn't want Stats. Apple costs to gold pieces, item to stats, orange columns, three, item through stats, coconut cause five and they go. That is how you can work with an array of objects. You're going to create a normal array with the type being the class name as that, as the type that we want to work with, the name of the array, just like usual and your size just like normal. And then all you have to do is use your member access operator to work with the available members of each class. By the way, if you are working with a collection of the base class type and then you insert items of a subclass type. So for example, if we would've had like a vector of animals before and then inserted cat and dog objects. You'll want to work with an actual array of pointers to those objects and not the objects and cells. And that's because each subclass will be of varying size and memory. Whereas if you just had a pointer to each of those subclass objects in your container, all the pointers will take up the same amount of memory and you won't have any issues. So like I said, if you're working with the container of a base class type and we'll be having items of your subclass types. As that will work, makes sure you're working with an array of pointers to those objects, and not just those objects themselves. That way, they all take up the same amount of memory and you don't have any issues. Anyways. That's it for this video. Thanks for watching, and I'll see you in the next one. 100. Chapter 15 (Classes): Alt Class Creation: In this video, I'm gonna be showing you how you can separate your classes into separate files. One being a header file and the other being a CPP file, which if you look at the top here, is what we've been working with this whole time. And these include statements that we've been working with, have been allowing us to access header files of other classes as well. And I'll be showing you how we can access or include our own class files. And this is really going to allow you to organize your code and use it in multiple programs as you'll be able to just access the file that it isn't. So let's get started. First off, here at the top, we're going to say class. And let's just call this my class. And then inside of here, we're just going to create a basic function. So we're going to say void, print, working open and close parentheses and a semicolon at the end. And then we're going to define this function outside of our class. Because if you remember, that is the proper way to define functions for your classes. So say boy in my class, colon, colon, or the scope resolution operator, followed by working open and close parentheses. And then we'll have our code block down below. And inside of here, we're going to say CL is working just like that. And there you go. Let's see how we can split this up now into multiple files properly. Now, just to add a couple of comments here. So your class declaration is going to go into something called a header file. All right, so this here is gonna go into a header file. And then your definitions for everything that you've declared is going to go inside of the cpp file. Right? So let's go ahead and do that. Then down here in our main function, Let's go ahead and also print out. Before we print that out, let's actually create a new line. So say M9, end of main, like so. Now I'm only going to be showing you guys how to do this in Visual Studio. But that being said, I recommend following along regardless as you may learn something along the way, and depending on what program you are using to write your code in C plus plus, you should be able to do something similar in there as well. And you'll simply have to look up the few differences and how to create the files themselves. Now, what we're gonna do is go over here to our Solution Explorer on the right. And if you don't see this, you can go up to the top here and click on View, and then click on Solution Explorer. So all we're gonna do is right-click over here and the Solution Explorer go down to Add and then click new item, then click where it says dot CPP. And down here at the bottom, instead of source, we're going to type the name of the class we want to create. In this case, we'll say my class and make sure it's spelled the same there. Then go ahead and hit Add. And now you can see our new file is right here at the top. Then we'll right-click over in our solution explorer again, go down to Add, click new item, and go to where it says header file. And then change the name here to my class dot h. And once again, make sure it's spelled properly and with the right capitalisation. Well, I add again. And there we go. Then if we go back to our main file here and cut out everything here that would go on our header file. Then we're gonna go back over to our header file, which is signified by my class dot h and paste it there. Then we'll go back to our main file again and cut out our class definitions. Go over to our CPP file and paste that there as well. And you're instantly going to notice that our CPP file here has no clue what my class is. And that's because just like in our main file where we included these header files here, we have to do the same and our CPP file. So what we're gonna do, I say hashtag. Hopefully. Then anytime you're working with or adding a user created header file and not one that's built into C plus plus. Instead of the less than and greater than symbol, you're going to use double quotes like that. Then we're going to say my class dot h. Now, you'll notice that it doesn't understand what C out means. And that's because it's part of a, another header file. And if we just mouse over that, you can see that you can hit Alt Enter to show potential fixes. And all I want to do is include the iostream file. So please click on that, will be added right there at the top. And we still need to include where this comes from. So once again, we can hit Alt, Enter on Visual Studio and either add std, followed by the scope resolution operator and C out. Or we can do what we did before and add this using namespace, std. We could have just typed all of this out manually, but I just wanted to show those of you using Visual Studio. Another way we can quickly fix our code. Then if we go back to our main file, we can go ahead and include our header file from my class. So we'll say hashtag include. Remember, we're going to put it in quotes. And as you can see in Visual Studio at already pops up our options here. So we can just double-click where it says my class dot h, and I'll print that out for us. Then down here, we can go ahead and create an object or an instance of our class. So say my class. This class. And then in order to access our function from my class, we need to go back over to our header file and make this function. Remember we're just going to say public followed by a colon and then put our function declaration below that. Then if we go back to our main file now, we can save this class dot print working within close parentheses and a semicolon at the end. And now if we run a code, everything is going to work just fine. As you can see, I printed out as working and end of main. Now, another thing I did want to mention is that if you end up closing out with any one of these files by accident. So let's just say I close out of the header file over in your solution explorer. Under header files you can see is right here. Just double-click on that and it'll open that right up. And then your CPP file is down here under source files and our main CPP file, and there's also under source files as well. One last thing I do want to show you is the following. If we go back to our header file here for my class, you can see that we have those hashtag pragma once. What this does is it makes sure that our header file is only included one time. And to show you what this does exactly, if we go back to our main file here and we copy our include header file and paste it again and run our code. As you can see, it works just fine. And that's because this hashtag pragma once basically says, anytime we include our header file here or state that we want to include it, it is going to check if it already has the declaration for my class, which if you remember, that's all that goes in your header file basically is that declaration for a class. And if it does this include statement is ignored. And if it doesn't, it will add the declaration. So the first time through and added are my class declaration and the second time it ignored it. However, not all compilers support this pragma once directive here at the top. And I'm not going to get into what directors are pretty much anytime you see this pound or hashtag symbol here, or with this hashtag or pound include, it is a type of directive. And like I said, I'm not going to get into it, but essentially not all compilers support those. So I'm going to show you an alternative, and I'm also going to show you what would happen if we didn't have that. So now if we try to run our program again, you can see we're gonna get an error. That's because we tried to redefine this class. So another way we can fix this is with what is called a guard idiom. And all we have to do is in our header file, we're going to say hashtag if N def, and then below that will say hashtag define. And then down at the end of our header file, we'll say hashtag. And at the end of our f and f and define here, we're going to add our header file name being my class in all caps. So I'll say my class underscore and then H. And this is a common naming convention when working with a header guard and, or a guard idiom thing. What we have here or the pragma once that you saw before. So all you're gonna do is have your header file name in all caps. And then instead of a diet, you're going to have an underscore. So up here it says my class dot h in here we have my class underscore age and all caps. Like I said, this is just a very common naming convention. Then we'll do the same after to find. So let's go over what this means. Now, if your compilers supports the hashtag pragma once that we had before, right? You can just leave that at the top. However, if it does not, this is another header guard, the format that you can use which should be supported. So let's go over what this all means. So first we have this hashtag F and F. That means if not defined, and what are we checking? If it is not defined is this header file right here, and that's the name we're giving our header file here. Essentially, you can look at everything passed this directive here as the body of our if statement. So if my class header file is not defined, then we want to define it. And down here, we need to end our if statement. But instead of ending it with a closed curly bracket, we're going to say hashtag and right? And remember this is called a header guard if you want to look more into it. And as I said before, those going to do the exact same thing as the pragma once. So if your compiler support site, Great. Not just add the following to your header file. Pound if N def followed by your header name, then you'll say hashtag define with the same name right underneath that. And then at the very end, you're going to say hashtag. And then just remember, your header file includes all your class declarations, which is why I've said in the past, we want to separate our declarations from our definitions. And then in our CPP file, we have all of our include directives. And I know I've been calling these statements throughout the course, but they are technically called directives. So you're going to have all your include directives, any using namespaces that you might want to use, and then your class definitions. Then in your main file, you need to include your class header file if you want to use that class. And we can add as many header files and CPP files as we need to split up our code here. And remember, all we had to do to do that is click on our program name right under solution here, right-click, go down to Add and then new item. And then we added each file individually. And now, even though we have two declarations of our header file, just like when we had our pragma once directive, our new header guard that we just created will work. So if we run it, you can see that this works just fine. Now in a program that's small, you likely won't be accidentally adding your header file twice or trying to redefine the class twice like this. But when you start working with much bigger programs, they can definitely become an issue. So you want to make sure you can avoid that by using header guards, either using the hashtag pragma once if your compiler support set, or you can just use the hashtag if and death hashtag define and hashtag and F, like we did here. So there you go. That is how you can split up your classes into individual files. And then this will also allow you to use your classes in other programs. And hopefully this kinda drives home why we been separating our class declarations from our class definitions as it makes it really easy to just cut and paste them into the separate files as needed. And remember all our header guard did was allow us to accidentally essentially redeclare our class without any issues. So I'll go ahead and remove this. Now, there are some specific cases where you don't actually want their guard, but most of the time you will. And if you want to look into when you wouldn't want that, feel free to do so. But I'm not going to be getting into that in this course anyways, that's it for this video. Thanks for watching, and I'll see you in the next one. 101. Congratulations!: Congratulations on completing this course. You've taken a big step forward in your programming journey, and you should be very proud of everything you've achieved here. If you enjoyed the course or found it helpful in any way, we'd greatly appreciate it. If you could leave us a positive review. Your feedback not only helps us to improve, but also encourages others to start their journey here as well. Now, try and think back to when you first started. The idea of coding likely fell overwhelming. Now look how far you've come. You've mastered the fundamentals, explored advanced topics, and gain the skills to start creating your own projects. Now that you've completed this course, your next step is to put those skills into practice. A great way to solidify what you've learned is to tackle a small, manageable project, something that you can complete in like two to four weeks. And I'd highly recommend starting by cloning an existing simple application for non commercial purposes, of course. And by creating something that already exists, you can focus on learning how each feature works and how to implement them yourself rather than designing it from scratch. And then once that's done, try adding your own unique twist or expanding on the concept as a second project. Is a fantastic way to both practice and flex your creativity while building confidence in your skills. And then from there, you can start branching out to bigger, better and more unique projects of your own. And remember, it is perfectly okay to come back and revisit the course material as you work on your own projects. Programming is all about learning as you go. Even season developers constantly look things up, whether it's refreshing their memory or solving a new problem. Using resources like this course or the Internet is not a weakness. It's a part of being a great programmer. Now, this is just the beginning. Whether you were working on your first project, returning to refresh your knowledge or diving into another one of our courses, the knowledge you've gained here will be your stepping stone for even bigger achievements. Speaking of which, be sure to check out our other content for more learning opportunities. And if you haven't already, please consider leaving our view to share your experience. It really does make a difference, and we'd love to hear from you. From all of us here at Primoris Arts, congratulations again on this amazing milestone. We can't wait to see what you create next, keep learning, stay curious and remember the sky's the limit. Well done, and best of luck on your next adventure.