This is a program that prints out "Of man's first disobedience"; it is traditional to begin talking about a computer language by giving an example program which prints something out. This is often called a "Hello World!" example. The line numbers, and the space following the line numbers are not part of the program, they are just there to make it easier to explain what is happening. Sometimes the example numbers include letters, this is just a trick for adding extra examples without renumbering.
HelloWorld.cpp
1.1
1 #include<iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 8 cout << "Of man's first disobedience" << endl; 9 10 return 0; 11 12 }
This example looks quite complicated and obscure at first and contains statements that we won't think too hard at first, this example does give the standard form that all C++ programs take and we will discuss each part of this program soon. First, though here is how the program is run. Here, the dollar sign, $, is supposed to represent the prompt, the prompt does vary for different command shells, for example, it might read something like salmon ~/www/481++/text%.
1.2
1 $ g++ HelloWorld.cpp -o hw 2 $ ./hw 3 Of man's first disobedience 4 $
The command on line 1.2.1 compiles the program, that is, it converts it from the high level c++ code we can understand into the machine code the computer understands. g++ refers to the gcc compilers, other compilers will have different commands. After g++ is HelloWorld.cpp, the name of the program, after that is -o hw which tells it to name the machine code program hw; if the -o hw is omitted the program is given the default name a.out. On the next line the program is run. When the computer is given a command it looks in a set of standard places for the program corresponding to that command, unless you have set it up otherwise, the folder containing your program will not be one of those places, so the computer has to be told that in this case it should look for the program in the current folder, that is the role of the ./ in line 1.2.2, this ./hw tells the computer to run the program called hw in the current folder, the current folder is always called ".". The program runs and the output appears in line 1.2.3.
Lets go back to the actual program: before we begin, most spaces in a C++ program are there for visual convenience, it is useful to put lots of spaces in, but the convention is arbitrary since the compiler ignores them. Obviously in the program, in line 1.1.5 for example, the space between int and main is needed, the compiler wouldn't know what intmain meant, but the space between cout and << in line 1.1.8 is not needed, the compiler would know that cout<< is a cout followed by a << and so that space is only there to make the line easy to read. This is also true of new lines: lots of extra vertical space make the program easier to read. Now, to the program itself, line 1.1.1 is an instruction to the preprocessor; the preprocessor makes changes to the program before it is actually compiled. In this case it is including the iostream package. c++ has a core and a large set of packages, although input and output is an important part of most programs, it is nonetheless not in the core, but has to be loaded as a package. Without the #include<iostream> the part of the program that prints out the words "Of man's first disobedience" would not work. At the moment, this line should just be seen as something that is included in a standard c++ program.
The situation with line 1.1.3 is somewhat similar, it is not going to be immediately obvious why it is needed and it should be thought of as just a standard part of the program template. Roughly speaking c++ has what are called namespaces. The idea behind namespaces is to avoid accidently using names that the language is already using for something else. Here, for example, in the iostream package there is a command cout which is used for printing to screen; if you weren't used to the iostream package you might inadvertently use cout as a variable or whatever, causing complicated errors. To avoid this, any name used in iostream is put in a separate namespace called std, short for standard. Names in the std namespace are specified by the prefix std:: and this will distinguish them from any other use of the same names. Other commonly used packages use the std:: namespace. However, if you feel confident that you won't make a mistake by using a name that the language also used, writing std:: quickly becomes annoying; the command using namespace std; says that this prefix can be dropped, it says that the std namespace is being used and so the prefix isn't needed to tell the compiler where the name is. Without this command the program would look like this, with extra std::'s in line 1.3.8.
1 #include<iostream> 2 3 4 5 int main() 6 { 7 8 std::cout << "Of man's first disobedience" << std::endl; 9 10 //it is good practice to include the next line but most compilers won't mind if you leave it out. 11 return 0; 12 13 }
Next, line 1.1.5 has the main statement, the statements inside main are what are actually run, as you can see line 1.1.6 has a left brace, {, which is closed by a right brace, } on line 1.1.12. It is good practice, though not essential, to indent stuff enclosed in braces, it makes it easier to read the code. Emacs will indent automatically. Again, we won't worry too much about the form the main statement takes, it has a return type int which passes a value back to the computer when the program runs, in programs that interact with the computer in a sophisticated way this can be used to tell the computer whether or not the program has run successfully or not by passing back different values, here we just pass back the value 0, this is done on line 1.1.11. Again, this is something we will talk more about again and, as noted in line 1.3.10 you won't actually get an error if you leave this out. line 1.3.10 is a comment, the compiler ignores any line starting with //.
The actual printing is done in line 1.1.8. This is a statement and so it ends in a semicolon, ;, return 0; in line 1.1.10 and using namespace std; in line 1.1.3 also end with semicolons. cout is a stream, this can be thought of as a pipe leading from the program to the screen, the stream operator << adds stuff to the pipe, in this case it adds "Of man's first disobedience" and so that gets printed on the screen. The endl is also added, endl is short for end line and endl causes a carriage return. It does slightly more than that however, when a program is running it often delays output and input until it is convenient: the output is stored up "in the pipe" until there is a good moment for flushing out the pipe by actually outputting on the screen. This can be inconvenient and another effect of an endl is to flush the stream, that is print out everything that has been stored up for printing. Here it is not needed since the program finishes a line later and the stream is always flushed at the end, but it is something of a habit to flush screen output as it is made. flush also flushes the stream but without a carriage return. A \n in a string gives a carriage return without flushing, so the following is functionally identical to the HelloWorld.cpp program 1.1 despite the change to line 1.4.9. The backslash in \n is the notation used for what is called an escaped character, one that can't be typed. It should be also be noted that strictly speaking \n is a line feed and \r is a carriage return; basically \n finishes the line and returns the cursor to the start of the next line. Lines 1.4.2/3 also illustrate another way to do comments, anything appearing between a /* and */ is ignored.
1 #include<iostream> 2 /* this is another way 3 to do comments*/ 4 using namespace std; 5 6 int main() 7 { 8 9 cout << "Of man's first disobedience\n"<< flush; 10 11 return 0; 12 13 }
Summary 1
1 #include<iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 8 SOME STATEMENTS 9 10 return 0; 11 12 }