|
กก |
Probably the best way to start learning a programming language is with a program. So here it is our first program:
The left side shows the source code for our first program, which we can assign any filename, for example hiworld.cpp. The right side shows the result of the program once compiled and executed. The way to compile a program depends on the compiler you are using. Depending on if it has a Development Interface or not and on its version. Consult the section compilers and the manual or help included with your compiler if you have doubts on how to compile a C++ program. The previous program is the first program that most programming apprentices write for the first time, and its result is the printing on screen of the "Hello World" sentence. It is one of the simpler programs that can be written in C++, but it already includes the basic components that every C++ program has. Take a look at them one by one:
Therefore, you may notice 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 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 <<) that were included within key-bracket signs ({}) in the main function. The program has been structured in different lines for that it become more legible, but it is not compulsory to do so. For example, instead of
we could have written:
main () { cout << " Hello World "; return 0; }with exactly the same meaning. In C++ the separation between instructions is specified with an ending semicolon (;). The division of code in different lines only serves for that it can become more legible and schematic for the humans that read it. Here is a program with more instructions:
In this case we used the cout << method twice in two different instructions. Once again, the separation in different lines of the code has just been done to give greater readability to the program, since main could perfectly have been defined thus: main () { cout << " Hello World! "; cout << " I'm to C++ program "; return 0; }Also we are free to divide the code in more lines if we consider it convenient:
And the result would be exactly the same one than in the previous examples.
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 that finaly do not produce any code. These must be specified in their own line and do not require to include a semicolon (;) at the end.
Comments.Comments are pieces of source code discarded from the code by the compiler. They only serve for that the programmer can put notes or descriptions on parts of a program.C++ supports two ways to mark comments: // line commentFirst of them: the line comment, discards the code from the pair of slashes (//) until the end of the same line. The second one, the block comment, starts at the first /* characters and ends when the first */ characters are reached, with the possibility to include several lines. We are going to add comments to our second program:
If you include comments within the sourcecode of your programs without using the comment characters combinations //, /* nor */, the compiler will take them as if they were C++ instructions and, more likely, it will show up one or several error messages.
Additional readings: | |||||||||||||||