设计一个Student类,它包含三个私有数据成员:int num、string name、char sex;两个公有的成员函数:set()和display(),分别用来设置一个学生的信息和显示该学生的信息。编写类和主函数进行验证。要求成员函数在类外实现。
时间: 2023-04-10 15:04:37 浏览: 168
好的,我可以回答这个问题。以下是完整的代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
int num;
string name;
char sex;
public:
void set(int n, string na, char s) {
num = n;
name = na;
sex = s;
}
void display() {
cout << "学号:" << num << endl;
cout << "姓名:" << name << endl;
cout << "性别:" << sex << endl;
}
};
int main() {
Student stu;
stu.set(1001, "张三", 'M');
stu.display();
return 0;
}
```
这个程序定义了一个名为 Student 的类,它包含三个私有数据成员:num、name 和 sex,以及两个公有成员函数:set() 和 display()。set() 函数用于设置学生的信息,display() 函数用于显示学生的信息。
在主函数中,我们创建了一个名为 stu 的 Student 对象,并使用 set() 函数设置了它的信息。然后,我们调用 display() 函数来显示该学生的信息。
这个程序的输出应该是:
```
学号:1001
姓名:张三
性别:M
```
希望这个回答能够帮助到你!
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)