设计一个学生类,包含:学号、数学、英语、程设、数据结构、总分,功能:
1.接收学号及四门成绩,设计构造函数、析构函数、复制构造函数;
2.求每一位学生四门课的最高分、最低分(多个接口)
3.显示学生信息
参考:
#include<iostream>
#include<cstring>
using namespace std;
class Student {
private:
char name[18];//定义长度为18的姓名字符数组
int num;//学号
int mathScore, englishScore;//数学成绩,英语成绩
static int count, mathTotalScore, englishTotalScore;//人数,数学总成绩,英语总成绩
public:
Student(const char* nm, int nu, int math, int english) :num(nu), mathScore(math), englishScore(english) {
strcpy(name, nm);
count++;//计数器
mathTotalScore += math;//统计数学总成绩
englishTotalScore += english;//统计英语总成绩
}
//析构函数,释放字符数组
~Student() {
delete[]name;
}
//显示基本数据:
void showBase() {
cout << "显示基本数据:\n" << endl;
cout << "姓名:" << name << endl;
cout << "学号:" << num << endl;
cout << "数学成绩:" << mathScore << endl;
cout << "英语成绩:" << englishScore << endl << endl;
}
//显示静态数据:
static void ShowStatic() {
cout << "显示静态数据:\n" << endl;
cout << "总人数:" << count << endl;
cout << "数学总成绩:" << mathTotalScore << endl;
cout << "英语总成绩:" << englishTotalScore << endl;
}
};
//静态成员数据初始化
int Student::count = 0;
int Student::mathTotalScore = 0;
int Student::englishTotalScore = 0;
int main() {
//对象数组
Student stu[3] = { {"张三",2018,89,76},
{"李四",2019,79,86},
{"王五",2020,69,96}
};
//遍历输出数组,打印基本数据
for (int i = 0; i < 3; i++) {
stu[i].showBase();
}
//打印静态数据
stu->ShowStatic();
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!