The fourth day of c + + Learning

1. Code area

It has two characteristics:

1 is sharing, that is, we click on many of the same files, but only execute that piece of code and will not generate another piece of code.

2 is readable, that is, the code is only readable, but cannot be written, otherwise the data will be modified and the game gold coin will be changed

2. Global area

1. Variables stored in the global area

1. Global variables

#include<iostream>
int a=10;//global variable
int main()
{

}

2. Static variables

static int a=10;

3. String constant

"hello world"

4. Global constants

#include<iostream>
const int a=10;//Global constant
int main()
{

}

2. Variables not stored in the global area

1. Local constants

const int a=10;

2. Local variables

int a=10;

3. Summary

1. Generally speaking, I learned the classification of constants, mainly string constants

2.c + + is divided into global area and code area before the program runs

3. Stack area

Precautions for stack area:

1. Don't return the address of the local variable: because the local variable is stored in the stack area, and the data in the stack area is automatically released after the function is executed, we can't continue to use it. You may find that it can be used again, but that's because the compiler helps us keep it automatically once

4. Stacking area

#include <iostream>
using namespace std;
int* func()
{
	int* a = new int(10);//New opens up a space on the heap area, which is controlled and released by the programmer area, and new returns an address
	return a;
}
int main()
{
	int* p = func();
	cout << *p << endl;//p=10
	return  0;
}

5.new operator

1. Create a small space

#include <iostream>
using namespace std;
int main()
{
	int* p = new int(10);
	cout << *p << endl;
	delete p;//Address after delete
	return 0;
}

2. Open up a one-dimensional array

#include <iostream>
using namespace std;
int main()
{
	int* array = new int[10];//It means a number of 10 sizes
	for (int i = 0; i < 10; i++)
	{
		array[i] = 100 + i;
	}
	for(int i=0;i<10;i++)
	{
		cout << array[i] << " ";
	}
	delete[] array;//We're going to release the array like this and add a []
	return 0;
}

6. References in C + +

1. Reference must be initialized

int &b;//This is not possible and needs to be initialized

2. Once initialized, it cannot be changed

#include <iostream>
using namespace std;
int main()
{
	int a = 10;
	int &b = a;
	int c = 20;
	b = c;//This is assignment
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	cout << "c=" << c << endl;
	return 0;
}

3. Function of reference

When exchanging values, in addition to pointer exchange, you can also use referenced operations

#include <iostream>
using namespace std;
void swap(int& a, int& b)//Do it in this way.
{
	int temp = a;
	a = b; 
	b = temp;
}
int main()
{
	int a = 10;
	int b = 20;
	swap(a, b);
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	return 0;
}

The principle is that a and b in void swap (int & A, int & b) are equivalent to aliases. We actually exchange the original A and b

4. Nature of reference

1. A reference is actually an integer pointer, specifically a pointer constant

#include <iostream>
int main()
{
	int a = 10;
	int& b = a;//Actually int * const B = & A;
	b = 20;//The c + + compiler finds that B is a reference and automatically converts it to * b=20. All subsequent examples are * b=val;
	return 0;
}

7. Advanced order of function

1. We can write the equal sign directly when defining the function

#include <iostream>
using namespace std;
int func(int a, int b = 20, int c = 30)//We can do this
{
	return a + b + c;
}
int main()
{
	cout << func(10);//ha-ha
	return 0;
}

#include <iostream>
using namespace std;
int func(int a, int b = 20, int c = 30)
{
	return a + b + c;
}
int main()
{
	cout << func(10, 30, 30);
	return 0;
}
//The output is 70, not 60, indicating that we have stronger ability to pass parameters and can eat what we defined there

2. There are two points to note

1. When the assignment is defined directly in the function, it should be written from left to right

#include <iostream>
using namespace std;
int func(int a, int b = 20, int c )//That's not possible, because b has been assigned, so we have to write c as well
{
	return a + b + c;
}
int main()
{
	cout << func(10, 30, 30);
	return 0;
}

2. Only one function can be declared and defined

#include <iostream>
using namespace std;

int func(int a = 10, int b = 20, int c = 30);//We can't do this. How can we understand that the compiler will be confused. Is it written in you or in there
int func(int a = 10, int b = 20, int c = 30)
{
	return a + b + c;
}
int main()
{
	cout << func();
	return 0;
} 

Keywords: C++ Back-end

Added by Jalz on Sat, 15 Jan 2022 01:45:50 +0200