C++ Lesson 1: Intro
You know, welcome to the first of many C++ lessons. Now, for these lessons, I'm going to be using Xcode on a Mac, and if you want to follow the story exactly the way I do, I recommend you install Xcode if you're watching from a Mac.
So, yeah, the first thing you're going to do is install Xcode, and if you've done that, then you're going to want to go to the Xcode menu and say file, new project. This is all for the current version of Xcode. If you have a later version or previous version, things might be slightly different.
So, we're gonna go to Mac OS X application, command line tool, C++. Now, we're going to say choose, and it's gonna ask for the name of our project. First, we'll make a new folder and we'll call it C++ lessons. I recommend you do this too, and I'll save that onto my desktop, and we'll call this lesson 1.
All right, so now Xcode can bring up a window that looks something like this. So, pretty much the only thing you really need to worry about is main.cpp, so I’ll make that very big and focus, so it's pretty much all we're looking at.
Now, I'm gonna go over the structure of a program. These tutorials are gonna be pretty slow-paced, and I'm assuming that you don't know anything. This is your first programming language, and the only thing I'm assuming is you've installed Xcode.
So, I'm gonna go ahead and explain these. If you want faster-paced tutorials, then you could look for something else. These, they're gonna be slow-paced for beginners.
So, we have this as our program. I'm just gonna space things out so you can see better. This is actually a comment, so this is not even part of the program. I can delete that.
So, first, before I do anything else, I'll just run the program that is already here for me without even writing my own program. So, I'm gonna hit build and run, and programs, they run in the console. You can either hit command shift R, or you can go to run and say console.
All right, so this program, all I did is say "Hello, World." So now I'm gonna explain why. This just includes some files that you need in C++ for it to work. You can include more than that later, but we might do that. For now, that's the only thing we're gonna include.
And, like, for example, if you're doing a program that uses a lot of math, you might include something called math.h. Then you have something called your main function, which is everything in between these two curly braces.
You can actually delete this; this is just gonna get confusing, and we don't need to worry about that right now. So, we have this, which is just including some files. Then in between the main function, we have this line of code which says "Hello, World," as you can see right there—it’s at "Hello, World." And then it just returns zero.
Return zero just means—don't worry about that. Just leave that there, just means return zero. So, std:: you don't even actually need that. If you don’t want to put that anymore, just remember at the top of your program, say using namespace std and then put a semicolon.
All code has semicolons at the end of the line unless it has curly braces like this. Yeah, so "cout" stands for console out. So, in our console, whatever message we had went out into our console, and our message goes in quotes.
And you don’t even need that; that’s just backslash N. The N stands for new line. So, that’s why I think that you can see they put a new line there. But an easier way to do it, I think, is endl, which stands for end line. It’s easier for me to remember.
So, if we’re going to—for every new thing, I'll just say "thing" for now—every new thing that we want to print out or send out to the console, we’re going to put in between carets. So, we’re going to put in carets, well actually less than symbols, carets.
But yeah, uh, and L for end line. I’m gonna space this out just so the way things are easy to read, because again these are for me; this is tutorials for beginners. I don’t want anything to be complicated. If anything is complicated, just shoot me a question or tell me I’m going out too fast of a pace.
So, we’re gonna clear the console. To do that, you just hit clear log, and you always have to state before you run. So we’re gonna hit build and run, and as you can see, it did exactly the same thing—"Hello, World."
"Hello, World" is usually the first program people make when they're learning a new language. So, we just together made this program.
So, as I said earlier, in the quotes is whatever string or whatever chunk of text you want to send out to the console. So, we can actually change this. Let's just—all that matters is it's in quotes.
So, why don’t I change that to "Hello, viewers"? Sorry about that, phone’s ringing, but I'm not gonna answer that. All right, "Hello, viewers." Now, we’re gonna save, and then we're gonna go back into the console. I’ll clear it slowly so we don’t have all that junk in our way, and I’ll say build and run.
And what do you know? It says "Hello, viewers!" So, cout just means console out, and then we have our message in quotes. So, we have the carats—the mat, whatever thing we want.
So, the first thing we want is a piece of text or a string that goes in quotes, and then endl for new line. So, let's say I want another thing. I'll put another new line, so I'll put endl again.
Don’t worry about that. Then I’ll build it again, and now you can see there’s two new lines in between. So, yeah, that was one cout; that was one line of code in our main function.
We’re gonna help put another line of code. So hopefully you understood that, and the new line of code will just be reviewed. So I’m just gonna get rid of that line because it bothered me, and just one new line is good enough.
So, we’ll say "Hello, fellow viewers," or "Hello, viewers," and then we’ll make a new line. You can do that by hitting enter, and then I’ll say cout for console out, and then Xcode already has everything laid out for me, like the carats, the expression, the semicolon.
So, I can just hit the right arrow key. Wow, someone really wants me to pick up the phone! All right, so I can just hit the right arrow key to accept that, or I can type it out myself. I’m gonna type it out myself just to clean and practice.
So, our carats, and then we’re gonna put another message or another string or another chunk of text. I’m gonna just refer to weight as string from now on. So, string equals chunk of text; just remember that.
So, another chunk of text; it goes in quotes. And what should I—I couldn’t hear. I’ll just say "Thanks for watching!"
All right, so we have this, and then we have this—first line of code, second line of code. Now, what’s the difference? This line of code has an endl; that’s a new line, but we don’t even need a loan, and we just wanted one more piece of text.
So, what do we need? A semicolon! All lines of code end with a semicolon. So, let me just try not putting a semicolon—see what happens. So, if I don’t put a semicolon, it’ll give me an error right there, and I’ll say expected semicolon before return.
That means it’s expecting a semicolon before this return. So, let’s go before the return—beauty to do right there! A semicolon, so it will tell you the error is, to some extent, and help you debug.
But it’s a great habit to just remember to put a semicolon. I’m just gonna—now that we’re familiar with the semicolons, I can put them right by the endl, and that won’t confuse it.
All right, so now I save my program. Let’s try now that it has semicolons—try building it again. So, let’s clear the log, and then I’ll build it again.
"Hello, viewers! Thanks for watching!" And the reason why it says running there and not like down there like it did before is because there’s no endl, so it’s gonna do whatever on the same line.
But this is all part of one program—our first C++ program. And so, yeah, if you’ve watched this video, you successfully made your first C++ program, and all this does is print out two lines.
But you can—you can have infinite cout statements. You can say cout 1 or cout 2 or let me try putting a lot of cout statements and see what happens.
So, I’m just gonna copy that and paste it a couple of times, and then we’ll hit build and run. So, essentially—well, yeah, I should have put a new line. That all runs on the same lines; like that’s kind of annoying.
But pretend—pretend I think y’all! Yeah, that’s not too confusing. So, it says "Hello, viewers! Thanks for watching!" Thanks for watching.
So, I can have as many cout statements as I want, and the code runs in this order from top to bottom. So, it'll run here and it'll go and say "Hello, viewers!" You can put whatever you want in there.
I put "Hello, viewers!"—your first string, and then it makes a new line, and then it says, "All right, let me print something else to the console!" And then it prints this string, and then it goes to the next line of code, and then it prints something else to the console, which is another string, and that string is also "Thanks for watching!"
And you get the idea. So it starts here, and it ends right before your return, and it runs all the code in that order.
Yeah, so hopefully I'm just gonna delete that because that is some ugly code right there. So hopefully this tutorial wasn’t too fast-paced, and you were able to understand it all.
So, hello fellow viewers, and thanks for watching! Yeah, feel free to come back to our channel for more videos later. This has been the first of many C++ videos. Thanks for watching!