Detailed C++ structure (1)

Recently, a netizen sent me a private message and asked me to publish a blog post about the structure to explain the basic structure of the structure. However, since the structure itself can be related to pointers and arrays, the blogger is preparing to publish a three-phase structure The blog post of the body separately explains the explanation of the structure.
Without further ado, let's start to learn about the structure. Prerequisite: When learning any new knowledge, first understand the concept. Only when you have a correct and sufficient understanding of the concept can you use the corresponding tool (keyword) correctly

1. Definition of structure

A structure is actually a structure in which the member list consists of several members, and each member is a component of the structure.
The definition format of the structure: struct structure name {property name;};

Understanding: We can understand it as a template, but at the same time, we can regard it as a custom type, just like int type, short type, char type, such keywords to use. The (class) class we learned later is actually very similar in structure to (structure) struct.

Struct is a keyword, and the structure name after struct is artificially set, and the structure name can be regarded as a template, a template with attributes.

For example, we want to create a template of an animal. The attributes of this animal are: name, weight, and age.
Therefore, our analysis of this idea should be: ①create the template of animals, ②add attributes to this template.
The code is implemented like this:

//创建动物结构体
struct Animal	
{
    
    
//属性名
	char name[10];		//名字
	int weight;			//体重
	int age;			//年龄
};

Obviously, the animal structure we created in this way conforms to the definition format of the structure: struct structure name {property name;};

Two, the variables of the structure

What are the variables of the structure? As we mentioned earlier, to create a structure, we use struct to create it. The format is: struct structure name {property name;};
but after we create this structure, we can use the structure, which is the template , Which is the Animal above us, we can understand Animal as a keyword similar to int, short, and char to use. We can use Animal to define variables, and the variables defined by Animal also have memory.
However, what we need to say here is that Animal is a template. This template has its own attributes. If an object is created with such a template with attributes, the created object will have the same attributes.

First of all, let's take a look at several ways to create objects in a structure:
Two (1) Create an object when creating a structure

Format:
struct structure name
{attribute name;} variable name 1, variable name 2;

//创建动物结构体的同时创建结构体变量
struct Animal	
{
    
    
//属性名
	char name[10];		//名字
	int weight;			//体重
	int age;			//年龄
}Cat,Dog;//用Animal这个结构体创建了Cat,Dog这两个结构变量,这两个变量都拥有名字,体重,年龄这几个属性。
Two (two) create the object after creating the structure

Format: structure name structure variable name;

#include<iostream>
using namespace std;

//第一步,创建动物结构体
struct Animal	
{
    
    
//属性名
	char name[10];		//名字
	int weight;			//体重
	int age;			//年龄
};

int main()
{
    
    
//第二步,创建完结构体后再用结构名去创建变量
	Animal Cat;	//用Animal这个结构体创建了Cat变量,Cat拥有Animal的所有属性
	Animal Dog;	//用Animal这个结构体创建了Dog变量,Dog拥有Animal的所有属性

	return 0;
}
Two (three) After understanding how to define structure variables, let's take a look at how to use variables to call the properties of variables.

Format: variable name. Attribute name
Through this format, we can initialize the attributes of the variable accordingly
Insert picture description here

Two (4) When it comes to initialization, there are two ways to initialize structure variables: ① Direct initialization (initialize when defining structure variables, and the order of initializing members is the order of structure attribute declaration) ② Indirect initialization.

Direct initialization is the same as the following:
the attribute declaration order of the structure itself is: name[10];weight;age
Insert picture description here
Then, when directly initialized, the order of assignment is the name, weight, and age. The assignment is performed and the output verification is successful. The properties of the variables are initialized.
Code such as:

#include<iostream>
using namespace std;

//创建动物结构体
struct Animal	
{
    
    
//属性名
	char name[10];		//名字
	int weight;			//体重
	int age;			//年龄
};

int main()
{
    
    
	Animal Cat = {
    
    "猫",10,1};	//在创建结构变量的时候进行初始化,初始化顺序为结构体属性的声明顺序
	Animal Dog = {
    
    "狗",20,2};	//直接初始化,分别为名字,体重,年龄
	cout <<"动物名字为:"<< Cat.name << endl;
	cout << "动物体重为:" << Cat.weight << endl;
	cout << "动物年龄为:" << Cat.age << endl;

	cout << "动物名字为:" << Dog.name << endl;
	cout << "动物体重为:" << Dog.weight << endl;
	cout << "动物年龄为:" << Dog.age << endl;
	return 0;
}

