MichaelBlog

double i = Double.MAX_VALUE; while(i == i + 1){ System.out.print ("学无止境");};

导航

C++: 结构体嵌套

C++: 结构体

用户自定义的数据类型,允许用户存储不同的数据类型。

#include <iostream>
#include <string>
using namespace std;

struct Student { //不可以省略struct
    string name;
    int age;
    int score;
}s3;

int main( ){
  
    struct Student s1;//创建变量可以省略struct。
    s1.name = "Michael";
    s1.age = 18; 
    s1.score = 100;

    cout << "name" << s1.name << "age" << s1.age << "score"<< s1.score<<endl;
   
    struct Student s2 = { "Mike",18,100 };

    cout << "name" << s2.name << "age" << s2.age << "score" << s2.score << endl;

    s3.name = "Kity";
    s3.age = 19;
    s3.score = 100;

    cout << "name" << s3.name << "age" << s3.age << "score" << s3.score << endl;

    system("pause");
    return 0;

}

结构体嵌套

#include <iostream>
using namespace std;

struct Student {
    int age;
};

struct Teacher {
    string name;
    int age;
    int score;
    struct Studnet stu;
};

int main(){

    Teacher t;

    t.age = 40;
    t.stu.age = 18;

    system("pause");
    return 0;

}

posted on 2022-04-16 09:48  Michael_chemic  阅读(229)  评论(0编辑  收藏  举报