* 工、定义完全封装的学生类, * 2、键盘输入三个学生,与序号1,2,3有一一对应关系; * 3、输出序号与对应学生的姓名,学号,坐别
时间: 2023-11-28 14:48:44 浏览: 29
以下是完全封装的学生类的代码示例:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
string id;
string address;
public:
void setName(string name) {
this->name = name;
}
void setId(string id) {
this->id = id;
}
void setAddress(string address) {
this->address = address;
}
string getName() const {
return name;
}
string getId() const {
return id;
}
string getAddress() const {
return address;
}
};
```
接下来,我们可以通过键盘输入三个学生的信息,并将其与序号1,2,3一一对应:
```cpp
int main() {
Student s[3];
for (int i = 0; i < 3; i++) {
cout << "请输入第" << i+1 << "个学生的姓名,学号和地址:" << endl;
string name, id, address;
cin >> name >> id >> address;
s[i].setName(name);
s[i].setId(id);
s[i].setAddress(address);
}
cout << "序号\t姓名\t学号\t地址" << endl;
for (int i = 0; i < 3; i++) {
cout << i+1 << "\t" << s[i].getName() << "\t" << s[i].getId() << "\t" << s[i].getAddress() << endl;
}
return 0;
}
```
以上代码会输出每个学生的序号、姓名、学号和地址。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)