编写一个student类,里面有学生的姓名,年龄,学号,出生日期,写一个方法,方法是read方法,
时间: 2024-05-09 11:17:18 浏览: 67
用于读取学生信息并输出,代码如下:
```python
class Student:
def __init__(self, name, age, student_id, birthdate):
self.name = name
self.age = age
self.student_id = student_id
self.birthdate = birthdate
def read(self):
print("姓名:", self.name)
print("年龄:", self.age)
print("学号:", self.student_id)
print("出生日期:", self.birthdate)
```
这个类有四个属性:学生姓名、年龄、学号和出生日期。read方法可以读取这些属性并输出到屏幕上。
相关问题
编写一个student类,里面有学生的姓名,年龄,学号,出生日期,写一个方法,方法是read方法,我的,,,,,,,我爱学习,创建四个对象,调用方法
非常好,我已经明白了您的要求。下面是Python语言实现的Student类代码及read方法:
```python
class Student:
def __init__(self, name, age, id, birthday):
self.name = name
self.age = age
self.id = id
self.birthday = birthday
def read(self):
print("{}(学号:{},年
用C++编写程序设计学生类Student和学生管理类StudentManger,并完成编码实现和测试,具体功能如下: (1)Student类包含学生学号、姓名、性别、分数等信息,定义构造函数、set函数、get函数等; (2)StudentManger类数据成员包含Student数组、学生个数,定义构造函数、save函数、read函数; (3)save函数的功能是将学生信息保存到文件中,read函数的功能是读取文件信息内容到Student数组中; (4)编写主函数完成功能测试。
以下是C++编写的程序:
```cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int MAX_STUDENT_NUM = 100; // 学生数量上限
// 学生类
class Student {
public:
// 构造函数
Student(string id="", string name="", char gender=' ', float score=0.0) {
this->id = id;
this->name = name;
this->gender = gender;
this->score = score;
}
// set函数
void setId(string id) {
this->id = id;
}
void setName(string name) {
this->name = name;
}
void setGender(char gender) {
this->gender = gender;
}
void setScore(float score) {
this->score = score;
}
// get函数
string getId() const {
return id;
}
string getName() const {
return name;
}
char getGender() const {
return gender;
}
float getScore() const {
return score;
}
private:
string id; // 学生学号
string name; // 学生姓名
char gender; // 学生性别
float score; // 学生分数
};
// 学生管理类
class StudentManager {
public:
// 构造函数
StudentManager() {
studentNum = 0;
}
// 保存学生信息到文件中
bool save(string filename) {
ofstream fout(filename.c_str());
if (!fout) {
cout << "Error: cannot write to file " << filename << endl;
return false;
}
fout << studentNum << endl;
for (int i = 0; i < studentNum; i++) {
fout << students[i].getId() << " "
<< students[i].getName() << " "
<< students[i].getGender() << " "
<< students[i].getScore() << endl;
}
fout.close();
return true;
}
// 从文件中读取学生信息
bool read(string filename) {
ifstream fin(filename.c_str());
if (!fin) {
cout << "Error: cannot open file " << filename << endl;
return false;
}
fin >> studentNum;
for (int i = 0; i < studentNum; i++) {
string id, name;
char gender;
float score;
fin >> id >> name >> gender >> score;
students[i].setId(id);
students[i].setName(name);
students[i].setGender(gender);
students[i].setScore(score);
}
fin.close();
return true;
}
// 添加学生
bool addStudent(Student student) {
if (studentNum == MAX_STUDENT_NUM) {
cout << "Error: too many students" << endl;
return false;
}
students[studentNum++] = student;
return true;
}
// 删除学生
bool removeStudent(string id) {
int index = -1;
for (int i = 0; i < studentNum; i++) {
if (students[i].getId() == id) {
index = i;
break;
}
}
if (index == -1) {
cout << "Error: student " << id << " not found" << endl;
return false;
}
for (int i = index; i < studentNum - 1; i++) {
students[i] = students[i+1];
}
studentNum--;
return true;
}
// 修改学生信息
bool modifyStudent(string id, Student student) {
int index = -1;
for (int i = 0; i < studentNum; i++) {
if (students[i].getId() == id) {
index = i;
break;
}
}
if (index == -1) {
cout << "Error: student " << id << " not found" << endl;
return false;
}
students[index] = student;
return true;
}
// 根据学号查找学生
Student* findStudentById(string id) {
for (int i = 0; i < studentNum; i++) {
if (students[i].getId() == id) {
return &students[i];
}
}
return NULL;
}
// 根据姓名查找学生
Student* findStudentByName(string name) {
for (int i = 0; i < studentNum; i++) {
if (students[i].getName() == name) {
return &students[i];
}
}
return NULL;
}
// 显示所有学生信息
void showAllStudents() {
cout << "ID\tName\tGender\tScore" << endl;
for (int i = 0; i < studentNum; i++) {
cout << students[i].getId() << "\t"
<< students[i].getName() << "\t"
<< students[i].getGender() << "\t"
<< students[i].getScore() << endl;
}
}
private:
Student students[MAX_STUDENT_NUM]; // 学生数组
int studentNum; // 学生数量
};
int main() {
StudentManager manager;
string filename = "students.txt";
// 从文件中读取学生信息
if (!manager.read(filename)) {
return 1;
}
while (true) {
cout << "1. Add Student" << endl;
cout << "2. Remove Student" << endl;
cout << "3. Modify Student" << endl;
cout << "4. Find Student by ID" << endl;
cout << "5. Find Student by Name" << endl;
cout << "6. Show All Students" << endl;
cout << "7. Save to File" << endl;
cout << "0. Exit" << endl;
cout << "Enter your choice: ";
int choice;
cin >> choice;
switch (choice) {
case 1: {
string id, name;
char gender;
float score;
cout << "Enter student ID: ";
cin >> id;
cout << "Enter student name: ";
cin >> name;
cout << "Enter student gender (M/F): ";
cin >> gender;
cout << "Enter student score: ";
cin >> score;
Student student(id, name, gender, score);
manager.addStudent(student);
break;
}
case 2: {
string id;
cout << "Enter student ID: ";
cin >> id;
manager.removeStudent(id);
break;
}
case 3: {
string id, name;
char gender;
float score;
cout << "Enter student ID: ";
cin >> id;
Student* student = manager.findStudentById(id);
if (student == NULL) {
break;
}
cout << "Enter student name: ";
cin >> name;
cout << "Enter student gender (M/F): ";
cin >> gender;
cout << "Enter student score: ";
cin >> score;
student->setName(name);
student->setGender(gender);
student->setScore(score);
manager.modifyStudent(id, *student);
break;
}
case 4: {
string id;
cout << "Enter student ID: ";
cin >> id;
Student* student = manager.findStudentById(id);
if (student == NULL) {
cout << "Error: student " << id << " not found" << endl;
break;
}
cout << "ID\tName\tGender\tScore" << endl;
cout << student->getId() << "\t"
<< student->getName() << "\t"
<< student->getGender() << "\t"
<< student->getScore() << endl;
break;
}
case 5: {
string name;
cout << "Enter student name: ";
cin >> name;
Student* student = manager.findStudentByName(name);
if (student == NULL) {
cout << "Error: student " << name << " not found" << endl;
break;
}
cout << "ID\tName\tGender\tScore" << endl;
cout << student->getId() << "\t"
<< student->getName() << "\t"
<< student->getGender() << "\t"
<< student->getScore() << endl;
break;
}
case 6: {
manager.showAllStudents();
break;
}
case 7: {
if (!manager.save(filename)) {
return 1;
}
break;
}
case 0: {
return 0;
}
default: {
cout << "Invalid input" << endl;
break;
}
}
}
}
```
该程序实现了学生类和学生管理类,包括以下功能:
1. 添加学生
2. 删除学生
3. 修改学生信息
4. 根据学号查找学生
5. 根据姓名查找学生
6. 显示所有学生信息
7. 保存学生信息到文件中
8. 从文件中读取学生信息
在主函数中,我们通过一个死循环来实现不断的输入和处理用户的选择。根据用户的选择,调用相应的学生管理类中的函数来实现功能。用户可以选择在程序退出前将学生信息保存到文件中。
阅读全文