c++编程: 1)设计主函数; 2)定义1个对象stu1; 3)调用成员函数Input(),录入对象stu1的语文(chn)、数学(math)和英语(eng)成绩; 4)调用成员函数Display(),显示学生学号和总分数。
时间: 2024-01-05 08:05:02 浏览: 36
以下是符合要求的C++代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string id; // 学号
int chn; // 语文成绩
int math; // 数学成绩
int eng; // 英语成绩
public:
void Input() {
cout << "请输入学生的学号、语文、数学和英语成绩(以空格分隔):" << endl;
cin >> id >> chn >> math >> eng;
}
void Display() {
int total = chn + math + eng;
cout << "学生 " << id << " 的总分是:" << total << endl;
}
};
int main() {
Student stu1;
stu1.Input();
stu1.Display();
return 0;
}
```
在这个代码中,我们定义了一个名为 `Student` 的类,它有私有成员变量 `id`、`chn`、`math` 和 `eng`,分别表示学号、语文、数学和英语成绩。类中还定义了两个公有成员函数 `Input()` 和 `Display()`,分别用于录入成绩和显示学号和总分数。
在 `main()` 函数中,我们首先创建了一个名为 `stu1` 的 `Student` 对象,然后调用了它的 `Input()` 和 `Display()` 成员函数,完成了录入成绩和输出总分数的功能。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![dta](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)