C++test

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators naming convention

Start the C + + tour

As the old saying goes, "make haste", whether it is building or learning knowledge, it is very important to lay a solid foundation. Without knowledge of the foundation, skyscrapers can not give gifts, learning C + + babyfaced need to grasp the basic knowledge and fundamental structure, easy to grasp the overall situation, the following from the C + + building of the basic framework and brick by brick.

Structure of the 2.1c++ program
   /*文件名:example201.cpp功  能:两数相乘*/#include <iostream>                                 //编译预处理int main()                                          //主函数{    using namespace std;                            //名称空间编译指令    /*    声明三个代表整数的符号,num1和num2为乘数和被乘数,    resultNum为乘积,它们的初值都为0    */    int num1=0, num2=0, resultNum=0;     cout<<"请输入要相乘的两个整数,用空格键分开:";    //输出提示语句    cin>>num1>>num2;                                //接收用户输入    resultNum=num1 * num2;                          //乘法运算    cout<<"计算结果为"<<resultNum;                   //输入计算结果    cout<<endl;                                     //输出一个空行     return 0;                                       //main()函数返回}
2.1.1 Notes
    • C + + supports two styles of annotations:

    • C + + style: Starts with//, the scope is one line

    • C style: Starts with '/', ends with'/', the contents of two symbols are commented out, so they can span multiple lines. When used, be aware of the correct pairing of/ and/and C-style annotations do not support nesting.

    • Annotations are the instructions that programmers provide to readers and are a means of improving the readability of a program. Annotations are intended for human-readable use only and are optional parts of the program, and the C + + compiler ignores all comments and treats them as whitespace.

Hint: should develop in the code at the same time write comments at the good habit, the more complex the program, the more necessary to write comments, comments not only convenient for others to read the code, but also help the programmer's summary, review and review.

2.1.2 compilation preprocessing and old and new standards
    • Lines beginning with the sign #, called compilation preprocessing lines, use the # include compiler directive, which is used to add the contents of the iostream file to the program before compiling , and iostream sets the C + + input and output environment. such as CIN and cout are the C + + standard input, output device identifiers defined in iostream, and Endl are line breaks defined in iostream.

    • When using CIN and cout for input and output, the program must contain the iostream file (for the old
      compiler, the program should contain the Iostream.h file).

    • As already mentioned, the C + + language is a superset of the C language, the early C and C + + header files are in the form of. h, with the development of C + +, the standard of the header file is constantly changing. The comparison between the old and new standards, the gray area of C + + represents the pure C + + header file, the white area represents the C header file, and now, the old C file header file is reserved. h extension (this file can still be used by C + + programs), and C + + header files are stripped of the extension. Some C header files (such as X2.h in) are converted to C + + header files, the extensions are removed, and prefix C (indicated from the C language) is prepended to the file name, corresponding to the grid area (CX2) in. Older compilers may only support the old standard format, compilers that conform to the latest standards of Ansi/iso C + + support both the old standard format, or the new header file format, for pure C + + header files (such as iostream). Remove the extension. h is not just a form of change, No. h header files can use namespaces.

2.1.3 Main function
    • C + + uses a function organization process, each relatively independent process can be organized into a function, the program is generally composed of different functions hierarchically organized, therefore, the function is defined as the main body of C + +
2.1.4 Name Space
    • In, using the iostream header file, you should use the namespace compilation directive "using namespace std;" Makes the definition cout and CIN visible to the program, which is called a using-compiler directive

    • When using the source code provided by the computer software manufacturer, it is often encountered that the name of the function or the name of the variable, the compiler
      It is unrealistic to know which version to use and to rename functions and variables, and in order to solve this problem, the new Ansi/iso C + + standard introduces the feature of namespaces. Allows a vendor to encapsulate its product in a cell called a namespace, using namespaces to manage functions and variables so that the compiler can decide which version to use. If the source code provided by company A and company B has a C () function, and the respective code is defined in the namespace NamespaceA and Namespaceb, the following call method can be used to separate the two versions without error region:

       NamespaceA::C();       //调用A公司提供的C函数 NamespaceB::C();       //调用B公司提供的C函数
