C++ Introduction: First Program

To understand the basics of a C++ program, we'll take a look at the code for the program used in the preface:

test.cpp - Click here to download
/* test.cpp
   August 16, 2015
   Steven Dolly
   This program tests to make sure the C++ compiler works. */

// This statement includes the iostream header file into the program.
#include <iostream>

// This statement tells the program to use the standard namespace.
using namespace std;

// This is the main function
int main(){

    // This statement prints "Hello World!" to the screen.
    cout << "Hello World!\n";

    // This statement ends the main function, returning a value of 0.
    return 0;
}
Let's start at the end first and work our way backwards:

The Main Function: int main(){}

First, the main function. Every C++ program must have a main function, making it the most important part of any program. I will explain more about functions in a later lesson, but right now all you need to know is that when a function is called, the program will perform everything within the curly brackets ({ ... }) in order, line-by-line until the end of the function is reached. In this case two lines are performed, in this order:

    cout << "Hello World!\n";
    return 0;
  
The first uses a command, cout, which prints text to the screen (see the next section for more information). The second is an important part of functions referred to as the return value. This will become more clear as I explain functions, but for now just make sure the keyword "int" is in front of main and that the main function has the "return 0;" statement at the end of it. The goal of a C++ program is to carry out the main function; every line of code outside of the main function only serves to help the program carry out the main function.

Including Header Files & Using Namespaces

Speaking of code outside of the main function, let's talk about namespaces and header files. The C++ programming language already contains a large number of functions and commands for you to use, making things much easier. However, before you use these you must tell the compiler which ones you want to use. Groups of commands and functions are stored in files called libraries, and each library has a header file which contains a list of all the commands/functions found within the library. The iostream library contains all that you need for input/output to the screen (for example, the command cout), so we must include the iostream library in the program if we want to have screen input/output functionality. This is accomplished by inserting one line of code into the program:

    #include <iostream>
  
The idea of namespaces is similar to that of header files. Many of the commands and functions that come with C++ by default are stored under the standard namespace. I personally don't use namespaces unless I absolutely have to, so usually I just add the following line to every code file:

    using namespace std;
  
which tells the compiler to use the standard namespace.

Comments & Punctuation

Finally, you've probably noticed that I have said nothing about the lines of code that start with two slashes (//) and text that is in between the slash-asterisk combination (/* ... */). These groups of text are called comments, and they are completely ignored by the program. They are only here to help you and I understand and explain what each line of code does. One final thing before I move on: please note that every statement (except for the #include) ends with a semicolon. These semicolons must be at the end of every statement or you will get an error when you try to compile; this was the most common mistake I made as I was learning to write programs.