C ++ Primer Plus Essay (Chapter 3 Data Processing)

● C ++ variable naming rules.

    In order to store information in the computer, the program must record three basic attributes:
      ● where the information will be stored;
      ● what value to store;
      ● what type of information to store.

1. C ++ naming rules

● Only alphabetic characters, numbers and underscores (_) can be used in the name.
● The first character of the name cannot be a number.
● Distinguish between uppercase and lowercase characters.
● Ct + keywords cannot be used as names.
● Names beginning with two underscores or underscores and capital letters are reserved for implementation: (Compiler and resources used by it) _Use. Names beginning with an underscore are reserved for implementation and used as global identifiers.
● C ++ has no limit on the length of the name, all characters in the name are meaningful, but some platforms have a length limit.

● C ++ built-in integer type unsigned long, long, unsigned int, int, unsigned short, short, char, unsigned char, signed char and bool.

Integer short, int, long and long long

    If the width of each type is the same in all systems, it will be very convenient to use. For example, if short is always 16 bits, int is always 32 bits, and so on. But life is not that simple. There is no one option to meet all computer design requirements. C ++ provides a flexible standard that ensures a minimum length (borrowed from C language), as follows:
     ● short is at least 16 bits;
     ● int is at least as long as short;
     ● long is at least 32 bits, and int is as long;
     ● long long is at least 64 bits, and at least as long as long.

initialization

    Initialization combines assignment and declaration.

int n = 10;

    If you know what the initial value of the variable should be, you should initialize it. Separating variable declarations and assignments may cause unresolved problems in an instant:

short year ;
year = 1024;

C + 11 initialization method

    There is another initialization method, which is used for arrays and structures, but it can also be used for single-valued variables in C ++ 98:

int number = {24};

    There are not many cases where brace initializers are used for single-valued variables, but the C ++ 11 standard makes this situation even more. First of all, in this way, you can use the equal sign (=), or you can not use:

int emus {7};
int rheas = {12};

    Secondly, the curly braces can contain nothing. In this case, the variable will be initialized to zero:

Limit Exceeded

    The program sets a short variable (sam) and an unsigned short variable (sue) to the maximum short value, which is 32767 on our system. Then, the values ​​of these variables are increased by 1. This is no problem for sue, because the new value is still much smaller than the maximum value of the unsigned integer: but the value of sam changed from 32767 to -32768! Similarly, for sam, set it to 0 and subtract 1 , There will be no problem; but for the unsigned variable sue, set it to 0 and subtract it, it becomes 65535. It can be seen that these integer variables behave like odometers. If the limit is exceeded, its value will be the value at the other end of the range (see Figure 3.1). C ++ ensures this behavior of unsigned types: But C ++ does not guarantee that symbolic integer types will exceed the limit (. Overflow and underflow) without errors, which is the most common behavior in current implementations.

Member function cout.put ()

    What exactly is cout.put ()? Why is there a period in its name? The function cout.put () is an example of an important C ++ OOP concept member function. Classes define how to represent and control data. Member functions are grouped together and describe methods for manipulating class data. For example, class ostream has a pu () member function for outputting characters. Member functions can only be used through specific objects of the class (such as the cout object here). To use member functions with objects (such as cout), you must connect the object name and the function name (put ()) with a period. The period is called a member operator. cout.put () means that the function put () is used through the class object cout.

char literal

In C ++, there are many ways to write character constants. For regular characters (such as letters, punctuation marks, and numbers), the easiest way is to enclose the characters in single quotes. This notation represents the numeric encoding of characters. For example, the corresponding situation in the ASCII system is as follows:
      ● 'A' is 65, which is the ASCII code of the character A;
      ● 'a' is 97, which is the ASCII code of the character a;
      ● '5' is 53, which is the ASCII number 5 Code;
      ● '' is 32, which is the ASCII code of the space character;
      ● '!' Is 33, which is the ASCII code of the exclamation mark.
Insert picture description here

signed char和unsigned char

    Unlike int, char is neither unsigned nor signed by default. Whether there are symbols is determined by the C ++ implementation, so that compiler developers can maximize the matching of this type with the hardware properties. If char has a certain behavior that is very important to you, you can explicitly set the type to signed char or unsigned char:
if char is used as a numeric type, the difference between unsigned char and signed char will be very important. The representation range of unsigned char type is usually 0 ~ 255, while the representation range of signed char is -128 to 127. For example, suppose you want to use a char variable to store a value as large as 200, which may be possible on some systems, but may not be possible on other systems. But using unsigned char can achieve this purpose on any system. On the other hand, if the char variable is used to store standard ASCIr characters, it does not matter whether char is a symbol or not. In this case, char can be used.

