设计一个Student类,每名学生包含学号、姓名和总评成绩3个属性,学生的学号和姓名通过初始化得到,学生的总评成绩通过赋值运算符 “=”得到(例如Student s(“01234”,”张三”);s=95;)。要求能够通过普通函数display输出学生的学号、姓名和总评成绩等信息。
时间: 2024-03-17 13:45:56 浏览: 62
好的,以下是Student类的设计和实现:
```c++
#include <iostream>
using namespace std;
class Student {
private:
string id; // 学号
string name; // 姓名
int score; // 总评成绩
public:
Student(string id, string name) { // 构造函数
this->id = id;
this->name = name;
score = 0; // 初始化总评成绩为0
}
void operator=(int score) { // 赋值运算符重载
this->score = score;
}
void display() { // 输出学生信息
cout << "学号:" << id << endl;
cout << "姓名:" << name << endl;
cout << "总评成绩:" << score << endl;
}
};
```
使用方法如下:
```c++
int main() {
Student s("01234", "张三");
s = 95; // 赋值总评成绩
s.display(); // 输出学生信息
return 0;
}
```
输出结果如下:
```
学号:01234
姓名:张三
总评成绩:95
```
阅读全文
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)