定义出生日期类Birth,有成员变量year、month、day及构造函数、显示函数show();定义学生类Student,有成员变量name、id,子对象birth及构造函数、显示函数show。创建Student类对象stu,使用show函数输出stu信息。
时间: 2023-03-24 11:03:03 浏览: 113
好的,这个问题我可以回答。以下是代码实现:
```c
#include <stdio.h>
// 定义出生日期类Birth
typedef struct {
int year;
int month;
int day;
} Birth;
// 定义学生类Student
typedef struct {
char name[20];
int id;
Birth birth;
} Student;
// 构造函数
void initStudent(Student* stu, char* name, int id, int year, int month, int day) {
stu->id = id;
strcpy(stu->name, name);
stu->birth.year = year;
stu->birth.month = month;
stu->birth.day = day;
}
// 显示函数
void showStudent(Student* stu) {
printf("姓名:%s\n", stu->name);
printf("学号:%d\n", stu->id);
printf("出生日期:%d年%d月%d日\n", stu->birth.year, stu->birth.month, stu->birth.day);
}
int main() {
// 创建Student类对象stu
Student stu;
initStudent(&stu, "张三", 1001, 200, 1, 1);
// 使用show函数输出stu信息
showStudent(&stu);
return ;
}
```
输出结果为:
```
姓名:张三
学号:1001
出生日期:200年1月1日
```