创建学生类,私有数据成员学号、姓名、 C ++成绩,添加静态数据成员系别 xb 初始值为信息工程,静态数据成员总分初始值为0,人数初始值为0,公有成员函数有参和无参构造函数、拷贝构造函数,析构函数,设置系别函数,添加设置学生信息函数、输出学生信息函数,求总分函数 total ,静态求平均分成员函数 avg ,在主函数中创建对象数组 stu 包含3个对象,循环设置、输出学生信息并求出学生平均分并打印
时间: 2024-03-15 14:46:26 浏览: 12
好的,我理解了你的要求。下面是对应的 C++ 代码实现:
```cpp
#include<iostream>
using namespace std;
class Student{
private:
int id; // 学号
string name; // 姓名
float cppScore; // C++成绩
static string xb; // 系别
static float total; // 总分
static int count; // 人数
public:
Student(){} // 无参构造函数
Student(int id, string name, float score){ // 有参构造函数
this->id = id;
this->name = name;
cppScore = score;
total += cppScore;
count++;
}
Student(const Student& stu){ // 拷贝构造函数
id = stu.id;
name = stu.name;
cppScore = stu.cppScore;
total += cppScore;
count++;
}
~Student(){ // 析构函数
count--;
total -= cppScore;
}
void setDept(string dept){ // 设置系别
xb = dept;
}
void setInfo(int id, string name, float score){ // 设置学生信息
this->id = id;
this->name = name;
cppScore = score;
total += cppScore;
count++;
}
void printInfo(){ // 输出学生信息
cout << "学号:" << id << ",姓名:" << name << ",C++成绩:" << cppScore << ",系别:" << xb << endl;
}
static float totalScore(){ // 求总分
return total;
}
static float avgScore(){ // 求平均分
if(count == 0) return 0;
else return total / count;
}
};
string Student::xb = "信息工程"; // 初始化系别
float Student::total = 0; // 初始化总分
int Student::count = 0; // 初始化人数
int main(){
Student stu[3];
for(int i = 0; i < 3; i++){
int id;
string name;
float score;
cout << "请输入第" << i+1 << "个学生的信息(学号、姓名、C++成绩):";
cin >> id >> name >> score;
stu[i].setInfo(id, name, score); // 设置学生信息
}
cout << "三个学生的信息如下:" << endl;
for(int i = 0; i < 3; i++){
stu[i].printInfo(); // 输出学生信息
}
cout << "三个学生的平均分为:" << Student::avgScore() << endl; // 求平均分并输出
return 0;
}
```
希望这份代码能够满足你的需求。