C + + Primer 5th (Chinese version) "Overview" of the first part of the--c++ Foundation, chapter II

Source: Internet
Author: User

C + + primer is almost finished, but it is absolutely impossible to read this book in light. Read at least two times and do most of the operational exercises again to remember most of the details. Here, I would like to make a very very, very brief but integrated core part of C + + 's first--c++ Foundation, and take a glance at C + + (a very shallow part) instead of getting lost in detail.

Chapter II variables and basic types

2.1 Basic built-in types

1.c++ defines the arithmetic type and the empty type . Common types are bool, char, short, int., long, long long, float, double, long double, unsigned char, unsigned short, unsigned (in T), unsigned long, unsigned long long...

2. Type conversion. The floating-point variable discards the decimal point, and the other type converts to bool only false or true,true essentially represents 1,false, which essentially represents 0.

3. Try not to mix the signed type and the unsigned type, otherwise it is easy to cause overflow problems (negative numbers are converted to unsigned).

The operation between 4.unsigned must ensure that the result can not be a negative number, otherwise it will occur around the phenomenon, so that the results become very strange large numbers.

5. Integers starting with 0 represent octal, followed by only 0,1,2,3,4,5,6,7 eight symbols, otherwise an error occurs, integers starting with 0x (0X) represent 16, and thereafter 8,9,a,b,c,d,e,f and A,b,c,d,e,f are accepted.

2.2 Variables

6.std::string is a library type, which is defined in the namespace Std. The details of string are explained in detail in the third and Nineth chapters, which are not mentioned here first.

7. Beginners may think that initialization and assignment are no different, after all, on the surface, they are "give a variable a value." In reality, however, the difference between them is very large, especially in the case of copy control and dynamic memory allocation, where the behavior of the two operations may be completely different.

8. Take int as an example, there are four ways of initializing: int a=0;    int a={0};    int a{0};  int a (0); There is also a difference between them, and in the latter the difference will be more obvious.

9. Variables that are defined within a function but do not specify an initial value are initialized by default . At this point the variable is given the default value. What the default value is depends on the type of the variable, such as String, which does not accept the constructor of the argument (chapter seventh) initializes it to an empty string (note that the empty string is not a string with a space, but nothing).

10. Variable naming is generally lowercase letters, class names usually start with uppercase letters, user-defined identifiers cannot appear consecutively with two underscores, and cannot be underlined with the beginning of a capital letter. Additionally, identifiers outside the body of the function cannot begin with an underscore.

11. A valid region of a name begins with the declaration statement of the name, ending at the end of the scope at which the statement is located. Variables that are in the global scope are hidden by a variable with the same name as the inner layer, and can be obtained through the global scope "::" Access:

 1  #include <iostream>2  int  A; //  external variable, with int a=0; equivalent  3  int   main (  4  {  5  int  a = 1   6  std::cout <<:: a << Std::endl; //  Using an external variable A, output 0  7  return  0< /span> 8 } 

2.3 Composite Types

The composite type refers to a type that is defined based on another type. The main two of the composite types in C + + are references and pointers.

For reference, the key point is that it is equivalent to an individual name for an existing variable. This is equivalent to a binding, not a copy, and the value of the pointer is the address of a variable.

2.4 Const Qualifier

The 13.const is divided into top and bottom layers. The top-level const pointer represents the value of the pointer (not the address of the pointer, the address of the variable to which the pointer is pointing), which means that the pointer cannot be assigned, but it can point to other legitimate objects, and the underlying pointer indicates that the object that the pointer refers to cannot be changed (to be exact, Can not be changed by the way of this pointer. If this pointer points to a very mass, this very amount can of course be changed by other means. It can be imagined that the underlying const is subjectively thinking that he is pointing to a constant, so consciously obey the rules without changing the value he points to.

2.5 Processing Type

The.auto type specifier can be used to infer types. After c++14, Auto is available almost everywhere , including but not limited to function return types, templates. Note that auto discards the top-level and reference properties, and if you need to define the top-level or reference type through auto, you need to pass the const auto, auto&, or const auto&.

Thedecltype type prompt can be imagined to be true inference. Note that Decltype does not calculate its value, and does not manipulate it. such as DECLYTPE (a=1) B; This statement does not assign 1 to variable a. If it is a dereference operation, Decltype will get the reference type. The reason is also very simple, look at the following code:

int a=1,*b=&a;

*b=2;

The second line of these two lines of code is equivalent to changing the value of a by a pointer to 2 indirectly. As you can imagine, the second line of *b is only a reference type that can be bound to a and really change the value of a. If the type of *b is a value type, then at best a copy of a, assigning it a value of 2 does not change the a itself.

In addition, if you add one or more layers of parentheses to a variable, Decltype will also get a reference type.

2.6 Custom Data Structures

16. With respect to custom data structures, the difference between using the keyword struct and class is only two points: Default access rights (chapter seventh) and Inheritance (chapter 15th).

C + + Primer 5th (Chinese version) "Overview" of the first part of the--c++ Foundation, chapter II

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.