Next we will introduce a very simpleC ++++ProgramTo understand the composition of the C ++ program. Now, the reader does not need to enter the code.Create a program. All the details are not described here, as they will be discussed in later chapters. See Figure 1-2.
Figure 1-2
The program shown in Figure 1-2 displays the following message:
Thebestplacetostartisatthebeginning
The program contains a function main (). The first line of the function statement is:
Intmain ()
A function is a self-contained block of the Code, represented by a name. In this example, It is main. There can be many other code in the program, but each C ++ program must contain at least the main () function and only one main () function. The execution of the C ++ program always starts from the first statement in main.
The main () function contains two executable statements:
Cout <"Thebestplacetostartisatthebeginning ";
Return0;
These statements are executed in order. Execute the first sentence first. In general, the statements in a function are always executed in order, unless one statement changes the execution order. Chapter 1 describes the types of statements that can change the execution sequence.
In C ++, input and output are executed using streams. If you want to output a message, you can put the message in the output stream. If you want to input a message, put it in the input stream. In C ++, the standard output stream and input stream are called cout and cin. They use the computer screen and keyboard respectively.
The above Code uses the insert operator <put the string "Thebestplacetostartisatthebeginning" in the output stream to output it to the screen. When writing a program involving input, use the extraction operator>.
The name cout is defined in the header file iostream. This is a standard header file that provides the definitions required to use the standard input and output functions in C ++. If the program does not contain the following code lines:
# Include <iostream>
So it won't be compiled, because the iostream header file contains the definition of cout. Without it, the compiler doesn't know what cout is.
Tip:
There is no space between the angle brackets and the standard header file name. In many compilers, spaces between two angle brackets <and> are very important. If spaces are inserted here, the program may not compile.
The second statement in the function body is also the last statement:
Return0;
After the program is completed, the control is returned to the operating system. It also returns the value 0 to the operating system. You can also return other values to indicate different end conditions of the program. The operating system can also use this value to determine whether the program is successfully executed. However, whether the program can be executed depends on the operating system.