学校的人事部门保存了有关学生的部分数据(学号、姓名、年龄、住址),教务部门也你存了学生的另外一些部分数据(学号、姓名、性别、成绩),两个部门分别编写了本部门的学生数据管理程序,其中都用了 Student作为类名。现在要求在全校的学生数据管理程序中调用这两个部门的学生数据,分别输出两种内容的学生数据。要求用 ANSI C++编程,使用命名空间。
时间: 2024-03-18 13:45:47 浏览: 68
好的,我可以帮你解决这个问题。下面是我的实现代码:
```cpp
#include <iostream>
namespace Personnel {
class Student {
public:
int id;
std::string name;
int age;
std::string address;
};
}
namespace Education {
class Student {
public:
int id;
std::string name;
std::string gender;
int score;
};
}
int main() {
// 定义一个人事部门的学生对象
Personnel::Student personnelStudent;
personnelStudent.id = 1;
personnelStudent.name = "张三";
personnelStudent.age = 20;
personnelStudent.address = "北京市";
// 定义一个教务部门的学生对象
Education::Student educationStudent;
educationStudent.id = 1;
educationStudent.name = "张三";
educationStudent.gender = "男";
educationStudent.score = 90;
// 输出人事部门的学生数据
std::cout << "Personnel Department:" << std::endl;
std::cout << "ID: " << personnelStudent.id << std::endl;
std::cout << "Name: " << personnelStudent.name << std::endl;
std::cout << "Age: " << personnelStudent.age << std::endl;
std::cout << "Address: " << personnelStudent.address << std::endl;
// 输出教务部门的学生数据
std::cout << "Education Department:" << std::endl;
std::cout << "ID: " << educationStudent.id << std::endl;
std::cout << "Name: " << educationStudent.name << std::endl;
std::cout << "Gender: " << educationStudent.gender << std::endl;
std::cout << "Score: " << educationStudent.score << std::endl;
return 0;
}
```
在这个程序中,我们使用了命名空间来分别定义了人事部门和教务部门的学生类。在 `main` 函数中,我们分别创建了一个人事部门的学生对象和一个教务部门的学生对象,并且分别输出了它们的数据。通过使用命名空间,我们可以避免不同部门的类名冲突的问题。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![java](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![java](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)