学校的人事部门保存了有关学生的部分数据(学号、姓名、年龄、住址),教务部门也保存了学生的另外一些数据(学号、姓名、年龄、成绩),两个部门分别编写了本部门的学生数据管理程序,其中都用了Student作为

学校的人事部门保存了有关学生的部分数据(学号、姓名、年龄、住址),教务部门也保存了学生的另外一些数据(学号、姓名、年龄、成绩),两个部门分别编写了本部门的学生数据管理程序,其中都用了Student作为类名。现在要求在全校的学生数据管理程序中调用这两个部门的学生数据,分别输出两种内容的学生数据。要求用C++编程,使用命名空间。

方法一:两个头文件,一个源文件
(1)头文件1:header1.h

#include<iostream>
#include<string>
using namespace std;

namespace student1
{
	class Student
	{
		public:
			Student(int n,string nam,int a,string addr)
			{
				num=n;
				name=nam;
				age=a;
				address=addr;
			}
			void show_data();
		private:
			int num;
			string name;
			int age;
			string address;
	};
	
	void Student::show_data()
	{
		cout<<"num:"<<num<<" name:"<<name<<" age:"<<age<<" address:"<<address<<endl;
	}		
}

(2)头文件2:header2.h

#include<iostream>
#include<string>
using namespace std;

namespace student2
{
	class Student
	{
		public:
			Student(int n,string nam,char s,float sco)
			{
				num=n;
				name=nam;
				sex=s;
				score=sco;
			}
			void show_data();
		private:
			int num;
			string name;
			char sex;
			float score;
	};
	void Student::show_data()
	{
		cout<<"num:"<<num<<" name:"<<name<<" sex:"<<sex<<" score:"<<score<<endl;
	}
}

(3)源文件:main.cpp

#include<iostream>
using namespace std;
#include"header1.h"
#include"header2.h"

using namespace student1;

int main()
{
	Student stud1(1001,"Wang",18,"123 Beijing Road,Shanghai");
	stud1.show_data();
	student2::Student stud2(1102,"Li",'f',89.5);
	stud2.show_data();
	return 0;
}

方法二:一个完整的源程序文件

#include<iostream>
#include<string>
using namespace std;

namespace student1
{
	class Student
	{
		public:
			Student(int n,string nam,int a,string addr)
			{
				num=n;
				name=nam;
				age=a;
				address=addr;
			}
			void show_data();
		private:
			int num;
			string name;
			int age;
			string address;
	};
	
	void Student::show_data()
	{
		cout<<"num:"<<num<<" name:"<<name<<" age:"<<age<<" address:"<<address<<endl;
	}		
}

namespace student2
{
	class Student
	{
		public:
			Student(int n,string nam,char s,float sco)
			{
				num=n;
				name=nam;
				sex=s;
				score=sco;
			}
			void show_data();
		private:
			int num;
			string name;
			char sex;
			float score;
	};
	void Student::show_data()
	{
		cout<<"num:"<<num<<" name:"<<name<<" sex:"<<sex<<" score:"<<score<<endl;
	}
}

using namespace student1;

int main()
{
	Student stud1(1001,"Wang",18,"123 Beijing Road,Shanghai");
	stud1.show_data();
	student2::Student stud2(1102,"Li",'f',89.5);
	stud2.show_data();
	return 0;
}
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解决这个问题。下面是我的实现代码: ```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` 函数中,我们分别创建了一个人事部门学生对象和一个教务部门学生对象,并且分别输出了它们的数据。通过使用命名空间,我们可以避免不同部门的类名冲突的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值