c++ 学校的人事部门保存了有关学生的部分数(学号、姓名,年的、住证),教务部门 也保存了学生的另外一一些部分数据(学号姓名、性别、成绩),两个部门分到绵写了本部 门的学生数据管理程序,其中都用了Student作为类名、现在要求在全校的学生数据管理 程序中调用这两个部门的学生数据,分别输出两种内容的学生数据。要求用ASIC+编 程,使用命名空间。
时间: 2024-03-19 18:41:03 浏览: 15
好的,这是一个比较典型的设计多个类的程序,可以定义两个类分别用于表示人事部门和教务部门的学生数据。具体实现可以参考以下代码:
// student.h
#include <string>
namespace HR {
class Student {
public:
Student(int id, const std::string& name, int age, const std::string& address);
int getId() const;
const std::string& getName() const;
int getAge() const;
const std::string& getAddress() const;
private:
int id_;
std::string name_;
int age_;
std::string address_;
};
}
namespace Education {
class Student {
public:
Student(int id, const std::string& name, char gender, double score);
int getId() const;
const std::string& getName() const;
char getGender() const;
double getScore() const;
private:
int id_;
std::string name_;
char gender_;
double score_;
};
}
// student.cpp
#include "student.h"
namespace HR {
Student::Student(int id, const std::string& name, int age, const std::string& address)
: id_(id), name_(name), age_(age), address_(address) {}
int Student::getId() const {
return id_;
}
const std::string& Student::getName() const {
return name_;
}
int Student::getAge() const {
return age_;
}
const std::string& Student::getAddress() const {
return address_;
}
}
namespace Education {
Student::Student(int id, const std::string& name, char gender, double score)
: id_(id), name_(name), gender_(gender), score_(score) {}
int Student::getId() const {
return id_;
}
const std::string& Student::getName() const {
return name_;
}
char Student::getGender() const {
return gender_;
}
double Student::getScore() const {
return score_;
}
}
// main.cpp
#include "student.h"
#include <iostream>
#include <vector>
int main() {
// HR department data
HR::Student s1(1001, "Tom", 20, "Beijing");
HR::Student s2(1002, "Jack", 21, "Shanghai");
HR::Student s3(1003, "Lucy", 19, "Guangzhou");
// Education department data
Education::Student s4(1001, "Tom", 'M', 90.5);
Education::Student s5(1002, "Jack", 'M', 85.0);
Education::Student s6(1003, "Lucy", 'F', 92.5);
// output HR department data
std::cout << "HR department data:" << std::endl;
std::vector<HR::Student> hr_students = {s1, s2, s3};
for (const auto& s : hr_students) {
std::cout << "id: " << s.getId() << ", name: " << s.getName() << ", age: " << s.getAge()
<< ", address: " << s.getAddress() << std::endl;
}
// output Education department data
std::cout << "Education department data:" << std::endl;
std::vector<Education::Student> edu_students = {s4, s5, s6};
for (const auto& s : edu_students) {
std::cout << "id: " << s.getId() << ", name: " << s.getName() << ", gender: " << s.getGender()
<< ", score: " << s.getScore() << std::endl;
}
return 0;
}
在上述代码中,我们定义了两个类HR::Student和Education::Student,分别用于表示人事部门和教务部门的学生数据。这两个类都定义了一些成员变量和成员函数,用于存储和访问学生数据。同时,我们还定义了一个main函数,用于演示如何在全校的学生数据管理程序中调用这两个部门的学生数据,分别输出两种内容的学生数据。最后,我们使用命名空间来避免类名冲突。