设计学生类Student和学生管理类StudentManger,并完成编码实现和测试,具体功能如下: (1)Student类包含学生学号、姓名、性别、分数等信息,定义构造函数、set函数、get函数等; (2)StudentManger类数据成员包含
时间: 2023-12-14 12:36:14 浏览: 247
一个学生数组和数组长度,成员函数包含添加学生、删除学生、修改学生信息、查询学生信息等功能。具体编码实现如下:
```c++
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string stuNo; // 学号
string name; // 姓名
char gender; // 性别
float score; // 分数
public:
// 构造函数
Student(string _stuNo = "", string _name = "", char _gender = 'M', float _score = 0.0) {
stuNo = _stuNo;
name = _name;
gender = _gender;
score = _score;
}
// 设置学号
void setStuNo(string _stuNo) {
stuNo = _stuNo;
}
// 获取学号
string getStuNo() {
return stuNo;
}
// 设置姓名
void setName(string _name) {
name = _name;
}
// 获取姓名
string getName() {
return name;
}
// 设置性别
void setGender(char _gender) {
gender = _gender;
}
// 获取性别
char getGender() {
return gender;
}
// 设置分数
void setScore(float _score) {
score = _score;
}
// 获取分数
float getScore() {
return score;
}
};
class StudentManager {
private:
Student* students; // 学生数组
int len; // 数组长度
public:
// 构造函数
StudentManager() {
students = NULL;
len = 0;
}
// 析构函数
~StudentManager() {
if (students != NULL) {
delete[] students;
}
}
// 添加学生
void addStudent(Student stu) {
// 先将原有数组复制到新数组中
Student* newStudents = new Student[len+1];
for (int i = 0; i < len; i++) {
newStudents[i] = students[i];
}
// 添加新学生
newStudents[len] = stu;
// 释放原有数组
if (students != NULL) {
delete[] students;
}
// 更新数组指针和长度
students = newStudents;
len++;
}
// 删除学生
void deleteStudent(string stuNo) {
// 先查找学生
int index = findStudent(stuNo);
if (index < 0) {
cout << "该学号的学生不存在!" << endl;
return;
}
// 将该学生从数组中删除
for (int i = index; i < len-1; i++) {
students[i] = students[i+1];
}
// 释放原有数组
Student* newStudents = new Student[len-1];
for (int i = 0; i < len-1; i++) {
newStudents[i] = students[i];
}
if (students != NULL) {
delete[] students;
}
// 更新数组指针和长度
students = newStudents;
len--;
}
// 修改学生信息
void modifyStudent(string stuNo, string name, char gender, float score) {
// 先查找学生
int index = findStudent(stuNo);
if (index < 0) {
cout << "该学号的学生不存在!" << endl;
return;
}
// 修改学生信息
students[index].setName(name);
students[index].setGender(gender);
students[index].setScore(score);
}
// 查询学生信息
void queryStudent(string stuNo) {
// 先查找学生
int index = findStudent(stuNo);
if (index < 0) {
cout << "该学号的学生不存在!" << endl;
return;
}
// 输出学生信息
cout << "学号:" << students[index].getStuNo() << endl;
cout << "姓名:" << students[index].getName() << endl;
cout << "性别:" << students[index].getGender() << endl;
cout << "分数:" << students[index].getScore() << endl;
}
private:
// 查找学生(返回值为数组下标)
int findStudent(string stuNo) {
for (int i = 0; i < len; i++) {
if (students[i].getStuNo() == stuNo) {
return i;
}
}
return -1;
}
};
int main() {
// 测试代码
StudentManager sm;
sm.addStudent(Student("001", "张三", 'M', 90.0));
sm.addStudent(Student("002", "李四", 'F', 85.0));
sm.addStudent(Student("003", "王五", 'M', 95.0));
sm.addStudent(Student("004", "赵六", 'F', 80.0));
sm.queryStudent("002");
sm.deleteStudent("003");
sm.modifyStudent("004", "钱七", 'M', 88.0);
sm.queryStudent("003");
return 0;
}
```
阅读全文