c++学生类Student中包含当前学生对象的姓名、学号和三门课程的成绩
时间: 2023-12-06 09:40:49 浏览: 67
好的,那么我们可以按照以下方式定义一个C++的学生类Student,其中包含姓名、学号和三门课程的成绩:
```c++
class Student {
public:
// 构造函数
Student(string name, string id, float score1, float score2, float score3) {
this->name = name;
this->id = id;
this->score1 = score1;
this->score2 = score2;
this->score3 = score3;
}
// 获取学生姓名
string getName() {
return name;
}
// 获取学生学号
string getId() {
return id;
}
// 获取学生三门课程的成绩
float getScore1() {
return score1;
}
float getScore2() {
return score2;
}
float getScore3() {
return score3;
}
private:
string name; // 学生姓名
string id; // 学生学号
float score1; // 第一门课程成绩
float score2; // 第二门课程成绩
float score3; // 第三门课程成绩
};
```
这样,我们就定义了一个名为`Student`的类,它包含了学生姓名、学号和三门课程成绩的信息,并且提供了相应的接口函数来获取这些信息。在实际使用时,我们可以创建学生对象,然后调用相应的函数来获取学生的信息。例如:
```c++
// 创建一个学生对象
Student stu("Tom", "20210001", 89.5, 78.0, 93.4);
// 获取学生姓名、学号和三门课程的成绩
cout << "Name: " << stu.getName() << endl;
cout << "ID: " << stu.getId() << endl;
cout << "Score1: " << stu.getScore1() << endl;
cout << "Score2: " << stu.getScore2() << endl;
cout << "Score3: " << stu.getScore3() << endl;
```
输出结果为:
```
Name: Tom
ID: 20210001
Score1: 89.5
Score2: 78
Score3: 93.4
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](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)
![](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)
![](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)