bool type

    George Boole, who developed the mathematical representation of the law of logic. In the calculation, the value of the Boolean variable can be true or false. In the past, C ++, like C, did not have a Boolean type. As you will see in Chapters 5 and 6, C ++ interprets non-zero values ​​as true and zero as false7. However, you can now use bool types to represent true and false. True and false said. In other words, you can write statements like this:

bool is_ ready = true;

● Climits file that represents various integer system limits.

Operator sizeof and header limits

    The sizeof operator indicates that in systems using 8-bit bytes, the length of int is 4 bytes. You can use the sizeof operator on type or variable names. When using the sizeof operator on type names (such as int), put the name in parentheses: but use this operator on variable names (such as n_ short), the parentheses are optional:
Insert picture description here
Insert picture description here

● A cfloat file representing various floating-point types of system limitations.

At least as much as double-. The three types of effective digits can be as many. However, in general, float is 32 bits and double is 64 bits, at least as many as double. The three types of effective digits can be as many. However, in general, float is 32 bits, double is 64 bits, and long double is 80, 96, or 128 bits. In addition, the three types of index range is at least -37 to 37. The system limitations can be found in the header file cfloat or float.h. (cfloat is the E ++ version of the float file in the C language.) The following is the float.h = Borland E ++ Builder's some comments in the file:
Insert picture description here

● Various integer numeric literals (constant).

Integer literal

You can assign decimal numbers or hexadecimal numbers during assignment

#include <iostream>
int main()
{
	using namespace std;
	int chest = 42; // decimal integer literal
	int waist = 0x42;// hexadecimal integer literal
	int inseam = 042;// octal integer literal
	cout << "Monsieur cuts a striking figure!\n" ;
	cout << "chest =”<< chest <<”(42 in decimal) \n";
	cout << "waist =”<< waist <<”(0x42 in hex) \n";
	cout << "inseam =”<< inseam <<”(042 in octal) \n" ;
	return 0;
}

● Use const qualifiers to create symbolic constants.

    After the constant is initialized, its value is fixed, and the compiler will not allow you to modify the value of the constant. If you do this, g ++ will indicate that the program is trying to assign a value to a read-only variable. "
If you do not provide a value when you declare a constant, the value of the constant will be undefined and cannot be modified.
If you have used the C language before, you may think that the #define statement discussed earlier is enough to complete such work. But const is better than #defien. First, it can specify the type explicitly. Second, you can use C ++ scope rules to limit definitions to specific functions or files (scope rules describe how well names are known in various modules, Will be discussed in Chapter 9.) Third, you can use const for more complex; complex types, such as arrays and structures introduced in Chapter 4.

● C ++ built-in floating point types: float, double and long double.

1. Floating point type

    In fact, C and C ++ require at least 32 bits for float, at least 48 bits for double, and not less than float, and long double is at least as much as double. The three types of effective digits can be as many. However, in general, float is 32 bits, double is 64 bits, and long double is 80, 96, or 128 bits. In addition, the three types of index range is at least 37 to 37.
Previewed the ostream method setf () which will be introduced in Chapter 17. This call forces the output to use fixed-point notation to better understand the accuracy, it prevents the program from switching larger values ​​to E notation, and causes the program to display 6 decimal places.

2. The advantages and disadvantages of floating point numbers

    Compared with integers, floating-point numbers have two major advantages. First, they can represent values ​​between integers. Second, because of the scaling factors, they can represent a much larger range. On the other hand, floating-point operations are usually slower than integer operations, and accuracy will be reduced. The following program list illustrates the last point.

#include < iostream>
int main()
{
	using namespace std;
	float a = 2.34E+22f;
	float b = a + 1.0f;
	cout <<"a = "<< a<< endl;
	cout << "b - a = " << b - a << endl;
	return 0;
	
}

● A cfloat file representing various floating-point types of system limitations.

Insert picture description here

● Numeric literals of various floating point types.

1. Write floating point numbers

    C ++ has two ways to write floating point numbers. The first is to use the commonly used standard decimal notation.
The second method of representing floating-point values ​​is called E notation, which looks like this: 3.45E6, which refers to the result of multiplying 3.45 and 1000000; E6 refers to the 6th power of 10, that is, 1 after 6 0s. Therefore, 3.45E6 means 3450000. 6 is called the exponent, and 3.45 is called the mantissa.

● C ++ arithmetic operators.

1. Five operators

