0
点赞
收藏
分享

微信扫一扫

结构体01:结构体的定义和使用

_鱼与渔_ 2022-01-13 阅读 74
#include<iostream>
#include<string>
using namespace std;
//创建学生数据类型 
struct Student{
	string name;
	int age;
	int score;
}s3;

//通过学生类型创建具体学生 
int main(){
	//1.
	//struct 关键字可以省略 
	struct Student s1;
	//给s1属性赋值
	s1.name="张三"; 
	s1.age=18;
	s1.score=100;
	cout<<"姓名:"<<s1.name<<"  "<<"年龄:"<<s1.age<<"  "<<"成绩:"<<s1.score<<endl;
	
	
	//2.
	struct Student s2{
		"李四",18,100  
	};
	cout<<"姓名:"<<s2.name<<"  "<<"年龄:"<<s2.age<<"  "<<"成绩:"<<s2.score<<endl;
	
	//3. 在定义结构体时顺便创建变量 
	s3.name="王五";
	s3.age=18;
	s3.score=100;
	cout<<"姓名:"<<s3.name<<"  "<<"年龄:"<<s3.age<<"  "<<"成绩:"<<s3.score<<endl;
	
	
	
}
举报

相关推荐

0 条评论