设计一个用于人事管理的People(人员)类.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include <iostream>
  2. #include <cstring>
  3. class Date {
  4. public:
  5. int year, month, day;
  6. Date(int y = 0, int m = 0, int d = 0) : year(y), month(m), day(d) {}
  7. void input() {
  8. std::cin >> year >> month >> day;
  9. }
  10. void display() const {
  11. std::cout << year << "年" << month << "月" << day << "日";
  12. }
  13. };
  14. class People {
  15. private:
  16. char name[11];
  17. char number[7];
  18. char sex[3];
  19. Date birthday;
  20. char id[19];
  21. public:
  22. People() {
  23. std::strcpy(name, "");
  24. std::strcpy(number, "");
  25. std::strcpy(sex, "");
  26. std::strcpy(id, "");
  27. }
  28. People(const People& other) {
  29. std::strcpy(name, other.name);
  30. std::strcpy(number, other.number);
  31. std::strcpy(sex, other.sex);
  32. birthday = other.birthday;
  33. std::strcpy(id, other.id);
  34. }
  35. void input() {
  36. std::cout << "姓名:";
  37. std::cin >> name;
  38. std::cout << "编号:";
  39. std::cin >> number;
  40. std::cout << "性别(男/女):";
  41. std::cin >> sex;
  42. std::cout << "出生日期(年 月 日):";
  43. birthday.input();
  44. std::cout << "身份证号:";
  45. std::cin >> id;
  46. }
  47. void display() const {
  48. std::cout << "\n姓名:" << name << std::endl;
  49. std::cout << "编号:" << number << std::endl;
  50. std::cout << "性别:" << sex << std::endl;
  51. std::cout << "出生日期:";
  52. birthday.display();
  53. std::cout << std::endl;
  54. std::cout << "身份证号:" << id << std::endl;
  55. }
  56. ~People() {}
  57. };
  58. int main() {
  59. int num;
  60. std::cout << "员工人数:";
  61. std::cin >> num;
  62. People* peopleArray = new People[num];
  63. for (int i = 0; i < num; ++i) {
  64. peopleArray[i].input();
  65. }
  66. People firstCopy = peopleArray[0];
  67. firstCopy.display();
  68. for (int i = 0; i < num; ++i) {
  69. peopleArray[i].display();
  70. }
  71. delete[] peopleArray;
  72. return 0;
  73. }