In the C ++ programming example, we can see all the code of the program starting from the main function to the end. However, in Visual C ++, MFC encapsulates some classes and hides some code, therefore, we cannot see all the code of the source program. For example, we cannot find the main function from all the source files of the project.
Simple C ++ program // Program into 1.cpp # include void main () {cout <"Let's learn to write a C ++ program. "; cin. get ();} This program consists of 6 lines, 1) 1st behavior comments. If each line of the program contains a symbol "//", all its right symbols are comments. Annotation is a description to help read the program. It has nothing to do with the program running.
When a program is compiled, the comment is treated as a space. This line indicates that this program is stored in the file "program release 1.cpp. 2) The second line # include is a compilation preprocessing instruction, which tells the compilation system to put the header file iostream provided by the system during compiling the translation program. the content of h is inserted to the second row. Its role in the program is related to the output Statement of Row 3. A c ++ programming instance is composed of a main function. Here, main is the name of the main function, and the first void indicates that the function has no return value. Brackets () indicate the function. The parameter table in the brackets is the function parameter table, but this function does not have any parameters. Therefore, void indicates that it has the same effect as empty brackets. 4th ~ The six rows are called function bodies and are enclosed. A function can contain any number of rows.
Row 5th is the only task to be executed in this program: output (Display) a string to the screen. Cout is a standard output file name, which indicates the screen. The symbol "<" is an operator that instructs the computer to transport its right side to a cout file, that is, a screen, with a double-enclosed string. Both cout and <are in the header file iostream provided by the system. h. Therefore, when the program needs to use standard input and output functions such as cout and <, the command # include included in the second line must be listed.
Cin. get () indicates to press any key to continue. 5) The execution result of this Program is displayed on the screen: Let's learn to write a C ++ Program. ------------------------------------- 1.1.2 calculate the sum of two numbers: // program limit 2.cpp # include void main () {int a, B, sum; // defines three Integer Variables a = 43; // assign an integer constant to the variable a B = 37; sum = a + B; // sum cout <"The sum is" <void main () {const float pai = 3.14; // const is used to describe a float-type constant pai. m is a constant, so the value of float radius will not be changed in the program. // It indicates a float-type variable radius, used to store the cout radius value <"E Nter radius: "; cin> radius; // The file indicated in iosream. h is used to receive keyboard input.
In this case, input a number of 2.5 from the keyboard and send the input value to the variable radius. float area = pai * radius; // describes The C ++ programming instance, used to store The calculated area cout <"\ n The area of circle is :"; cout <void main () {float t, tc, tf; char corf; const float fac = 1.8, inc = 32; cout <"Enter temperature :"; cin> t; // enter the temperature value cin> corf; // It indicates that the temperature is between us (C) or US (F) if (corf = 'C' | corf = 'C') {tc = t; tf = t * fac + inc ;} else if (corf = 'F' | corf = 'F') {tf = t; tc = (t-inc)/fac ;} else tc = tf= 0.0; c Out <void main () {int x, y; char op; cout <"first integer:"; cin> x; cout <void main () {int x, y, quit = 1; char op, cont; while (quit) // The whiel loop statement, which contains an expression, called a control expression.
- How to better set Dev C ++
- Analysis on the use and learning of C ++ Compiler
- Brief description of struct ideology in C ++
- C ++ programming language Overview
- Measure the test taker's knowledge about the differences between C ++, C #, and Java.
When the value of the {// expression in this table is 1, the code in the body continues to be recycled. Start from the braces and enter the loop body. Cin> x; cin> y; cin> op; switch (op) {case '+': cout <> cont; if (cont = 'n ') quit = 0; // when the operator inputs the character 'n', The quit value is 0. // When the while expression is detected again, the while loop is exited because the quit value is 0. } The improvements made by this program are obvious, as long as after each computation is completed, the user can continue running by pressing the 'y' key (or any character other than 'n.
The four arithmetic operations specified by the user are completed until the letter 'N' is received. In the loop body, you must have a statement that changes the value of the while expression. Otherwise, the statement cannot jump out once it enters the endless loop. This is called an "endless loop", which should be avoided in programming.