现要求用C++设计一个表示学生的类,里面有学生的三项成绩:计算机成绩、数学成绩、英语成绩,要求可以求总分、平均分、最高分、最低分,并且可以输出一个学生的完整信息,问:此类该如何设计? 要求:1.根据题目要求设计学生类,除了成绩以外要包含学生ID、姓名、年龄 2.有一个默认无参的构造函数,和一个带参的构造函数,函数的成绩参数是缺省的 3.所有属性必须私有化 4.提供get和set方法用于访问类中的成员 5.设计输出学生信息的方法 6.编写调用测试语句
时间: 2023-06-01 22:06:04 浏览: 199
class Student{
private:
int ID;
string name;
int age;
float computerScore;
float mathScore;
float englishScore;
public:
Student(){
ID = 0;
name = "";
age = 0;
computerScore = 0.0;
mathScore = 0.0;
englishScore = 0.0;
}
Student(int id, string n, int a, float cs=0.0, float ms=0.0, float es=0.0){
ID = id;
name = n;
age = a;
computerScore = cs;
mathScore = ms;
englishScore = es;
}
void setID(int id){
ID = id;
}
void setName(string n){
name = n;
}
void setAge(int a){
age = a;
}
void setComputerScore(float cs){
computerScore = cs;
}
void setMathScore(float ms){
mathScore = ms;
}
void setEnglishScore(float es){
englishScore = es;
}
int getID(){
return ID;
}
string getName(){
return name;
}
int getAge(){
return age;
}
float getComputerScore(){
return computerScore;
}
float getMathScore(){
return mathScore;
}
float getEnglishScore(){
return englishScore;
}
float getTotalScore(){
return computerScore + mathScore + englishScore;
}
float getAverageScore(){
return getTotalScore()/3.0;
}
float getMaxScore(){
float maxScore = computerScore;
if (mathScore > maxScore){
maxScore = mathScore;
}
if (englishScore > maxScore){
maxScore = englishScore;
}
return maxScore;
}
float getMinScore(){
float minScore = computerScore;
if (mathScore < minScore){
minScore = mathScore;
}
if (englishScore < minScore){
minScore = englishScore;
}
return minScore;
}
void printInfo(){
cout << "学生ID:" << ID << endl;
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "计算机成绩:" << computerScore << endl;
cout << "数学成绩:" << mathScore << endl;
cout << "英语成绩:" << englishScore << endl;
cout << "总分:" << getTotalScore() << endl;
cout << "平均分:" << getAverageScore() << endl;
cout << "最高分:" << getMaxScore() << endl;
cout << "最低分:" << getMinScore() << endl;
}
};
int main(){
Student s1(1001, "ZhangSan", 18, 88.5, 90.0, 85.0);
s1.printInfo();
return 0;
}
阅读全文
相关推荐


















