Programs are a sequence of instructions or statements. These statements form the structure of a C++ program. C++ program structure is divided into various sections, namely, headers, class definition, member functions definitions and main function.
Probably the best way to start learning a programming language is with a program. So here is our first program:
1 2 3 4 5 6 7 8 9 10 11 | // my first program in C++ #include <iostream.h> int main () { cout << "Hello World!"; return 0; } |
Output:
1 2 3 | Hello World! |
The above first block shows the source code for our first program, which we can name, for example, hiworld.cpp. The second block shows the result of the program once compiled and executed. The way to edit and compile a program depends on the compiler you are using.
The printing on screen of the “Hello World!” sentence is one of the simpler programs that can be written in C++, but it already includes the basic components that every C++ program has. We are going to take a look at them one by one:
// my first program in C++
This is a comment line. All the lines beginning with two slash signs (//) are considered comments and do not have any effect on the behavior of the program. They can be used by the programmer to include short explanations or observations within the source itself. In this case, the line is a brief description of what our program does.
#include <iostream.h>
Sentences that begin with a pound sign (#) are directives for the preprocessor. They are not executable code lines but indications for the compiler. In this case the sentence #include <iostream.h> tells the compiler’s preprocessor to include the iostream standard header file. This specific file includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is used later in the program.
int main ()
- This line corresponds to the beginning of the main function declaration. The main function is the point where all C++ programs begin their execution. It is independent of whether it is at the beginning, at the end or in the middle of the code – its content is always the first to be executed when a program starts. In addition, for that same reason, it is essential that all C++ programs have a main function.main is followed by a pair of parenthesis () because it is a function. In C++ all functions are followed by a pair of parenthesis () that, optionally, can include arguments within them. The content of the main function immediately follows its formal declaration and it is enclosed between curly brackets ({}), as in our example.
cout << "Hello World";
- This instruction does the most important thing in this program. cout is the standard output stream in C++ (usually the screen), and the full sentence inserts a sequence of characters (in this case “Hello World”) into this output stream (the screen). cout is declared in the iostream.h header file, so in order to be able to use it that file must be included.Notice that the sentence ends with a semicolon character (;). This character signifies the end of the instruction and must be included after every instruction in any C++ program (one of the most common errors of C++ programmers is indeed to forget to include a semicolon ; at the end of each instruction).
return 0;
- The return instruction causes the main() function finish and return the code that the instruction is followed by, in this case 0. This it is most usual way to terminate a program that has not found any errors during its execution. As you will see in coming examples, all C++ programs end with a sentence similar to this.
- Therefore, you may have noticed that not all the lines of this program did an action. There were lines containing only comments (those beginning by //), lines with instructions for the compiler’s preprocessor (those beginning by #), then there were lines that initiated the declaration of a function (in this case, the main function) and, finally lines with instructions (like the call to cout <<), these last ones were all included within the block delimited by the curly brackets ({}) of the main function.
The program has been structured in different lines in order to be more readable, but it is not compulsory to do so. For example, instead of
1 2 3 4 5 6 7 | int main () { cout << " Hello World "; return 0; } |
we could have written:
1 2 3 | int main () { cout << " Hello World "; return 0; } |
in just one line and this would have had exactly the same meaning.
In C++ the separation between instructions is specified with an ending semicolon (;) after each one. The division of code in different lines serves only to make it more legible and schematic for humans that may read it.
Here is a program with some more instructions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <iostream> using namespace std; int main() { int firstNumber, secondNumber, sumOfTwoNumbers; cout << "Enter two integers: "; cin >> firstNumber >> secondNumber; // sum of two numbers in stored in variable sumOfTwoNumbers sumOfTwoNumbers = firstNumber + secondNumber; // Prints sum cout << firstNumber << " + " << secondNumber << " = " << sumOfTwoNumbers; return 0; } |
Output:
1 2 3 4 5 | Enter two integers: 4 5 4 + 5 = 9 |
Preprocessor directives (those that begin by #) are out of this rule since they are not true instructions. They are lines read and discarded by the preprocessor and do not produce any code. These must be specified in their own line and do not require the include a semicolon (;) at the end.
Comments in C++
Comments are pieces of source code discarded from the code by the compiler. They do nothing. Their purpose is only to allow the programmer to insert notes or descriptions embedded within the source code.
C++ supports two ways to insert comments:
// line comment
/* block comment */
The first of them, the line comment, discards everything from where the pair of slash signs (//) is found up to the end of that same line. The second one, the block comment, discards everything between the /* characters and the next appearance of the */ characters, with the possibility of including several lines.
1 2 3 4 5 6 7 8 9 10 11 12 13 | /* my second program in C++ with more comments */ #include <iostream.h> int main () { cout << "Hello World! "; // says Hello World! cout << "I'm a C++ program"; // says I'm a C++ program return 0; } |
Output:
1 2 3 | Hello World! I'm a C++ program |
If you include comments within the sourcecode of your programs without using the comment characters combinations //, /* or */, the compiler will take them as if they were C++ instructions and, most likely causing one or several error messages.
Source:
http://www.cplusplus.com/doc/tutorial/