The structure and writing format of C + + programs are summarized below.
1) A C + + program can consist of a program unit or multiple program units. Each program unit acts as a file. At program compile time, the compiling system compiles each file separately, so a file is a compilation unit.
2) in a program unit, you can include the following sections:
- preprocessing commands. The # include command is included in the 4 programs in the previous section.
- The global Declaration part (in the Declarations section outside the function). In this section, you include the definition of the declaration of the user's own defined data type and the variables used in the program.
- Function. A function is a part of implementing an operation, so a function is a necessary and essential part of a program. Each program must include one or more functions, which must have one (and only one) main function (the main function).
However, it is not required that each program file must have the above 3 parts, and some parts (including functions) may be missing.
3) A function consists of two parts:
- The first part of the function, which is the top row of functions. Includes the function name, function type, function property, function parameter (parameter) name, and argument type. Note: A function name must be followed by a pair of parentheses, and the function parameter can be defaulted, such as int main ().
- The body of the function, which is the part of the curly brace below the first part of the letter. If there are multiple curly braces in a function, the outermost pair {} is the range of the body of the function.
The function body generally includes:
- The partial declaration part (within the declaration portion of the function). Includes definitions for the types used in this function, declarations of functions, and variables. Note: The Declaration of data can be placed outside the function (its scope is global), or can be placed within the function (its scope is local, only valid within this function).
- Execution section. Consists of a number of execution statements, which are used to perform the operation in order to implement function functions.
4) statements consist of two types: one is the declaration statement and the other is the execution statement. C + + assigns a specific function to each statement. Statements are the basic components of implementing operations, and it is clear that functions without statements are meaningless. C + + statements must end with a semicolon.
5) A C + + program always executes from the main function, regardless of where the main function is located throughout the program.
6) class is a new and important data type added by C + +, which is the most important development for C + +. With the class, we can implement the encapsulation, information concealment, inheritance, derivation, polymorphism and other functions in object-oriented programming method. You can include data members and member functions in a class that can be specified as private (private) and common (public) properties. Private data members and member functions can only be called by member functions of this class.
7) C + + program writing format is free, a line can write several statements, a statement can be divided into multiple lines. C + + programs do not have line numbers, nor do they strictly prescribe writing formats like FORTRAN or COBOL (statements must start from a column).
8) A good, useful source program should be accompanied by the necessary annotations to increase the readability of the program. C + + also retains the form of a note in the language, which can be annotated with "/*......*/" for any part of a C + + program. The whole content between "/*" and "* *" as a comment.
When commenting with "//", the valid range is only one row, that is, the bank is valid and cannot cross the line. The valid range for commenting with "/*......*/" is multiple lines. As long as there is a "/*" At the beginning, there is a "* *" at the end of the last line. Therefore, the general habit is: a simple note with less content commonly used "//", the content of a long common "/*......*/".
The composing and writing form of C + + program