2.1.5 C + + morpheme
    • From a compile point of view, the smallest logical unit that makes up C + + is the word, the word is like the brick building of a house, and the words in C + + have the following categories:
    • (1) Direct constants
    • (2) string
    • (3) Keywords
    • (4) General identifiers
    • (5) operator
    • (6) Punctuation
2.2 Variables and basic types

Mathematics class We learned the concept of unknown, unknown is an identification of the solution, a different angle of thinking, unknown is also the means to store data, design C + + programs to solve problems, also need to store and identify information, it is necessary to use variables.

2.2.1 Variable
    • A variable is the symbolic name of a machine's memory location where data can be saved and accessed through symbolic names. There are 3 characteristics of variables:

    • Each variable has a name whose naming convention is the same as the generic identifier.

      • Each variable has a type.
    • Each variable holds a value. If a variable is required to hold a value, assign the value to the variable.

    • Before you can use a variable, you must first define it. The general format of the definition of a variable is as follows:

    • Data type variable name 1[= initial value 1], variable name 2[= initial value 2], ... ;

    • The content in square brackets is optional, which assigns an initial value to the variable when the variable is defined. "Data type" refers to C + + valid data types, such as int, double, long, etc., each variable belongs to a specific type, the type is an abstraction of a set of data, the data has the same range of values, operations and storage, C + + The data types in can be divided into basic data types and composite data types in two categories, as shown in 2.2.

2.3 Constants

With the previous description of the variable, it is obvious that the constants are much easier. Constants also correspond to an area of memory, which can be accessed by a constant name, with data types such as Integer, float, character, and Boolean, and the only difference is that the value of the constant cannot be changed by the program or input, which is also the origin of the constant name.

2.4 Operators and expressions
    • Operators, as the name implies, refer to symbols with operational significance, such as the addition of operators (+), minus operators (-), and so on, expression, is the C + + compiler can read the computer statement, by the operator and the operand by a certain grammatical rules, according to the operator to determine what operation of the operand, and to obtain the unique results
    • C + + provides operators in the following ways: arithmetic operators, relational operators, logical operators, bitwise operators, conditional operators, assignment operators, comma operators, sizeof operators, and other operators. At the same time, by the number of operands, the operator can be divided into a single-mesh operator (an operand), binocular operator (two operands) and three-mesh operator (3 operands).
2.5 Type Conversions

C + + has integral, floating-point, Boolean, character, and other basic types, which are described in later chapters as complex data types and user-defined types. Confusion can arise when operations are performed, especially for different types, and for this reason, C + + introduces a type conversion mechanism in which one data type can be converted to another data type.

2.6 Flow Control Statements
    • The program is composed of data structure and algorithm, the algorithm is a set of actions, and this one of the actions, is the C + + statements, statements are the most basic structure of the Unit, the program is the process of executing program statements, the order of execution of the program is called Process Control (or control process).
    • The structure theorem points out that any program logic can be represented by 3 basic structures, such as order, selection and loop, in the example code we gave earlier, the program statements are executed sequentially, is it possible to change the order of execution of the program statements? The answer is yes, as long as the use of the branch statement provided by C + + (If...else ... Statements and switch statements), loop statements (for loops and while loops), and steering control statements (continue, break, Goto, and so on).
2.7 Summary

This chapter describes the basic knowledge of the C + + language, in the C + + language, statements, identifiers, variables, constants, functions, preprocessing directives, input and output, etc., is an important concept, should be gradually mastered in practice these concepts, to be applied rationally. Describes the basic C + + operator composition (arithmetic operators, relational operators, logical operators, bitwise operators, conditional operators, assignment operators, comma operators, and other operators), their precedence and binding. This paper explains the various expressions composed of operators, explicit and implicit conversions of operands in expressions, and analyzes the process control statements of C + +, including branch structure (if ... Structure, If...else ... Structure and switch structure), loop structure (for structure, while structure and do...while structure), Process steering control statement break and continue, free-turn statement goto, etc., only a solid grasp of these most basic content, can step-by-step deep, learn C + +, Use good C + +.

C++test

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.