Operation result:
Insert picture description here
Indirect initialization is not to initialize when the variable is defined, but to call the attribute through the structure variable to assign the attribute.

#include<iostream>
using namespace std;

//创建动物结构体
struct Animal	
{
    
    
//属性名
	char name[10];		//名字
	int weight;			//体重
	int age;			//年龄
};

int main()
{
    
    
	Animal Cat = {
    
    "猫"};	//当然,我们可以只对变量的属性部分初始化,赋值的顺序仍然是结构属性声明的顺序。所以猫赋值给了字符数组name[10]
	Animal Dog = {
    
    "狗"};	//同理



	//对结构变量的属性进行调用,并且对属性进行初始化---间接初始化
	Cat.age = 2;
	cout <<Cat.name<< "的年龄为:" << Cat.age << "岁" << endl;
	Dog.weight = 30;
	cout <<Dog.name<< "的体重为:" << Dog.weight << "千克" << endl;
	return 0;
}

operation result:
Insert picture description here

Three, structure members

Through one or two sections, we learned how to create a structure, how to use the structure to create variables, and how to use variables to call their own related properties. Here we introduce the concept of structure members.
Structure members: Structure members include member properties and member functions (also called member methods or structure methods).
Member properties: member properties are what we said above, the structure's own properties. After the structure defines the variables, Variables also have the attributes of this structure. Thereby it is possible to perform operations such as assignment and output of attributes.
Member function: Member function is actually the function of the structure, what kind of function this template can perform, or what can the object created by this template do and what function it performs. This is the member function.

Still like the above, let’s take Animal as an example. We know that most animals can eat and jump, so eating can be used as a structure function, so we can think of writing the corresponding structure like this A function. Therefore, the variables created by the structure can also have the functions of the structure, so that the structure variable can call the corresponding member function.

== Format: structure variable. Function name (formal parameter list) ==
For example: the Insert picture description here
code is as follows:

#include<iostream>
using namespace std;

//创建动物结构体
struct Animal	
{
    
    
//成员属性:
	char name[10];		//名字
	int weight;			//体重
	int age;			//年龄
//成员方法:
	void Eat()
	{
    
    
		cout<<name << "吃东西了" << endl;
	}
	void Jump()
	{
    
    
		cout <<name<< "跳跃了" << endl;
	}


};

int main()
{
    
    
	Animal Cat = {
    
    "猫"};	//当然,我们可以只对变量的属性部分初始化,赋值的顺序仍然是结构属性声明的顺序。所以猫赋值给了字符数组name[10]
	Animal Dog = {
    
    "狗"};	//同理



	//对结构变量的属性进行调用,并且对属性进行初始化---间接初始化
	Cat.age = 2;
	cout <<Cat.name<< "的年龄为:" << Cat.age << "岁" << endl;
	Dog.weight = 30;
	cout <<Dog.name<< "的体重为:" << Dog.weight << "千克" << endl;

	//对结构体变量的函数进行调用,简称为:结构体变量执行了某功能。(或者调用了某函数)
	Cat.Eat();		//猫调用了吃东西函数(或者猫调用了吃东西方法)
	Cat.Jump();		//猫调用了跳跃函数(或者猫调用了跳跃方法)

	Dog.Eat();		//狗调用了吃东西函数(或者狗调用了吃东西方法)
	Dog.Jump();		//狗调用了跳跃函数(或者狗用了跳跃方法)

	return 0;
}

Operation result:
Insert picture description here
Regarding the learning of C++, welcome everyone to follow me, comment on my articles, like, bookmark, follow me not to get lost. Netizens are also welcome to discuss C++ dry goods with me. The knowledge of C++ is much more than that, we will analyze and explain one by one. If netizens want me to explain, they can comment and leave a message, and I will do my best to explain it to everyone.

: : ProMer_Wang

Link: https://blog.csdn.net/qq_43801020/article/details/107182468

This article is the original article of ProMer_Wang, the copyright belongs to the author, please indicate the source of the original text for reprinting, welcome to reprint!

Guess you like

Origin blog.csdn.net/qq_43801020/article/details/107182468