Here are 5 basic C ++ arithmetic operators.
      ● The + operator performs an addition operation on the operand. For example, 4 + 20 is equal to 24.
      ●-The operator subtracts the second number from the first one. For example, 12-3 is equal to 9.
      ● The * operator multiplies the operands. For example, 28 * 4 is equal to 112.
      ● The / operator divides the first number by the second number. For example, 1000/5 is equal to 200. If both operands are integers, the result is the integer part of the quotient. For example, 17/3 is equal to 5, and the fractional part is discarded.
      ● The% operator is modulo. That is, it generates the remainder after dividing the first number by the second number. For example, 19% 6 is 1, because 19 is 3 times 6 and more than 1. Both operands must be integers, and using this operator for floating-point numbers will result in compilation errors. If one of them is negative, the sign of the result meets the following rules: (a / b) * b + a% b = a.

2. The type of quotient

    The behavior of the division operator (/) depends on the type of operand. If both operands are integers, C ++ will perform integer division. This means that the fractional part of the result will be discarded so that the final result is an integer. If one (or two) of the operands is a floating-point value, the fractional part will be retained and the result will be a floating-point number.

3. Type conversion

    The rich types of C ++ allow different types to be selected according to requirements, which also makes the operation of the computer more complicated. For example, the hardware compilation instructions involved in adding two short values ​​may be different from adding two long values. Since there are 11 integer types and 3 floating-point types, the computer needs to deal with a large number of different situations, especially when operating on different types. In order to deal with this potential confusion, C ++ performs automatic; multi-type conversion:
       ● When assigning a value of one arithmetic type to a variable of another arithmetic type, C ++ will convert the value;
       ● The expression contains different When the type is, C ++ will convert the value;
       ● When passing the parameter to the function, C ++ will convert the value.

● Automatic type conversion.

1. Conversion by initialization and assignment

    Assigning a value to a type with a larger value range usually does not cause any problems. For example, assigning a short value to a long variable does not change this value, it just takes up more bytes. However, assigning a large long value (such as 2111222333) to the float variable will reduce accuracy. Because float only has 6 significant digits, this value will be rounded to 2.1H122E9. Therefore, some conversions are safe and some will cause trouble. Table 3.3 lists some possible conversion problems.
Insert picture description here
    When assigning 0 to a bool variable, it will be converted to false; non-zero values ​​will be converted to true.
    Assigning floating-point values ​​to integers will cause two problems. First, converting a floating-point value to an integer will truncate the number (excluding the decimal part). Second, the float value may be too large for the int variable. In this case, C ++ does not define what the result should be; this means that different implementations may react differently.

2. Expression conversion

    What happens when two different arithmetic types are included in the same expression? In this case, C ++ performs two automatic conversions: first, some types are converted automatically when they appear; second, some Types are converted when they appear in an expression at the same time as other types.
    In short, the compiler determines the conversion performed in the arithmetic expression through the check table. C ++ 11 slightly modified this checklist. The following is the C ++ 11 version checklist. The compiler will consult this list in turn.
    (1) If one operand is of type longdouble, convert the other operand to longdouble.
    (2) Otherwise, if one operand is of type double, convert the other operand to double.
    (3) Otherwise, if one operand is of type float, convert the other operand to float.
    (4) Otherwise, the operands are all integers, so integer promotion is performed.
    (5) In this case, if both operands are signed or unsigned, and one of the operands has a lower level than the other, it is converted to a higher-level type.
    (6) If one operand is signed and the other is unsigned, and the level of the unsigned operand is one higher than the signed operand, convert the signed operand to the unsigned operand Types of.
    (7) Otherwise, if the signed type can represent all possible values ​​of the unsigned type, the unsigned operand is converted to the type to which the signed operand belongs.
    (8) Otherwise, convert both operands to an unsigned version of the signed type.

3 、 C ++ 11 auto

    C ++ 11 has added a tool that allows the compiler to infer the type of variables based on the type of the initial value. To this end, it redefines the meaning of auto. auto is a C language keyword, but it is rarely used. For its previous meaning, please refer to Chapter 9. In the initialization declaration, if you use the keyword auto without specifying the type of the variable, the compiler will set the type of the variable to be the same as the initial value:

auto n = 100; // n is int
auto x = 1.5; // x is double
auto y = 1.3e12L // y is long double

● Mandatory type conversion.

1.
    Forced conversion by type C ++ also allows explicit type conversion through the mandatory type conversion mechanism. (C ++ recognizes that there must be type rules, and sometimes these rules need to be overridden.) There are two formats for mandatory type conversion. For example, to convert the int value stored in the variable thomr to a long type.

int  thomr;
long (thomr);
//或者
(long) thomr;
Published 64 original articles · praised 406 · 20,000+ views

Guess you like

Origin blog.csdn.net/famur/article/details/105437141