Chapter II Variables and Basic types (2)

Source: Internet
Author: User

2.1 Basic built-in types (skip, nothing to write about, are some basic things)

2.2 Literal constants

5. String literals

1. The previous literal values are basic built-in types

2. String literals, denoted by 0 and more characters enclosed in double quotation marks, "" or "Hello word"

3. In order to be compatible with the C language, all string literals in C + + have a compiler g++ automatically adds a null character at the end.

4. Character literal ' A ', which represents a single character a

5. String literal "A", which represents the letter A and the null character, the 2-character string

2.3 Variables

Key concepts: strong static Types

    1. C + + is a statically typed language, and at compile time, type checking is done,
    2. So Perl is a dynamic type of language??
    3. Dynamic type of language, no compilation process??
    4. Anyway, after I've written the Perl program, it's not compiled, a.pl, just call Perl's interpreter, run it, Perl a.pl
    5. In C + +, the type of the variable determines which operations the variable supports, such as int a,b;,a.b, and it is clear that integer variables A and B are not supported. This operation,
      1. So, when to check, this illegal operation?
      2. In the compile phase check, g++ a.cc, if the above illegal operation, the compiler will prompt the error, do not produce the executable file
    6. compiler, you must be able to identify the type of each variable in your code, so in C + +, you must define the variables before using them
      1. int A, B; float c,d;
      2. The compiler must be able to recognize the type of each variable in the code above

2.3.3 Defining variables

  1. Initialization
    1. C + + supports the form of two types of initialization variables
      1. Replication initialization: Using the equals sign =,int a = 1024;
      2. Direct initialization: Place the initialization value in parentheses, int a (1024);
    2. Initialization and Assignment in C + + are two different operations, (I'm thinking, this is not too small buckle)
      1. Initialize Yes, create a variable and assign an initial value to the variable

      2. The assignment is to erase the current value of the object and replace it with the new value.

    3. using = to initialize variables, easy to make C + + programming novice, confused, easy to initialize as a form of assignment

      1. Wipe, so to see, C + + master on the initialization and assignment of the distinction is very sensitive, like a bean curd should add sugar or salt to distinguish a ratio.

    4. Since there are two types of initialization, there must be a difference between the two methods, now only after the direct initialization, the syntax is more flexible, and more efficient.

  2. Use multiple initialization

    1. First of all, what is the purpose of the author's writing this section?

      1. First, I still don't understand the concept of multiple initializers

      2. My current understanding is that there are two ways to initialize the object of the string class:

        1. Replication initialization: std::string str1 = "C + +";

        2. Direct initialization: std::string str2 ("C + +");

        3. Both of these methods create a string object, and the initial value is a copy of the string literal
        4. The so-called multiple initialization std::string STR3 = (10, ' 9 '); Initializes a string object with one counter and one character
          1. The only way to initialize the STR3 variable is to directly initialize the
          2. When there are multiple initializations, replication initialization cannot be used. (Duang, Duang, Duang, what is called multiple initialization or not understand!) )
  3. Initializing multiple variables
    1. Double salary = 9999, wage (salary + 1); Each variable has its own initialization method, which is copy initialization and direct initialization, respectively.
    2. int A, b =8,c=7,d=1; Initialization and uninitialized blending are also possible
    3. std::string title ("C + +"), publisher = "A-w"; Direct initialization and replication initialization

Initialization rules for 2.3.4 variables

    1. Initialization of built-in type variables
      1. Whether the built-in type variable is automatically initialized depends on where the variable is defined.
        1. Variables defined outside the body of the function are initialized to 0
        2. Variables defined in the function body are not initialized.

2.3.5 Declarations and definitions

    1. C + + programs typically consist of multiple files, such as a large pile of header files and a large heap of source files.
    2. In order for multiple source files to access the same variables, C + + distinguishes between declarations and definitions
    3. The definition of a variable:
      1. Used to allocate storage space for variables,
      2. You can also specify an initial value for a variable
      3. In a program, variables have and have only one definition
    4. Statement:
      1. Used to indicate to the program the type and name of the variable
      2. Definition is also a declaration, yes, int a = 5, is also a declaration, to the program shows that the type of variable A is an int, the name is a, and also allocated storage space for the variable, the initial value is specified
      3. Declaring a variable using the extern keyword instead of defining a variable
      4. extern int i; only the variable i is declared and no storage space is allocated for the variable i
      5. Just to illustrate, ah, I have a variable i here, but defined elsewhere in the program, I'm just here to tell you, haha
    5. In a program, a variable can be declared more than once, but only once.
    6. Variables used in multiple files, for example, variable a used in header and source files, need to have a declaration separated from the definition, typically a file containing the definition of a variable, other files that use that variable, and the declaration of the variable (not the definition)

Chapter II Variables and Basic types (2)

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.