C++学习日志 13

结构体基本概念
结构体术语用户自定义的数据类型,允许用户存储不同的数据类型

结构体定义和使用
语法:struct 结构体名 { 结构体成员列表 };

示例:
#include<iostream>
using namespace std;
#include<string>

struct Student {//这里的struct不能够省略
    //成员列表
    string name;
    int age;
    int score;
}s3;//定义结构体时顺便创建变量

int main() {
    //struct Student s1,这里的struct可以省略
    Student s1;//这里的struct可以省略
    s1.name = "张三";
    s1.age = 20;
    s1.score = 100;
    cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl;

    //struct Student s2 = {...}
    struct Student s2 = { "李四",19,80 };
    cout << "姓名:" << s2.name << " 年龄:" << s2.age << " 分数:" << s2.score << endl;

    //定义结构体时顺便创建变量
    s3.name = "王五";
    s3.age = 18;
    s3.score = 89;
    cout << "姓名:" << s3.name << " 年龄:" << s3.age << " 分数:" << s3.score << endl;

    system("pause");
    return 0;
}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务