[C++primer notes] Chapter 2: variables and basic types

Chapter II variables and basic types

2.1 basic built-in type

2.1.1 arithmetic type

Including integer and floating-point types, C++11 specifies a minimum of 8 bits for char, 16 bits for int and 64 bits for long, allowing the compiler to give them larger sizes, such as float32 bits. Where char corresponds to a machine byte.
The smallest addressable memory block: byte, with 8 bits
A word usually has 4 bytes and 32 bits.
signed char theoretically represents the value of - 128-127.

When you know that the value cannot be negative, use the unsigned type.

2.1.2 type conversion

	bool b = 42;  // b =1 
	int i = b;  // i = 1
	i = 3.14;  // i = 3, only the integer part of the floating point number is retained.
	double p = i;  // p = 3.0
	unsigned char c = -1;   // c = 255, unsigned number out of range mod256
	//Principle 0 - 1, and unsigned 0-255
	signed char c2 = 256; /
	//When we give a signed number a value that is out of range, undefine
unsigned u = 10;
int i = -42;
std::cout<< u + i << std::endl;
//Output 4295967264, unsigned int size, result after modulo
	Another classic example, 408, has been tested and has been circulating,
	i = 0 Then subtract one to get 2 int 
for(unsigned int i = 10;i>=0;i--)
	std::cout << i << std::endl;

2.1.3 literal

Integer and floating point literals
Character and string literals
Transfer sequence \ n \t \ generally only look at the first three characters. Pay attention to the backslash

Specify the literal value... Question: is it really useful?

2.2 variables

A variable is a storage space that can be manipulated by a program.
2.2.1 variable definition

Initialization is not assignment. Initialization means to give an initial value to a variable when it is created, and assignment means to erase the current value of the object and replace it with a new value.

		List initialization,Initialize variables with curly braces.
	int units_sold = 0;
	int units_sold = {0};
	int units_sold{0}'
	int units_sold(0);

It is recommended to initialize each built-in variable to simply and reliably prevent errors caused by accessing uncertain values in uninitialized variables.

2.2.2 relationship between variable declaration and definition

C + + supports separate compilation.
Declaration: make the variable name known to the program, because the separated compiled program may have multiple files,
One file may use the variable name of another file, so declare it.

extern int i ; // Declare i instead of defining i
int j ;      // Definition j

Note: the declaration does not explicitly initialize variables, that is, assign values.
C + + is a statically typed language that checks types at compile time.
The compiler checks whether the data type supports the operation to be performed.

2.2.3 identifier

int somename, someName, SOMENAME;

The identifier must start with a letter or underscore. It is case sensitive and cannot be a keyword name
Identifiers defined outside a function cannot begin with an underscore.

Variable naming conventions:
1.Variable names are generally in lowercase letters, such as index,Do not use Index
2.User defined class names generally begin with uppercase letters, such as Sales_item
3.Underline multiple words, such as student_loan´╝î Do not use studentloan

			 If we can persist, it will be effective.

2.2.4 scope of name

Most scopes are separated by curly braces,

2.3 composite type

Composite types include references and pointers.

A reference is another name for an object.
The reference itself is not an object. Once a reference is defined, it can no longer be bound to other objects.

	int i = 1024;
	int &r = i;  
	r Values and i Always bind.

The pointer itself is an object, which allows assignment and copying of the pointer without assigning an initial value at the time of definition*
Gets the address of the object

	int ival = 42;
	int *p = &ival;
	Pointer to the address of an object.

Accessing objects with pointers

	int ival = 42;
	int *p = &ival;
	cout << *p;
	If the pointer points to an object,
	Dereference is allowed*To access the object.
	Assigning a value to the object pointed to by the dereference character is equivalent to assigning a value to the object.

Key concept: some symbols have multiple meanings

int i = 42;
int &r = i;  //quote
int *p;  // Define p pointer
p = &i;   
*p = i;
int &r2 = *p;

A null pointer does not point to any object.

int *p1 = nullptr; //Literal nullptr initialization pointer
int *p2 = 0;
int *p3 = NULL;
 The three are equivalent. They all define null pointers.

Recommendation: initialize all pointers.
If an uninitialized pointer is used, the current content of the memory space occupied by the pointer will be regarded as an address value. Accessing the pointer is equivalent to accessing a non-existent object in a non-existent location. If there is content in the address, a very thorny problem will arise.

2.3.3 understanding compound type declarations

	int i = 1024, *p = &i, &r = i;

There is only one basic data type, but the form of declarator can be different, that is, different types of variables may be defined in a definition statement.

Pointer to pointer

2.4const qualifier

const ing buff_size = 512; 

The compiler will replace all the places where buff_size is used with 512 during compilation,
It is only valid in files. const for different files needs to be defined repeatedly for this variable,
The solution is to add the extern keyword to each file, whether declared or defined

file1.cc Medium:
extern const int buffsize = fcn();
--------
file1.h in
extern const int buffsize;

2.5 treatment type

Type alias: helps to understand the real purpose of using the type.

typedef double wages; // Waves and double are synonymous
typedef wages base, *p; // p is a synonym for double *

Another way to declare aliases

using SI = Sales_item;  // synonym

P61 pointer, constant and type alias are not understood

2.5.2 auto type

Auto allows the compiler to analyze the type of expression for us and calculate the type of variable through the initial value. Obviously, the variable defined by auto must have the initial value.

	auto item = Sales_item1 + Sales_item2;
	item Type is Sales_item
	auto i = 0, *p = &i;

2.5.3 decltype type

Sometimes you want to infer the type of the variable to be defined from the type of the expression, but you don't want to initialize the variable with the value of the expression. Therefore, decltype is introduced to select and return the data type of the operand.

const int ci = 0, &cj = ci;
decltype(ci) x = 0; // The type of x is const int
decltype(cj) y = x;  // The type of y is const int &, and y binds x
decltype(cj) z;   // Error, z is a reference and must be initialized

int i = 42, *p = &i, &r = i;
decltype(r+0) b; //Yes, b is an uninitialized int
decltype(*p) c;   // Error, c is int &, must be initialized

2.6 user defined data structure

Summary

Glossary

Learning experience

Tags: C++

Posted on Sat, 09 Oct 2021 01:25:04 -0400 by Sakujou