创建学生类Student,包含姓名(string或char数组都可)、学号(int)、三门课考试成绩(用float数组表示)。编写构造函数及其它必要函数。 创建班级类Banji,可根据学生人数动态创建班级。编写构造函数和析构函数,并提供获取指定学生对象的成员函数。 编写主函数测试上述类,包括创建Banji对象,设置/修改某班某同学成绩等。
时间: 2023-05-22 20:04:13 浏览: 77
设计学生类Student,属性:编号(整型);姓名(字符串),成绩(整型)。
5星 · 资源好评率100%
好的,我了解要扮演一个由CSDN开发的C知道,我将尽力回答您的问题。关于您的第一个问题:
```
class Student {
public:
Student(string name, int id, float score1, float score2, float score3) {
this->name = name;
this->id = id;
scores[0] = score1;
scores[1] = score2;
scores[2] = score3;
}
void setName(string name) {
this->name = name;
}
void setID(int id) {
this->id = id;
}
void setScore(int i, float score) {
scores[i] = score;
}
string getName() {
return name;
}
int getID() {
return id;
}
float* getScores() {
return scores;
}
private:
string name;
int id;
float scores[3];
};
class Banji {
public:
Banji(int numStudents) {
this->numStudents = numStudents;
students = new Student[numStudents];
}
~Banji() {
delete[] students;
}
void setStudent(Student student, int index) {
students[index] = student;
}
Student getStudent(int index) {
return students[index];
}
private:
int numStudents;
Student* students;
};
int main() {
Banji banji(3);
Student student1("Tom", 1001, 80.5, 77.5, 90.0);
Student student2("Alice", 1002, 85.0, 73.5, 81.0);
Student student3("Bob", 1003, 77.0, 92.5, 87.0);
banji.setStudent(student1, 0);
banji.setStudent(student2, 1);
banji.setStudent(student3, 2);
Student student = banji.getStudent(1);
cout << "Name: " << student.getName() << endl;
cout << "ID: " << student.getID() << endl;
float* scores = student.getScores();
for (int i = 0; i < 3; i++) {
cout << "Score " << i + 1 << ": " << scores[i] << endl;
}
return 0;
}
```
在这里我们创建了一个Student类和一个Banji类,Student类包含姓名、学号和三门课的考试成绩,Banji类可以根据学生人数动态创建,并提供了设置和获取某个学生对象的成员函数。我们编写了主函数来测试这两个类,包括创建Banji对象,设置/修改某班某同学成绩等。希望这可以对您有所帮助!
阅读全文