C++ Tutorial #1 — Hello World


Topics covered: During this video I introduce #include (used to include a header file into a program) using namespace std (this will be discussed at length later on) and cout.

Source code:


Hello World
“Hello World” is the quintessential “starter program” in most programming languages, we simply include enough files to write this message to the screen, and then try to explain what each part of the program does. In this case the program will look something like this (I don’t have the source code transcribed for this video right now):


#include <iostream>
using namespace std;
int main(){

    cout << "Hello World" << endl;

    return 0;
} //end main

Now let’s break the above code down part by part and explain what each part of it does.

#include <iostream>
This line tells our compiler to “include” the library “iostream”, which is also known as input/output stream. This library contains all the code required to use the functions cout / cin (covered later) in our programs. For quite some time cin / cout will be all that we’re really using iostream for, as many of the more advanced uses of it just aren’t sensible at this point.


Using namespace std;
Namespaces can be fairly confusing to some people, because until you see what the above program looks like without this statement it’s hard to understand what exactly it does, so I’ll show you that, and then try to explain it a bit better:


#include <iostream>
int main(){
    std::cout<<"Hello World" << endl;

    return 0;
}

In the above code you’ll notice that I left out the using namespace std; but had to change cout<< to std::cout<<. This is what the using namespace std; line does. It allows us to use items from the ‘standard namespace’ without mentioning the namespace that they’re in. We’ll talk about namespaces MUCH later on in the series when we get out of using just STD, and maybe get into Boost and other libraries.


int main(){
This line is fairly simple, it shows where the start of the “main” part of our program is. I want you guys to take special notice to the { though. That’s known as a Scope Bracket, these can make your life easier or harder based on the habits you develop with them now. You guys should use this as something of a rule when it comes to programming: “Every line I write should end with either a ; or an { / }. This will make your life much easier, especially in the early stages of programming.


cout <<”Hello World”<<endl;
This line calls the function “cout” from the iostream library, and passes in the text string “Hello World”. The part after that (endl) is just to make a new line after that. We’ll get more into how this works in subsequent lessons


return 0;
This line tells the compiler that it can “return” (to the command prompt) if everything went well. The return command will make more sense later. The reason we’re using return 0 and not some other number will be covered way later as well.


} //end main
This is really just a closing bracket } , the //end main after it is what’s known as a comment. Comments are used by programmers to keep track of what’s what in a program. In this case when you have a longer program with many brackets, you might want to know that this bracket is closing your int main() opening bracket.