02_Language Basics

C++ data types

1. Basic types: (1) character type (char) (2) integer type (int) (3) floating point type (float/double) (4) boolean type (bool)

2. Structural types: (1) array (2) structure (struct) (3) union (unoin) (4) enumeration (enum) (5) class (class)

3. Finger Type

4. Empty type

1. Basic data types

Character type: char

Only occupies 1 byte (1Byte=8bit), used to store an ASCII character or an 8-bit binary number (8 0 or 1, such as: 0100 01001)

Integer: int

4 bytes on a 32-bit computer, used to store an integer

Floating point: float/double

float occupies 4 bytes, double occupies 8 bytes. Floating-point type, also known as real type, is used to store real numbers

Boolean: bool

It is a newly added data type of C++ and occupies one byte. C language does not support boolean type!


The above basic data types can be preceded by the following modifiers to form multiple types:

signed signed     unsigned unsigned     long long     short short

When using these four modifiers to modify int, the keyword int can be omitted, for example, long is equivalent to long int

In C++, int and char without modifiers are considered as signed by the compiler, that is, they are equivalent to the modifier signed before, such as char is equivalent to signed char


2. Identifiers and Keywords

identifier

1. Consists of letters, numbers and underscores

2. The first character must be a letter

3. Uppercase and lowercase are considered two different characters

4. Unlimited length, usually named with meaningful words

5. Cannot be a C++ keyword

keywords

a lot

3. Constants and Variables

constant

1. Integer constants

2. Floating point constants

3. Character constants

(1) Ordinary characters: such as 'A' '>'

(2) Escape character: represented by backslash /, such as: '\a' '\\' '\"'

4. String constants

5. Symbolic constant: Use the type specifier const, which defines a variable as a symbolic constant, such as: const int a=40; Once defined, a cannot be changed, otherwise a compilation error occurs

variable


4. Operators and Expressions

operator

1. Arithmetic operators: (1) + (2) - (3) * (4) / (forward slash) (5)%

2. Relational operators: (1) > (2) >= (3) == (4) < (5) <= (6) !=

3. Logical operators: (1)! (2) && (3) || The operands on both sides of the operator can be of any type, but the result can only be 0 or a non-zero value

4. Assignment operator: = This operator also provides 10 compound assignment operators, such as: += >>=   

5. Self-increment and self-decrement operators: (1)++ (2)--

6. Comma operator: ,

7. Conditional operator: the only 3-term operator in C++, a ? b : c

8. Bit manipulation operators: (1) ~ (2) & (3) | (4) ^ (5) << (6) >>

9. Find the character size (length) operator: sizeof()

10. Cast operator: int a; double b=3.158; a= (int)(b)

11. Unary operators: (1) & address operator (2) * content operator These two operators are also binary operators

expression


5. Array type

1. Array definition
int a[4];
Note the difference with Java and C#


2. Array assignment


3. Character array
char s[9]="computer"; //The system will automatically add a '\0' after the last character of the array, so the length is 9


6. Enumeration Types

The value of a variable in the program is limited to the elements in the collection, and these data collections can be defined as an enumeration type

enum definition
enum weekday
{
    Sun, Mon, Tue, Wed, Thu, Fri, Sat
}
Definition of enum variable

(1)enum weekday d1,d2;    (2)enum weekday{...} d1,d2;    (3)enum{...} d1,d2;


7. Structure type

structure definition
struct person
{
    char num[5];
    char name[8];
    int age;
};
Definition of structure variables

Defining structure variables is similar to the definition of enumeration types, there are 3 types:

(1)person p1,p2[5];    (2)struct person{...}p1,p2[5];    (3)struct{...}p1,p2[5];

Usually, all structure type descriptions are stored in the .h header file, and then the header file is embedded in the program with the included command. When programming, you can use (1) to define structure variables, so it is recommended to use (1) way to define structure variables.

Initialization of structure variables
person p1={"16999","messi",21};
struct variable reference

(1) '.' is generally used, such as: p1.age;

(2) Use '->' for pointers, such as: p1->name;


8. Types of Unions

community definition

union person
{
    int age;
    char name[8];
}

Definition of Union Variables

Application of Union Variables


9. Control Statements

1. Select the structure statement

(1)if(){...}                        if(){...}    else if(){...} ...

(2) switch statement

switch(x)
{
    case a: xxxxx; break;
    case b: xxxxx; break
    case c: xxxxx; break;
    default :xxxxx; break;
}
2. Loop structure statement

(1)while(){...}

(2)do{...} while();

(3)for(e1;e2;e3){...}

3. Steering Statement

(1) break statement (2) continue statement (3) goto statement


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325492184&siteId=291194637