C + + Learning Day10 -- references, classes

Keywords: C++ Back-end

Day1

Reference

What is a reference

A reference is a pointer. A reference is just a high-level modification based on correction to make the code easier to read. A reference is a reference to an existing variable. In other words, "reference variable" must refer to an existing variable. It is not a new variable itself. They do not really occupy memory.

Reference example

int main(){
	int a = 5;
	int* b = &a;//&Plus the existing variable points to its memory address
	int& ref = a;
	/*In this case, the & sign is actually a part of the variable type, and it does not necessarily mean correction or certain
	It's a reference. It depends on the characters before and after. Here, it's a reference because it's next to the variable type*/
	std::cin.get();
}

Now we have created an alias called ref, that is, Ref. this reference points to the address of variable a.

#define Log(x) std::cout<<x<<std::endl

int main(){
	int a = 5;
	int* b = &a;
	int& ref = a;
	ref = 2;
	Log(ref);
	Log(a);
	std::cin.get();
}


When we change the value of ref, what we actually change is the stored value of the address where a is located.
We create a function to increment the value.

void incream(int val)
{
	val++;
}

int main(){
	int a = 5;
	incream(a);
	Log(a);
	std::cin.get();
}

Now what is passed in is not a pointer or reference, but a value call. During the call process, copy the value stored by a to the function, and a new variable val will be directly created. So we need to pass this variable by reference to increment it. What should I do? Instead of passing the value into the function, we directly pass the address of the variable a. Because we can find the variable address in the function, we can see that the number 5 is being modified. Is to write through the memory address. There are two ways:
1. Use pointer

void incream(int* val)
{
	(*val)++;
}

int main(){
	int a = 5;
	incream(&a);
	Log(a);
	std::cin.get();
}

You'll see the value change

2. Use references

void incream(int& val)
{
	val++;
}

int main(){
	int a = 5;
	incream(a);
	Log(a);
	std::cin.get();
}

You will find that the value also becomes 6.

Important statement: once you declare a reference, you cannot change the object it references. For example:

int main(){
	int a = 5;
	int b = 8;

	int& ref = a;
	ref = b;
	//incream(a);
	Log(a);
	std::cin.get();
}

Now you assign a to ref, then you assign b to ref, and finally you will find that both a and b become 8.

That means that when you declare a reference, you must assign an actual variable to it. So what if I really want to change what the value ref refers to?
We need to create some variables so that it points to a first, and then to b. In fact, it means using pointers.

int main(){
	int a = 5;
	int b = 8;

	int* ref = &a;
	*ref = 2;
	ref = &b;
	*ref = 1;
	//incream(a);
	Log(a);
	Log(b);
	std::cin.get();
}

The result is:

Class

What is a class

In short, a class is a way to organize data and functions together. For example, in a game, we want to represent a player in some way, so what do we need to do to represent a player? We must need a data that represents the player's position, and the character may have other attributes, such as moving speed. We may also need 3D models to display players on the screen. All data needs to exist somewhere. We can create variables for these data.

int main(){
	int playerX0, playerY0;
	int speed0 = 2;
	int playerX1, playerY1;
	int speed1 = 2;
	std::cin.get();
}

playerX0 and playerX1 represent two players. When you have multiple players, it will be very troublesome. You may think of using arrays, but these are a pile of unorganized variables filled with code, which is not good. The other is that if you want to use functions to move on behalf of players, you will generate a lot of code, which is difficult to maintain. We can use classes to simplify this step. We can create a class for players, called player, which contains all the data we want at one time, and finally as a type.

//definition
class Player
{
	int x, y;
	int speed;
};

int main(){
	Player player;//instantiation 
	std::cin.get();
}

Variables made of class types are called objects, and the process of creating new pairs is called instantiation. If we want to set these variables, we can simply write player.x. if we directly access them, the compiler will directly report an error, because the properties / variables in the class are private by default, which also involves a concept called access control. So you need to change it to the following form:

class Player
{
public:
	int x, y;
	int speed;
};

Public means public. We can access it outside the class. Next, we can use a function to change the values of x and y.

class Player
{
public:
	int x, y;
	int speed;
};

void Move(Player& player,int xa,int ya)
{
	player.x += xa * player.speed;
	player.y = ya * player.speed;
}

int main(){
	Player player;
	Move(player,1,-1);
	std::cin.get();
}

In fact, there is a better way. Classes can contain functions. This function is called a method, which means that we can put the move function in the Player class. That is, it can be changed into the following form:

class Player
{
public:
	int x, y;
	int speed;
	void Move( int xa, int ya)
	{
		x += xa * speed;
		y = ya * speed;
	}
};



int main(){
	Player player;
	player.Move(1, -1);
	std::cin.get();
}

Posted by mandred on Mon, 08 Nov 2021 07:20:27 -0800