学生参加考试,有N个学生,每个学生参加3门课程考试。要求定义学生类student,数据有学号num,姓名name,三门课程socre[3]:数学,英语,计算机;静态成员count表示学生人数。要求工程文件实现,工程名称为:自己姓名。 (1) 输入学生学号,姓名,学生成绩。(setdate 函数实现) (2) 设计构造函数和析购函数。每构造一个对象,实现个数加1,每删除一个对象,个数减1。 (3) 求每个学生的总分。(成员函数stu_sum实现) (4) 设计一个友元函数,输出学生每门课程成绩对应的等级。(友元函数kc_dj()) 90~100:优秀;80~90:良好;70~79:中等;60~69:及格;小于60:不及格。 (5) 输出学生信息,每门课程成绩,相应等级,学生总分。(print函数实现) (6) 编写主函数验证,输入学生个数,学生相应数据,输出信息。对象数组实现。
时间: 2024-02-15 18:05:34 浏览: 28
以下是一个可能的实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
static int count; // 学生人数
int num; // 学号
string name; // 姓名
int score[3]; // 三门课程成绩
public:
Student(int num, string name, int score1, int score2, int score3) {
this->num = num;
this->name = name;
this->score[0] = score1;
this->score[1] = score2;
this->score[2] = score3;
count++;
}
~Student() {
count--;
}
void setdate(int num, string name, int score1, int score2, int score3) {
this->num = num;
this->name = name;
this->score[0] = score1;
this->score[1] = score2;
this->score[2] = score3;
}
int stu_sum() {
return score[0] + score[1] + score[2];
}
friend void kc_dj(Student s);
void print() {
cout << "学号:" << num << ",姓名:" << name << endl;
cout << "数学成绩:" << score[0] << ",等级:";
kc_dj(*this);
cout << "英语成绩:" << score[1] << ",等级:";
kc_dj(*this);
cout << "计算机成绩:" << score[2] << ",等级:";
kc_dj(*this);
cout << "总分:" << stu_sum() << endl;
}
static int getCount() {
return count;
}
};
int Student::count = 0;
void kc_dj(Student s) {
for (int i = 0; i < 3; i++) {
int score = s.score[i];
cout << score << ":";
if (score >= 90) {
cout << "优秀";
} else if (score >= 80) {
cout << "良好";
} else if (score >= 70) {
cout << "中等";
} else if (score >= 60) {
cout << "及格";
} else {
cout << "不及格";
}
cout << " ";
}
cout << endl;
}
int main() {
int n;
cout << "请输入学生个数:";
cin >> n;
Student *students = new Student[n];
for (int i = 0; i < n; i++) {
int num, score1, score2, score3;
string name;
cout << "请输入第" << i + 1 << "个学生的学号、姓名、数学成绩、英语成绩、计算机成绩(用空格分隔):";
cin >> num >> name >> score1 >> score2 >> score3;
students[i].setdate(num, name, score1, score2, score3);
}
cout << "学生人数:" << Student::getCount() << endl;
for (int i = 0; i < n; i++) {
students[i].print();
}
delete[] students;
return 0;
}
```
在这个实现中,我们使用了一个静态成员变量 `count` 来表示学生人数。每次创建一个 `Student` 对象时,都会将 `count` 加一,每次销毁一个对象时,都会将 `count` 减一。我们还实现了一个静态成员函数 `getCount()` 来获取学生人数。
构造函数和析构函数分别用于实现个数加一和减一的操作。
`setdate()` 函数用于设置学生的学号、姓名和三门课程成绩。
`stu_sum()` 函数用于计算学生的总分。
`kc_dj()` 函数是一个友元函数,用于输出学生每门课程成绩对应的等级。
`print()` 函数用于输出学生信息、每门课程成绩、相应等级和学生总分。
在主函数中,我们首先输入学生人数,然后创建一个 `Student` 类型的对象数组,并逐个输入每个学生的信息。之后打印学生人数和每个学生的信息,最后释放对象数组的内存。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)