C ++ Primer: Chapter 1 and Chapter 2

Source: Internet
Author: User

I have read it again in the past, but I didn't understand it in detail at the time. I will read it here to check for missing content and review the basics. Record the details. 1. Variable initialization: Assign the initial value to the variable when defining the variable, unless it is determined that the variable will overwrite the initial value before being used for other intentions. If you cannot reset the variable before reading the variable, initialize the variable. The initialization of the variable is as follows: [cpp] int val1 = 0; // initialize int val2; // partial functions of the compiler are not initialized. 2: error Syntax Error Type Error declaration error 3. Input File Terminator from the keyboard OS using different values as file Terminator. In Windows, the file Terminator is input by entering Ctl + z, which is usually Ctl + d in Unix. 4. Integer type: indicates the arithmetic type of integers, characters, and boolean values. 5. In C ++, it is legal to assign a negative value to an unsigned object. The result is the value after the negative number is calculated for the number of values of this type, if-1 is assigned to an 8-bit unsigned char, the result is 255. 6. literal cinstant integer nominal value: It can be expressed in decimal, octal, and hexadecimal notation in three minutes ). It must start with 0 in octal format and start with 0x or 0X in hexadecimal format. You can also add a suffix after it to forcibly convert a nominal value constant, for example, L or l, representing the constant long, U or u, representing the unsigned type. For example, 128u/* unsigned */8lu/* unsigned long */floating point nominal value: generally expressed in decimal or scientific notation, and science and technology are expressed in E or e. Then, F or f is added to indicate the single precision, and L or l is used to represent the extended precision. For example: 3.1232F 3.232323E0f Boolean value and character nominal value: Non-printable character Transfer sequence 7, variable 1. What is a variable: The variable provides a named storage area that the program can operate on. Every variable in C ++ has a specific type, this type determines the memory size and layout of the variable, the value range that can be stored with the value in the memory, and the operation set that can be applied to the variable. 2. C ++ keyword 8. variable naming conventions variable names generally Use lowercase letters. For example, if the name is usually written as index rather than Index or INDEX identifier, you should use an identifier that can help the Group remember to contain multiple words to write an underline between each word, or the first letter of each embedded word is capitalized 9. There are two initialization methods for variable initialization: copy-initialization and direct-initialization ). Int ival (1024); // direct-initialization int ival = 1024; // copy-initialization is not a value assignment. initialization refers to creating a variable and assigning it an initial value, the value assignment replaces the current value of the distinct object with the new value. 10. Variable initialization Rules 1. Whether the built-in type variables are automatically initialized depends on the location of the variable definition. Variables defined in the function in vitro are initialized to 0, and built-in type variables defined in the function body are not automatically initialized. In addition to the operands used as the value assignment operator, uninitialized variables may cause errors that are hard to be found. Therefore, we recommend that you initialize each built-in object, which is easier and safer, though not required. 2. Declaration and definition of variables: used to allocate storage space for variables, you can also specify the initial value for variables. In a program, variables have only one definition ,. Declaration: the type and name of the variable used to surface the program. Definition is also declaration. when defining a variable, we declare its type and name. You can declare a variable name without defining it by using the extern keyword. The undefined Declaration includes the object name, object type, and the keyword "extern" extern int I before the object type; // declares but does not define I; int I; // declares and define I can be declared multiple times, but only once. The declaration can be initialized only when the Declaration is also a definition, because only the definition allocates storage space. The initialization type must have storage space for initialization. 11. const qualifier 1. Magic number: its meaning is not reflected in the context. 2. the const qualifier converts an object into a constant const int bufSize = 512; // The bufSize cannot be modified because the constant cannot be modified after definition, so it must be initialized during definition. 3. the const object is the local variable of the file by default. When a non-const variable is defined in the global scope, it can be accessed throughout the program. Define a non-const variable in one file and use this variable in another file. [Cpp] // file_1.cpp int counter; // definition // file_2.cpp extern int counter; // uses counter from file_1 ++ counter; // add counter defined in file_1 the const variable declared in the global scope is a local variable defined in the object's file. This variable only exists in that file and cannot be accessed by other files. By specifying the const variable as extern, you can access the const object in the entire program: [cpp] // defines and initializes a const that is accessible to other files extern const int counter = 12; // definition // file_2.cpp extern const int counter; // uses counter from file_1 // uses counter defined file_1 for (int index = 0; index! = Counter; ++ index) {//} non-const variables are considered as extern by default. To enable the const variable to be accessible in other files, you must specify extern as the display. 12. reference is another name of the object. In actual programs, references are mainly used as form parameters of functions. A reference is a composite type that is defined by adding the "&" symbol before the variable name. A composite type is a type defined by another type. [Cpp] int ival = 1024; int & refVal = ival; // OK int & refVal2; // error: a refernce must be initialized int & refVal3 = 10; // error: initializer must be an object reference is an alias: After the reference is initialized, it is bound to the specified object during initialization as long as the reference exists. It is impossible to bind a reference to another object. Define multiple references: multiple references can be defined in a type definition line. The "&" symbol must be added before each referenced identifier. Const reference: a reference to a const object. It can be read but cannot be modified. A non-const reference can only be bound to objects of the same type as the reference. A const reference can be bound to objects of different but related types or to the right value. 13. typedef name: used to define synonyms of types. There are three purposes: to hide the implementation of a specific type, emphasize the purpose of using the type to simplify the complex definition of the type, so that it is easier to understand that one type is allowed for multiple purposes, at the same time, the purpose of each use of this type is clear 14. enumeration: defines the integer constant set and aggregates them into groups. 1. Definition and initialization: The enumeration definition includes the enum keyword, followed by an enumeration type name. By default, the value of the first enumeration member is 0, and each of the following values is greater than the previous value. [cpp] // input is 0, output is 1, and append is 2 enum open_modes {input, output, append}; 2. enumeration members are constants: the value used to initialize enumeration members must be a constant expression. [cpp] // shape is 1, sphere is 2, cylinder is 3, polygon is 4 enum Forms {shape = 1, sphere, cylinder, polygon}; in Forms, the shape value is 1, and other values are implicitly initialized to 2, 3, 4 3. each enum defines a unique type. 15. header file: the header file is used for declaration, not for definition. (The header file is included in multiple source files, so it should not contain the definition of variables or functions, there are three exceptions: header files can define classes, const objects that are known during compilation, and inline letters. Number) Some const objects are defined in header files. 16. A brief introduction to the processor 1. other header files are often required. avoid Multiple inclusion: The Preprocessor variable name must be unique in the program. Any name used that matches the Preprocessor variable is associated with the Preprocessor variable. Preprocessor variables are in either of the following States: defined or undefined. You can use the following code to prevent multiple times: [cpp] # ifndef _ CPLUSPLUSPRIMER_H _ # define _ CPLUSPLUSPRIMER_H __//.... # endif/_ CPLUSPLUSPRIMER_H __

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.