2、编写程序,设计一个学生类 student,包括姓名和 3 门课成绩,利用重载运算符“+”将所有学生的成绩相 加并放在一个对象中,再对该对象求各门课程的平均分。
时间: 2023-09-12 15:05:13 浏览: 105
### 回答1:
下面是一个简单的实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
double score[3];
public:
Student(string name, double s1, double s2, double s3) {
this->name = name;
score[0] = s1;
score[1] = s2;
score[2] = s3;
}
Student operator+(const Student& other) const {
double s1 = score[0] + other.score[0];
double s2 = score[1] + other.score[1];
double s3 = score[2] + other.score[2];
return Student("", s1, s2, s3);
}
double getAverage(int i) const {
return score[i] / 2.0;
}
};
int main() {
Student s1("Tom", 80, 90, 85);
Student s2("Jerry", 90, 85, 95);
Student s3 = s1 + s2;
for (int i = 0; i < 3; i++) {
cout << "Subject " << i+1 << " average score: " << s3.getAverage(i) << endl;
}
return 0;
}
```
运行结果:
```
Subject 1 average score: 85
Subject 2 average score: 87.5
Subject 3 average score: 90
```
注:该实现中假设了每门课程的满分均为 100 分。如果需要处理其他情况,可以根据实际情况进行修改。
### 回答2:
首先,我们需要定义一个学生类Student,该类包括姓名和3门课程成绩:
```cpp
#include <iostream>
using namespace std;
class Student {
private:
string name;
int score[3];
public:
Student(string n, int s1, int s2, int s3) {
name = n;
score[0] = s1;
score[1] = s2;
score[2] = s3;
}
Student operator+(const Student& other) {
string newName = name + " " + other.name;
int newScore[3];
for (int i = 0; i < 3; i++) {
newScore[i] = score[i] + other.score[i];
}
return Student(newName, newScore[0], newScore[1], newScore[2]);
}
float operator[](int subject) {
return score[subject];
}
void printAverage() {
float sum = 0;
for (int i = 0; i < 3; i++) {
sum += score[i];
}
float average = sum / 3;
cout << "平均分:" << average << endl;
}
};
```
以上代码实现了重载运算符+,使得可以将两个学生的成绩相加,并返回一个新的学生对象。同时,重载运算符[],使得可以通过索引获取某门课程成绩。还定义了一个printAverage()函数,用于计算和输出平均分。
下面示例程序演示了使用该学生类:
```cpp
int main() {
Student student1("张三", 80, 85, 90);
Student student2("李四", 75, 88, 92);
// 使用重载运算符+将两个学生的成绩相加
Student total = student1 + student2;
// 输出每门课程的成绩
cout << "总成绩:" << total[0] << " " << total[1] << " " << total[2] << endl;
// 计算和输出平均分
total.printAverage();
return 0;
}
```
输出结果为:
```
总成绩:155 173 182
平均分:170
```
### 回答3:
学生类(Student)需要有姓名(name)和三门课程成绩(score1, score2, score3)属性。为了实现将所有学生的成绩相加并放在一个对象中并求各门课程的平均分,我们可以引入一个类静态变量(totalscore)来记录累计成绩和学生人数(count)。
```python
class Student:
totalscore = [0, 0, 0]
count = 0
def __init__(self, name, score1, score2, score3):
self.name = name
self.score1 = score1
self.score2 = score2
self.score3 = score3
self.addscore()
self.count += 1
def __add__(self, other):
return Student("", self.score1+other.score1, self.score2+other.score2, self.score3+other.score3)
def addscore(self):
Student.totalscore[0] += self.score1
Student.totalscore[1] += self.score2
Student.totalscore[2] += self.score3
@staticmethod
def print_avg():
avg_score = [total_score/Student.count for total_score in Student.totalscore]
print("三门课的平均分分别为:", avg_score[0], avg_score[1], avg_score[2])
stu1 = Student("小明", 90, 80, 95)
stu2 = Student("小红", 85, 88, 92)
stu3 = Student("小刚", 78, 95, 88)
total_stu = stu1 + stu2 + stu3
Student.print_avg()
```
以上代码中,我们定义了一个除了`__init__`初始化方法外,还定义了`__add__`方法实现学生成绩相加,并将结果返回。
通过`addscore`方法,我们将每个学生的成绩累加到静态变量`totalscore`中。
最后,通过调用`print_avg`方法,我们可以计算出每门课程的平均分并输出。
在测试中,我们创建了3个学生对象,然后利用重载运算符`+`将学生对象进行相加,得到一个包含所有学生成绩的新学生对象`total_stu`。然后,通过调用`Student.print_avg()`方法,输出并得到各门课程的平均分。