结构体 — 结构体的定义和使用_赋值

结构体 — 结构体的定义和使用_赋值_02

点击查看代码
#include<iostream>
#include<string> 

using namespace std;

//创建一个 自定义的数据类型
//语法:struct 类型名称 { 成员列表 };
struct Student
{
	//成员列表
	string name;

	int age;

	int score;

};



int main(){
	
	//创建一个结构体的 变量 (此时struct关键字可以省略)
	// struct Student s1
	struct Student s1;
	//给s1属性赋值,通过 . 访问结构体变量中的属性
	s1.name = "张三";
	cout << "姓名:" << s1.name << endl;

    // struct Student s2 = { ... }
	struct Student s2 = { "李四", 18, 80 };
	cout << "姓名:" << s2.name << endl;

    // 在定义结构体时顺便创建结构体变量
	struct Student
	{
		string name;
		int age;
		int score;

	}s3;
	s3.name = "王五";
	cout << "姓名:" << s3.name << endl;

	system("pause");

	return 0;
}