定义学生类,含有学生姓名、学号、成绩、学生数、学生累加成绩和平均成绩,显示各项内容。 输入输出说明: 输出: name is liming number is 070201 score is 90 count is 1 sum is 90 ave is 90 name is zhanghao number is 070202 score is 80 count is 2 sum is 170 ave is 85
时间: 2024-01-11 13:04:55 浏览: 61
好的,这是一个C++编程题,需要你编写代码来实现要求的功能。下面是代码参考:
```c++
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
string number;
int score;
static int count;
static int sum;
public:
Student(string n, string num, int s) {
name = n;
number = num;
score = s;
count++;
sum += s;
}
void output() {
cout << "name is " << name << " number is " << number << " score is " << score << " count is " << count << " sum is " << sum << " ave is " << sum / count << endl;
}
};
int Student::count = 0;
int Student::sum = 0;
int main() {
Student s1("liming", "070201", 90);
s1.output();
Student s2("zhanghao", "070202", 80);
s2.output();
return 0;
}
```
输出结果:
```
name is liming number is 070201 score is 90 count is 1 sum is 90 ave is 90
name is zhanghao number is 070202 score is 80 count is 2 sum is 170 ave is 85
```
说明:
- Student类定义了构造函数和output成员函数,用于初始化对象和输出学生信息。
- 在主函数中,通过调用构造函数创建两个Student对象s1和s2,并传入姓名、学号和成绩。
- 每次创建对象时,count计数器加1,sum成绩累加器加上该对象的成绩,用于计算平均成绩。
- 最后,通过调用每个对象的output函数输出学生信息,包括姓名、学号、成绩、学生数、学生累加成绩和平均成绩。
阅读全文