用c++生成按下列要求编写程序。 (1)定义一个分数类Score。它有 3个数据成员: Chinese //语文课成绩 English //英语课成绩 Mathematics //数学课成绩 2个构造函数:无参的和带参数的 3个成员函数:是否带参数根据需要自定 sum() //计算三门课总成绩 print() //输出三门课成绩和总成绩 modify() //修改三门课成绩 (2)定义一个学生类Student。它有 3个数据成员: Num //学号 Name //姓名 MyScore //成绩 2个构造函数:无参的和带参数的 3个成员函数:是否带参数根据需要自定 sum() //计算某学生三门课总成绩 print() //输出某学生学号、姓名和成绩 modify() //修改某学生学号、姓名和成绩 在主函数中,先定义一个学生类对象数组,再通过for循环给对象数组赋上实际值,最后输出对象数组个元素的值
时间: 2023-06-09 18:07:48 浏览: 110
好的,您要求的C++代码如下:
```
#include <iostream>
#include <string>
using namespace std;
class Score {
private:
int Chinese, English, Mathematics;
public:
Score() {}
Score(int Chinese, int English, int Mathematics) {
this->Chinese = Chinese;
this->English = English;
this->Mathematics = Mathematics;
}
int sum() {
return Chinese + English + Mathematics;
}
void print() {
cout << "语文:" << Chinese << ",英语:" << English << ",数学:" << Mathematics << ",总成绩:" << sum() << endl;
}
void modify(int Chinese, int English, int Mathematics) {
this->Chinese = Chinese;
this->English = English;
this->Mathematics = Mathematics;
}
};
class Student {
private:
int Num;
string Name;
Score MyScore;
public:
Student() {}
Student(int Num, string Name, Score MyScore) {
this->Num = Num;
this->Name = Name;
this->MyScore = MyScore;
}
int sum() {
return MyScore.sum();
}
void print() {
cout << "学号:" << Num << ",姓名:" << Name;
cout << ",成绩为:" ;
MyScore.print();
}
void modify(int Num, string Name, int Chinese, int English, int Mathematics) {
this->Num = Num;
this->Name = Name;
MyScore.modify(Chinese, English, Mathematics);
}
};
int main() {
Student students[3];
for (int i = 0; i < 3; i++) {
int num, chinese, english, math;
string name;
cout << "请输入第" << i+1 << "个学生的学号、姓名、成绩(语文、英语、数学):";
cin >> num >> name >> chinese >> english >> math;
Score score(chinese, english, math);
Student student(num, name, score);
students[i] = student;
}
for (int i = 0; i < 3; i++) {
students[i].print();
}
return 0;
}
```
阅读全文