定义学生类结构体,包含学生学号,姓名,出生日期
时间: 2023-09-13 19:03:52 浏览: 100
好的,以下是一个定义学生类结构体的示例代码:
```c++
struct Student {
int student_id; // 学号
std::string name; // 姓名
std::string birthday; // 出生日期,可以用字符串存储
};
```
在这个示例代码中,我们使用了 C++ 中的结构体来定义了一个名为 `Student` 的类,它包含了三个成员变量:`student_id` 表示学生的学号,`name` 表示学生的姓名,`birthday` 表示学生的出生日期。其中,`name` 和 `birthday` 都是用字符串来存储的,因为它们不需要进行数值计算,而是需要进行字符串操作。
相关问题
请用C++定义一个学生结构体student,包括学生的学号、姓名、性别和 出生日期。出生日期要求包括年、月、日。编写程序输出一个 学生的所有信息。
以下是用C++定义学生结构体student的代码:
```c++
#include <iostream>
using namespace std;
struct student {
string id; // 学号
string name; // 姓名
char gender; // 性别
int birthYear; // 出生年份
int birthMonth; // 出生月份
int birthDay; // 出生日期
};
int main() {
student s;
s.id = "20210001";
s.name = "张三";
s.gender = 'M';
s.birthYear = 2002;
s.birthMonth = 10;
s.birthDay = 1;
cout << "学号:" << s.id << endl;
cout << "姓名:" << s.name << endl;
cout << "性别:" << s.gender << endl;
cout << "出生日期:" << s.birthYear << "-" << s.birthMonth << "-" << s.birthDay << endl;
return 0;
}
```
输出结果:
```
学号:20210001
姓名:张三
性别:M
出生日期:2002-10-1
```
人的出生日期由年、月、日组成,定义一个结构体数据类型包含出生日期三个属性(struct date),定义一个学生类,类里面有属性学号、姓名、年龄、生日(用定义的结构体)、联系方式、地址等信息,可增加其他内容,给学生类赋值(自己的个人信息)
好的,我来帮你回答这个问题。
首先,我们可以定义一个结构体来表示出生日期:
```
struct date {
int year;
int month;
int day;
};
```
然后,我们可以定义一个学生类,其中包括学号、姓名、年龄、生日、联系方式和地址等信息:
```
class Student {
public:
int id; // 学号
string name; // 姓名
int age; // 年龄
date birthday; // 生日
string phone; // 联系方式
string address; // 地址
// 构造函数
Student(int id, string name, int age, date birthday, string phone, string address) {
this->id = id;
this->name = name;
this->age = age;
this->birthday = birthday;
this->phone = phone;
this->address = address;
}
};
```
接下来,我们可以创建一个自己的学生对象,赋上自己的个人信息:
```
int main() {
date my_birthday = {1999, 9, 1};
Student me(20210001, "张三", 22, my_birthday, "13812345678", "北京市海淀区中关村大街1号");
// 输出我的信息
cout << "学号:" << me.id << endl;
cout << "姓名:" << me.name << endl;
cout << "年龄:" << me.age << endl;
cout << "生日:" << me.birthday.year << "-" << me.birthday.month << "-" << me.birthday.day << endl;
cout << "联系方式:" << me.phone << endl;
cout << "地址:" << me.address << endl;
return 0;
}
```
输出结果如下:
```
学号:20210001
姓名:张三
年龄:22
生日:1999-9-1
联系方式:13812345678
地址:北京市海淀区中关村大街1号
```
希望我的回答能够帮助到你。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)