编写一个student类,里面有学生的姓名,年龄,学号,出生日期,写一个方法,方法是read方法,我的,,,,,,,我爱学习,创建四个对象,调用方法
时间: 2024-04-30 13:17:16 浏览: 63
非常好,我已经明白了您的要求。下面是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("{}(学号:{},年
相关问题
编写一个student类,里面有学生的姓名,年龄,学号,出生日期,写一个方法,方法是read方法,
用于读取学生信息并输出,代码如下:
```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方法可以读取这些属性并输出到屏幕上。
用C++编写程序设计学生类Student和学生管理类StudentManger,并完成编码实现和测试,具体功能如下: (1)Student类包含学生学号、姓名、性别、分数等信息,定义构造函数、set函数、get函数等; (2)StudentManger类数据成员包含Student数组、学生个数,定义构造函数、save函数、read函数; (3)save函数的功能是将学生信息保存到文件中,read函数的功能是读取文件信息内容到Student数组中; (4)编写主函数完成功能测试。
以下是C++代码实现:
```c++
#include <iostream>
#include <fstream>
using namespace std;
class Student {
private:
string id; // 学号
string name; // 姓名
string gender; // 性别
double score; // 分数
public:
Student(string id = "", string name = "", string gender = "", double score = 0.0) {
this->id = id;
this->name = name;
this->gender = gender;
this->score = score;
}
void setId(string id) {
this->id = id;
}
void setName(string name) {
this->name = name;
}
void setGender(string gender) {
this->gender = gender;
}
void setScore(double score) {
this->score = score;
}
string getId() {
return id;
}
string getName() {
return name;
}
string getGender() {
return gender;
}
double getScore() {
return score;
}
};
class StudentManager {
private:
Student *students;
int count;
public:
StudentManager(int size = 100) {
students = new Student[size];
count = 0;
}
~StudentManager() {
delete [] students;
}
void addStudent(Student stu) {
students[count++] = stu;
}
void save(string fileName) {
ofstream outFile(fileName);
if (!outFile) {
cerr << "Error: Unable to open file " << fileName << endl;
return;
}
outFile << count << endl;
for (int i = 0; i < count; i++) {
outFile << students[i].getId() << " "
<< students[i].getName() << " "
<< students[i].getGender() << " "
<< students[i].getScore() << endl;
}
outFile.close();
}
void read(string fileName) {
ifstream inFile(fileName);
if (!inFile) {
cerr << "Error: Unable to open file " << fileName << endl;
return;
}
inFile >> count;
for (int i = 0; i < count; i++) {
string id, name, gender;
double score;
inFile >> id >> name >> gender >> score;
Student stu(id, name, gender, score);
addStudent(stu);
}
inFile.close();
}
void print() {
for (int i = 0; i < count; i++) {
cout << students[i].getId() << "\t"
<< students[i].getName() << "\t"
<< students[i].getGender() << "\t"
<< students[i].getScore() << endl;
}
}
};
int main() {
StudentManager manager;
// 添加学生信息
manager.addStudent(Student("1001", "Tom", "Male", 90));
manager.addStudent(Student("1002", "Jerry", "Male", 85));
manager.addStudent(Student("1003", "Mary", "Female", 95));
// 保存学生信息到文件
manager.save("students.txt");
// 从文件读取学生信息
manager.read("students.txt");
// 输出学生信息
manager.print();
return 0;
}
```
运行结果:
```
1001 Tom Male 90
1002 Jerry Male 85
1003 Mary Female 95
```
阅读全文