C ++ primer version 4 notes (1)

Source: Internet
Author: User
Tags bitwise operators

Part 1 BASIC Language


[Chapter 2 variables and basic types]

C ++ built-in types include bool, Char, wchar_t, short, Int, long int, float, double, long double, the C ++ standard defines the minimum number of digits that each built-in type should occupy. The number of digits that each compiler determines depends on. In VC ++ 2008, the number of bytes occupied by each type is: 1, 1, 2, 2, 4, 4, 4, 8, and 8.

When the number that exceeds the range is assigned to the unsigned type, the result is the modulo between the number and the maximum number of the unsigned type variable, such as unsigned char a = 336; the actual value of A is 80. The actual value of unsigned char B =-1; A is 255. Add L before the character constant to convert the constant to the wchar_t type, such as l'a '. You can also add L before a String constant to convert it to a wide String constant. In C ++, initialization and assignment are two different operations. Initialization refers to creating a variable and assigning it an initial value. The assignment is to erase the current value of the variable and replace it with a new value. Whether the built-in type variables are initialized depends on the location where the variables are defined. All variables defined in the function in vitro are initialized to 0. The variables defined in the function body are not automatically initialized, and their values are undefined. The default value of a non-const variable is extern. It can be declared in other files using extern. However, the const variable is not extern by default. To use it in other files, except for extern declaration in other files, the const variable also needs extern for definition. Non-const references cannot reference const variables. const references can reference non-const variables. Non-const references can only reference this type of variables and cannot be initialized using literal constants or arithmetic expressions; the const reference can reference related type variables and can be initialized using literal constants and arithmetic expressions. Only enumeration members of this type or enumeration variables of this type can be used for initialization or assignment of enumeration objects. [Chapter 1 Standard library type]String is the standard library type of C ++, which is more abstract and advanced than the built-in type. String can store variable-length strings. the user does not need to be responsible for memory management. cin> s; leading and trailing spaces are omitted. s only stores continuous non-empty characters; string contains the matching type string: size_type, which is used to indicate the character length and Character index type. It is of the unsigned type (unsigned int or unsigned long ). String size () function returns the length and character subscript index. It is best to use a string: size_type variable. [Chapter 2 arrays and pointers]Modern C ++ should try to use vector and iterator types, and avoid low-level arrays and pointers. You cannot define referenced arrays or referenced variables. The dimension value of the array should be determined at compilation. Therefore, the dimension of the array can only be defined using constant expressions, this includes an integer literal value constant, an enumeration constant, and an integer const variable initialized using a constant expression. The subscript type of the array is size_t, And the subscript type of the vector is vector: size_type. The initialization and value assignment of pointers can be an integer constant expression with 0 values (for example, a const constant variable with 0 and a value of 0), but not a non-zero value or a common variable. Const double * cptr1; // cptr1 is a pointer to the const double type. You cannot change the value of the variable through cptr1, but the value of cptr1 can be changed to point to other variables; double * const cptr2; // cptr2 is a constant pointer to the double type. You can change the value of the variable through cptr2, but the value of cptr2 itself cannot be changed and cannot be pointed to other variables; const double * const cptr3; // cptr3 is a constant pointer to the const double type. The value of cptr3 and the value of the variable to which it points cannot be changed. The address of a const object cannot be assigned a pointer to a non-const object. The address of a non-const object can be assigned a pointer to a const object. In this case, the value of this object cannot be changed through this pointer, however, because the object itself is non-const, the object value can still be changed by pointing to the non-const pointer of the object or directly assigning values to the object. The pointer to the const object is usually used as a function parameter to ensure that the pointer value inside the function does not change its value to the object. Typedef string * pstring; const pstring cstr; // The equivalent form is not const string * str; it is string * const str; const pstring cstr; In this sentence, const modifies pstring, pstring is the string * pointer type, so the const in the equivalent form should be the pointer. The dimension of the array should be determined at compilation, while the dimension of the dynamically allocated array with malloc and new can be determined during the program running. The dimension can be any complex expression. When an array is dynamically allocated, if the array element type is a class type, the default constructor of this class is used for initialization. If the array element type is a built-in type, Initialization is not performed. You can explicitly Initialize an array with parentheses after it is dynamically allocated: int * p = new int [10] (); initialize each element to an integer with the default value of 0. This method can be used for initialization. It can only be initialized to the default value of the array type. You cannot specify the value in the final brackets as the array initialization, even if the default type of int is 0. If the dynamically allocated array element is of the const type, it must be initialized during definition: const int * p = new const int [10] (); while the value of the const type cannot be changed, therefore, the definition of this method is not very useful. An array with the defined length of 0 is not allowed, but when the new array is dynamically allocated, the parameter 0 is accepted as the array length, and a valid Non-zero pointer is returned, this pointer is different from the pointer returned by other new parameters. It cannot be used for unreferencing, but can be used for comparison. The c_str () function of string returns a C-style array of the const char type. Therefore, the accepted variable should also be of the const type: string s = "abc "; const char * str = s. c_str (); [Chapter 1 expressions]The operand of the remainder (Modulo) operator % can only be an integer, including bool, char, short, int, long, and the corresponding unsigned type. When both operands are integers: if both operands are positive, the result of // and % is positive. If both operands are negative, the result of/is positive and the result of % is negative; if only one operand is negative, the result of/is negative, but the specific value is 0 or negative infinity, it depends on the specific machine, % result symbols and values depend on specific machines. & | Operator. The second operand is calculated only when the first operand cannot determine the value of the entire expression. The operands of bitwise operators can only be integers, including signed and unsigned. The bitwise operation of signed integers depends on the specific machine. The right operand of the shift operator (left shift and right shift) cannot be a negative number, and must be strictly less than the number of digits of the left operand. Otherwise, the operation result is undefined. The value of the value assignment expression is the value of its left operand, and the result type is the left operand type. The value assignment operation has the right combination. Therefore, you can use the concatenation assignment operation: string s1, s2; s1 = s2 = "OK"; the result of a comma expression is the value of its rightmost expression. When delete is used to delete a space not allocated with new, the operation is invalid. compilation may pass, but an error is returned. For example: int a = 3; int * pa = & a; delete pa; but if the pointer value is 0, delete is legal and no error is reported, but it makes no sense to do so. After deleting a space pointed to by a pointer using delete, it is best to set the pointer value to 0 to avoid accidental reference of this hanging pointer. Pointers pointing to any data type can be converted to void * type, and integer constant 0 can be converted to any pointer type. C ++ automatically converts an enumerated object or enumeration member to an integer. The conversion result can be used wherever an integer is required (except for initialization and assignment of non-matching enumerated objects ). Except &, |, and?: Operator. C ++ does not define the calculation sequence of the operands of other operators. The order in which the operands are solved is determined by the compiler. For example, if (ia [index ++] <ia [index]), <The calculation order of the operands on both sides affects the final comparison result. if the left operand is calculated first, the result is equivalent to if (ia [index] <ia [index + 1]). if the right operand is calculated, the result is equivalent to if (ia [index] <ia [index]). Therefore, if a variable appears multiple times in an expression and the value of the variable is changed in a subexpression, it is best to split multiple expressions, avoid Ambiguity Caused by computational order. For example, if you want if (ia [index ++]
<Ia [index]) calculates the left value first, and then changes it to if (ia [index] <ia [index + 1])... ++ index; [Chapter 1 statements]The Type Used for switch determination can only be an integer value, including bool, char, short, int, long, and the corresponding unsigned type. Multiple objects (variables) can be defined in the first part of the for statement header, but because only one statement can appear here, all objects (variables) must be of the same type. The goto statement and the jump label should be in the same function. It is not recommended to use the goto statement. All programs that use the goto statement can be rewritten to programs that do not use the goto statement. The process of searching for the code that processes the message corresponding to throw is opposite to that of calling the function. First, find the matching catch clause in the current function. If there is no catch clause, return to the function that calls this function. This process is carried out. If there is no catch clause to handle this exception, the system calls the standard library function terminate. If the Exception Code is not placed in the try statement block, the terminate function is called directly when an exception occurs. The behavior of the terminate function depends on the system. Generally, the execution of the terminate function will cause the program to exit abnormally. The assert macro can be used to help program debugging. Assert (expr); When expr is true, continue to run forward; otherwise, the program stops running. This macro is only valid in the debug version. In the release version, this statement does not work. [Chapter 1 Functions]In earlier C ++ versions, the return value of a function that does not explicitly define the return value type is int by default, but in the new C ++ version, the return value type of a function must be explicitly defined. If the parameter is not referenced, the value of the real parameter is copied. If the parameter is referenced, the parameter is only the alias of the real parameter. By defining the form parameter as the reference type, you can change the value of the real parameter to return the internal value of the function. You can also avoid copying the real parameter value. Otherwise, copying large real parameter objects will consume a lot of resources. If the parameter is a reference type and the function does not modify the value of the referenced variable, it is best to define this type as a const reference. Because the non-const reference type requires that the real parameter be a non-const object of the same type (short cannot be assigned to Int &), furthermore, literal constants, const objects, and expressions that generate the right value cannot be real parameters. To change the value of a pointer in a function, you can define a reference to a pointer, for example, void swap (int * & P1, int * & p2 ). When an array parameter is of a non-reference type, the compiler only checks whether the real parameter is of the pointer or pointer type. When the real parameter is an array, the real parameter array is automatically converted to a pointer pointing to the first element of the array. The form parameter receives a copy of the pointer and can modify the pointer value within the function to access the array element. When an array parameter is of the reference type, the compiler will not convert the array arguments to pointers, but pass the array references themselves. In this case, the array size becomes part of the actual parameters, the parameter can only accept an array of real parameters of the corresponding size. The Compiler checks whether the size of the real parameters of the array matches the size of the parameter. For example, void printvalues (INT (& ARR) [10]) {} indicates that the real parameter can only be an array containing 10 elements. If the main function does not write a Return Statement (such as return 0;), the compiler will implicitly add a statement that returns 0. Do not return a pointer or reference pointing to a local object in the function. When the function returns, the local object is destroyed, and the pointer or reference pointing to an uncertain memory unit. You should have a function declaration before calling a function. A function can be defined only once, but can be declared multiple times. A good habit is: declare a function in the header file, include the header file in the source file, and implement the function. This ensures that the declaration of the function is consistent. When the function interface is changed, you only need to change one declaration in the header file. The default real parameters of the function should be placed at the end of the form parameter list. A good habit is to rank the least used default real parameters before the form parameter, and the most likely to use the default real parameters behind the form parameter list. By default, real parameters can be any appropriate type of expressions (including function calls ). By default, real parameters can be specified in function declaration or function definition. However, a file can only specify the default parameter once for one parameter. If the default real parameter is specified during function definition, the default real parameter can only be used in the source file containing the function definition. Therefore, a good habit is to specify the default real parameter when declaring a function. The Inline mechanism is suitable for functions with small optimizations, few rows, and often called. Most compilers do not support inline recursive functions. Inline functions should be defined in the header file. The compiler implicitly treats a member function defined in the class body as an inline function. The main function cannot be overloaded. If the parameters are of the non-reference (or pointer type) type, C ++ ignores the const parameter modifier and considers that the const modifier and the const modifier are the same function. If the parameters are of the reference (or pointer type) type, two different functions are involved: const modifier and const modifier. The C ++ name search takes place before the type check. If a number of overloaded functions are declared in the outer scope, and a function with the same name is declared in the internal scope, the function declared in the inner scope will overwrite the function declared in the outer scope, instead of overloading the scope declaration function at the external layer! Even if a variable with the same name as the outer-scope function is defined in the inner-layer scope, this variable will overwrite the function declared in the outer-scope function. The overload function should be declared in the same scope. The same name function in different scopes implements the coverage effect. If there are overload functions, the compiler determines which function to call is called function matching. There are three steps: 1. Determine the candidate function: determine the function with the same name as the called function; 2. Select a feasible function. Two conditions are met: match the number of actual parameters, match the actual parameter type (exact match or implicit conversion), and search for the best match. Matching precedence is arranged from high to low: exact matching, matching through type escalation, matching through standard conversion, matching through class type conversion. Function pointers only accept initialization or assignment of three types of values: Functions of the same type, function pointers, and constant expressions with 0 values. Pointers pointing to different function types cannot be converted to each other. Returns the pointer to the function: int (* ff (INT) (int *, INT); the function name is ff, and Its Parameter type is int. A function pointer is returned, its type is int (*) (int *, INT ). [Chapter 1 Standard IO library] IO types are defined in three independent files: iostream, fstream, and sstream, which correspond to standard input/output streams, file streams, and string streams respectively. C ++ defines the wide character type wchar_t, and the c ++ IO Library also provides support for the wide character type. Before the corresponding char type processing version, add the letter "w ", for example, wostream, mongoream, wiostream, wifstream, wofstream, wfstream, wistringstream, wostringstream, wstringstream, and wcin, wcout, and wcerr are defined. The standard library type does not support Value assignment and copy operations. To pass or return IO objects, you can only use references or pointers. Each IO object corresponds to a buffer. The corresponding buffer is refreshed in the following situations: 1. The program ends normally. 2. the buffer zone is full. 3. Use the endl, ends, flush, unitbuf, and other operators to display and refresh the buffer. 4. Associate the output stream with the input stream so that the associated output buffer is refreshed when the input stream is read. If the program ends abnormally, the output buffer will not be automatically refreshed! By default, cout and cin are bound to the standard library. Once the fstream object is opened, it will be associated with the specified file. To associate this object with another file, you should first close the existing association using the close () method, and then use open () method to associate with a new file.

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.