OK.
Hello, everybody.
Welcome to another lecture.
Today's lecture is going to be the big one.
We are going to write our first C++ program.
It's also going to be a lecture that kind of combines the first time we are going to use the vs code
lightweight text editor.
So whatever lightweight text editor that you might want to use, this video is kind of going to be about
that as well.
And it's going to be a Windows kind of themed version rather than Linux or Mac, but we're going to
try and cover all of those.
Don't worry, there will be a video for your operating system and the type of editor you want to use.
We're going to cover.
I think I'm reiterating this, but it will be kind of we'll go over a lightweight text editor like VSCO.
We will go over a big, robust all the bells and whistles, integrated development environment like
sea lion.
And then we will also go over command line based editors like them.
So we'll go over those three things so you can code C++ in whatever environment you want.
If you are watching this video right now and you do not have a C++ compiler installed, please go back
in.
Yes, this is only if you're on windows.
If you do not have a C++ compiler installed, please go back and watch my men GW installation video
where I tell you how to install a C++ compiler called Manjhi W, which is the minimum new compiler collection
for Windows.
And make sure you get up to speed on that.
So you have something because we are going to need a compiler to compile and run our C++ program today.
So love talking and not doing.
Let me get started.
I'm going to click on this code here.
If you don't have your text editor pinned to the taskbar, just find it and open it up.
So I'm going to go up here to file and new file, so I don't have this untitled one.
We get rid of that, and I'm going to click select a language and say this up here.
And I'm going to go down here and choose C++.
The reason I did that is because text editors actually have a great feature.
Pretty much all of them where they color code the syntax for C++.
So when you're typing code, the differences and the keywords and things that you are typing will lead
to it being color different, and that makes it very nice and readable.
And I think that you'll probably enjoy a certain color schemes better than others.
And in the future, I might show you how to adjust those color schemes and things to your liking.
So another thing I want to say is that this is going to have a ton of information, I know I kind of
said that before, but this video is just going to drop all kinds of stuff right off the bat, which
is not something I want to do, but it's something we got to do.
We need to have some type of basic program just to show you that C++ can do something.
And later, we're going to go into all of the pieces of this program that we write today.
So the things that I say today, just take it with a grain of salt, but know that everything that I
write down here is kind of like what you need to get the most basic program up and running, and that
is going to be the ever so cliche Hello World program.
Some of you might have heard about that if you've done any other coding in any other language before.
Most of the time, the first program that everyone makes is something called Hello World, where it
just prints those words Hello world to a console.
So we are going to have a console here.
A console is just basically something at the bottom of the screen that shows the output of a program.
It might also include errors that get output it if there is a problem with the compilation process or
a runtime error or something like that.
So, yeah, that's what a console is.
We're going to be using the term run all the integrated Terminal four vs code for that later on.
But first off, let's write our code right.
So I'm going to give a brief explanation of each line in this code, but it's going to be like a very
kind of abstracted explanation, a very high level view explanation of what is really going on.
So if there's some of you out there that already know about this stuff a little bit, yet you're still
watching this video and you're just like, Oh, that's not true what you said, it's a lot more to it
than that then.
Yeah, you know, I'm just kind of explaining it in a super layman's terms way, just so you can have
a general idea of why the heck, we're writing so much code just to print the words Hello world.
So with that, let me get started.
I'm going to put this hashtag symbol here, and I'm going to write the word include.
And then a less than sign, then the word I'll stream and a closing sign here.
So I had the less than sign than a greater than sign here.
To close it.
So what is this?
Well, all you need to know about this right now is that it's including something, right and it has
a hashtag.
You need to put a hashtag before you add the word include in C++.
And what is it including in our program is including something called extreme before these less than
greater than science?
What is extreme extreme is basically a toolbox that lets us use some things to print stuff to the console,
which is what we want to do, right?
We want to print the words Hello world to the console.
It stands for input output stream, and we will be using a lot of streams later on in the course not
just input output stream, but file streams as well.
Where we put stuff in files, we write stuff to files and read stuff from files.
So just a little heads up on that.
That's what we're going to be doing later with some streams.
Very common thing.
You almost always want to include this in your program.
As you'll see, it helps a lot with certain types of debugging and caching errors and stuff.
So the next thing that I am a type is not necessary to make the program work, but I'm going to type
it anyways because it lets us write a little bit less code.
I'm going to say using namespace STD.
And for those experienced C++ developers that have still maybe gotten the course just to go over some
algorithms or something.
This is something I'm just introducing to let people know kind of what it is, we'll be using this namespace
hosted a little bit in the course, but I know that a lot of C++ developers do not like to put this
in their code and for good reason, but we'll go over that later right now.
Just put it in your code if you are just starting out and do not know what all of this already is.
So the next thing that I'm going to do is put it, which stands for integer like a number like two or
five or 100 and then main with some parentheses.
And then I'm going to put one curly brace, but it's going to make two curly braces for me because the
editor is cool like that.
This right here is me defining a function, the function is called main, and this functions thing that
it does could be really anything.
It could be whatever we put in between these curly braces.
But whatever you put before, the name of the function is what the function returns.
So we're going to get into all of this stuff later.
We'll talk a lot about functions, but I just want to give a basic explanation.
When the function returns something, it just means that it's like giving you back something.
You might give it something in that would go in between these parentheses, actually, but we're not
going to do that right now.
I'm going to go in between the curly braces and press the enter key that's going to, like, separate
them nicely and then I can put what I want the function to do here in C++.
We have to make this main function to be able to do some stuff right now.
We could maybe in another programming language only have like one line of code to be able to do what
we want to do right now, but C++ is a little more complex and so we have to write all the stuff.
So now I'm going to use a tool from this little toolbox here.
And it is something called See Out, which just stands for outputting to the console console out is
what you can think of it as not only do I have to put that, but I have to put this thing called the
output operator.
It's like the output stream operator, it's going to be two of these little less than science.
And then after that, I put whatever I want to be printed out to the console.
So we want hello world, right?
So I put hello world after this, I'm going to put another two of these a little less than science.
So another one of these output stream operators, because I don't only want it to just print Hello World,
I also want it to move to the next line.
So to do that, I'm going to put in the line.
So Intel is what you do for line.
And in C++, you for pretty much all of your statements, you will be putting in a semicolon after that.
Sorry, I didn't explain that when I did the using namespace STD.
But that is something that you're going to put at most at the end of most, all of your statements.
You notice there's not one at the end of the function.
That's an exception.
So we have this CEO Hello World now, and then like I said, functions return things.
This returns an integer and it's kind of a convention in C++ to return to zero.
We do not have to do that.
I'm just putting it here.
I will go into this in much more detail later on why we have the return zero.
What really is the main function right now?
You should just know.
The only thing you really need to know is that main is a special name for a function.
C++ recognizes this specific name of this function, and it is like a special function that kind of
like the entry point to our program.
We will be getting lunch into greater detail, like I keep saying on all of this later, but this is
just a basic explanation of why we had to put this stuff here.
We need a function to be able to print something out and we need to return something from the function.
Well, we don't need to, but it's a convention that I'll get into much more later on.
So this is actually kind of complete now.
This is the bare minimum that we're going to be able to do to make this whole world happen.
So with that, what do we do now?
We wrote our code.
You remember what we talked about in the previous lectures.
So once you write your code in some sort of file, then you need to use the compiler to compile the
code.
And once the compiler compiles the code, then you can run the code.
So that's what we're going to do now.
But before we do that, we're going to have to save our files.
So what I'm going to do is I'm going to go to file save as I am in this folder, Dylan, right here.
I'm just going to call this.
I'm going to make a folder called Hello World.
Or actually, it looks at the file.
I'm going to call the file Hello, world that, but I'm not going to need the extension because they
already are.
It's in the same as typed right here.
They to have the extension here.
What I am going to do, though, is I'm actually going to make a new folder, so I'm going to go here
and I'm going to say new folder and just right clicked, and I'm just going to say this call this first
program.
All right.
And I'm going to save our file and here you can save it wherever you would like.
Actually, I am just choosing to make this directory, so I have like a clean directory here.
And so I have my file name here.
Here's the extension I'm going to say save and it's going to save our file.
Then you notice now that it's saved here and it actually shows the path that it was saved in.
So we have a C drive users still in first programs, the folder we made and then our file Hello World
Dot CPP.
I think I mentioned in the previous lecture, but the Dot CPP stands for C++.
It is an extension, a file extension similar to like dot text.
Things like that dot doc x, that good stuff.
So that's what the CPP stands for.
We will see that later on.
There are additional extensions for C++ program files.
So right now, though, we're just doing the CPP file.
So now we need to compile it since I am on a Windows machine and I'm in this code.
I am going to use this integrated terminal.
So I'm going to go up here and say new terminal.
And that brings up this thing down here, which is kind of what you can think of as the console.
This is actually not just a console, though, it is a true terminal.
It's like a command prompt or you can type commands in here, and that's what we're going to do in here
as well.
If you have an integrated development environment that you are doing this tutorial with, like Sea Lion,
it's basically going to be kind of at the bottom of your screen.
Remember that we will go over how to do everything at Sea Lion.
So if you're following this tutorial and it's not really making sense to you, then maybe just kind
of skip ahead to the one where we do it in sea lion.
So we will also be going over this, you know, using the standard command prompt in terminal and using
an editor in there, in which case I will be using a bash shell, a more Linux style shell that will
have similar commands to like if you are on Mac.
So, yeah, just wanted to say that.
So what do we do now?
We got this terminal?
I am not assuming that any of you know how to navigate through the terminal.
If you do, that's a great go to your file and then compile it and run it if you know how to compile
it.
If not, just go to the location of the file and wait there until I get there.
For the people that don't know about how to do that.
I'm going to introduce some commands.
First off, notice that this path appears, says C Drive users Dylan first program Hello World.
We are here, though C Drive users doing so.
We're in this first part.
This is the location that we are in.
We need to navigate to here where our program is, right?
So how do we do that?
We are going to use a command called C, which stands for Change Directory.
After that, you need to type the name of the directory and it has to be a directory that is in this
current folder that you're in.
So what we are going to do is type first program because we noticed that this is the path, right?
First program is inside of Dillon.
Dillon is inside of users.
Users are inside, inside of C Drive.
So the first program is located in the current directory that we are in.
We noticed that the right most thing on this line is Dylan.
And we know that first program is inside of Dylan because it comes directly after Dylan up here.
So you can also double check this by just seeing if it can recognize it, so I started typing.
First program f i r s, then I stop right here, I'm going to press the tab key.
And you notice that it recognized it and completed it and also put the path around it.
So the Dot says current directory, that's what that means.
Then inside the current directory that you're in.
It puts a little slash here and says, OK, first program, that's the directory.
And then it puts another slash here.
So this whole line is going to take us into first program.
So I press enter.
Notice what it says over here on our prompt now it says C users Dylan first program.
Now we are inside of the first program directory that we created, and we know that this is to the right
of first program.
That means that Hello World is in first program.
Now I'm going to show you a way to see what is inside of the directory that you are in in here and that
is using the L.
S Command L.
S stands for list and it will show you all of the other folders and files stored inside of the directory
or in since first program.
It will show us all of the folders and files that are inside of the first program directory.
So I type that and we notice there is this.
Hello, World Cup right here.
You see this right here.
Hello, World Cup.
So what I am going to do now is compile this.
So remember this command and this is just to kind of reiterate if you don't have a C++ compiler on your
Windows machine yet, please go back and watch the C++ compiler.
VIDEO For Windows, where I install many GW on windows, then come back to this point.
So I use G Plus Plus Dash Dash version.
This tells us that we for sure have the G Plus Plus package from GW so we can compile C++ code.
All I have to do is G Plus Plus and then a space and then the name of our file, which is Hello World.
So I start typing hello and then what I'm going to do is press tab again, just like we did before you
notice that it nicely completes that for us.
It doesn't autocomplete.
And that is because we are inside the directory that has hello world and it recognized it and finished
it for us when we press tab.
Once I press enter, what will happen is that the C++ compiler will go through the code in here and
it will turn it into something that the machine, which is our computer, can understand.
That way, we can run it.
I press enter.
We don't see any output here.
So that's a good thing.
If you saw some stuff pop up, most likely you have errors.
So don't worry, don't panic.
Go back up to where you wrote the code and make sure that it matches exactly what I have here, and
then try and run this same command again down here in the terminal.
So let's see what happened to you that we're going to use another less because what should have happened
is that the compiler should have generated an executable file which would have a dot e e extension instead
of the Dot CPP.
So if all was good, we should see both our Hello World Dot CPP code file, as well as the Dot XY executable
file, which is the file that we will run to get our output.
That is the C++ file that's been turned into something that can run on windows and is understood by
the computer.
So with that, I type else I press.
Enter.
Voila!
Look at this.
We have hello world dot CPP, but then we have a dot ICSI.
OK, we might get why it says XY, but why that a.
Well, the reason it says Ada XY is because we did not specify anything in here except to just compile.
This program that we wrote the CP file, but we never told the compiler what we wanted the executable
file to be named, and we can do that when you don't do that and you just do D Plus Plus and then a
space and then the file name with the Dot CP extension.
It just automatically defaults to calling the thing a dot XY.
If you are on windows, or it might call it a dot out, depending on what other machine you are on or
something else.
But what we can actually do is instead.
We can call it we can call it something else.
So I have this a key, which is fine and it will run fine.
But maybe I want to call it something else.
I'm going to show you another command right now that you should be extremely careful with.
Do not use this command on anything but something you are sure you do not want to ever see again.
It is the remove command.
And it is done with an R M. Sometimes the computer, the operating system will protect you from deleting
things that you really shouldn't be messing around with, but not all the time.
You don't really know.
You might accidentally delete something that you really don't want to delete, so just be careful with
this.
But I am going to do rim in a and then a dot, and then I'm a press tab.
And then we know that since it tab this out and completed it, that we are going to be deleting just
the ADX e file I press enter.
Now I'm going to take a look again to see what is in the directory, and we notice it is no longer there.
There is no ETA.
At this point, I'm going to do another G Plus Plus, but I'm going to do my hello world and then I'm
going to do a dash.
Oh, and that means that I want to specify I'm going to name the output file.
And what do I want to call it?
I think I want to call it.
Hello, Dot VXI.
So if I run this, it will compile.
Then it says, Oh, you would like to name an output file because you put a hyphen o, which is also
like a flag.
You know, this is something you can refer to as a flag as well.
And I'd put Hello XY afterwards because that tells the compiler what I want to name this output file,
so I press enter.
No problems there, so that's good, I type else again to see what happened, and here and there we
go, we have a program called hello rather than a.
Now what we can do is we can run this program by doing a dot and then a slash like this, and then I
start typing Hello.
And I press tab and we see hello.
Now I press enter.
And look what it does while it printed the words hello and a world to the console.
So just to prove that it's actually doing the stuff that we wanted to do and it's printing whatever,
it's not just some magical program that only does.
Hello, world, no matter what you write in code here.
Let's go ahead and change this.
I'm going to change this to Dylan.
And now I change this, it says to unsaved.
So what am I going to do?
I'm going to file save and now I am actually going to compile this again and I can.
You notice that this is changing here.
That's because I'm pressing up on the keyboard.
So if I press up, it goes back to the previous command that I did.
If I press up again, it goes before that.
I press up again, it goes before that.
And so that's a nice way to kind of backtrack in your history of what you've typed in the terminal,
the commands you've ran actually in the terminal.
And now I can just press enter.
I don't have to retype this whole thing.
I press enter and nothing happens, so we should be good to go.
Now I'm going to press up again and up again, and you notice that it's the same command I used right
here to run it.
Hello.
See, when I put the dot slash before it, and I'm going to press that, enter again.
And while I look, it changed it to Hello Dylan, now.
So that kind of shows you that whatever we put in between here is going to print out to the console.
Something else that I did not really mention.
I just kind of typed it where these quotations here.
You must use these quotations for right now if you just want to make some more programs that print out
different stuff.
Make sure to use the quote, the double quotes right now.
This is something called a string right here, and we're going to be getting into that in the near future.
Like I said, this is the first video we're going to take like a huge step backwards and start talking
about all these little pieces of C++.
This is a lot and we're not going to be like necessarily.
Using this type of program over and over and just explaining what we're going to do is take a big step
back and learn about data types and all kinds of stuff.
This was just to like, say, Hey, you can read program in C++ that print something out just so it
felt like you actually knew how to write a program instead of just going over a ton of theory right
away, which will take a huge step back and we'll go over the theory before the next program, we write.
OK, so hopefully this was a fun lecture for you and that you feel pretty cool if this is the first
program you wrote.
You wrote some code, you know how to write a program now.
You technically can write a program that prints any text to the council now because you could put anything
in here between these double quotes.
And it is going to print it to the council.
I can just smash the keyboard and put a ton of stuff here.
Like this?
Go here.
I can save it.
I can go here and press the up to compile it again.
I can go up here to run it, run it again and look, it just prints all of that stuff that I typed here.
So maybe not the most amazing program of all time, but still pretty cool, right?
If it's your first program.
So congratulations on getting just this far, even you have learned how to write a program in C++ in
the next lectures, we are going to be going over some other editors.
We're going to take a huge step back and we are going to go over some data types and stuff like that.
But.
Till then, go ahead and practice just typing some things in here, if you repeat this code, just you
clicking the keys and doing it, even though you're not so sure what all of this really means, it will
still help you, but we are going to go.
Way back and start at the very basics now and explain all the really fundamental concepts, OK?
With that, I will see you in the next lecture.