完成下面的成绩类 Score; 再把教材【例 2.7】中的 Student 类加以修改,使其含 有类 Score 的对象成员并在 main( )函数中测试 Student 类。 class Score{ //Score 类定义 float computer; float english; float mathematics; public: Score(float x1, float y1, float z1); Score( ); void print( ); void modify(float x2, float y2, float z2); };
时间: 2023-06-03 11:03:46 浏览: 95
抱歉,我可以回答这个问题。下面是 Score 类和修改过的 Student 类的代码:
```
#include <iostream>
using namespace std;
class Score{
//Score 类定义
float computer;
float english;
float mathematics;
public:
Score(float x1, float y1, float z1){
computer = x1;
english = y1;
mathematics = z1;
}
Score( ){
computer = 0;
english = 0;
mathematics = 0;
}
void print( ){
cout << "computer score: " << computer << endl;
cout << "english score: " << english << endl;
cout << "mathematics score: " << mathematics << endl;
}
void modify(float x2, float y2, float z2){
computer = x2;
english = y2;
mathematics = z2;
}
};
class Student{
string name;
int age;
Score score;
public:
Student(string n, int a, float c, float e, float m): name(n), age(a), score(c, e, m){}
void print(){
cout << "name: " << name << endl;
cout << "age: " << age << endl;
score.print();
}
void modify(float c, float e, float m){
score.modify(c, e, m);
}
};
int main(){
Student s1("Tom", 20, 80, 90, 85);
s1.print();
cout << "===========" << endl;
s1.modify(85, 92, 88);
s1.print();
return 0;
}
```
运行结果为:
```
name: Tom
age: 20
computer score: 80
english score: 90
mathematics score: 85
===========
name: Tom
age: 20
computer score: 85
english score: 92
mathematics score: 88
```
阅读全文