设计一个用于人事管理的People(人员)类。考虑到通用性,这里只抽象出所有类型人员都具有的属性:number(编号)、sex(性别)、birthday(出生日期)、id(身份证号)等等。其中"出生日期"定义为一个"日期"类内嵌子对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数和析构函数、拷贝构造函数、成员函数、带缺省形参值的成员函数。
时间: 2023-12-17 19:03:01 浏览: 30
以下是一个简单的人事管理的People类的示例代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Date {
public:
Date(int year = 0, int month = 0, int day = 0) : m_year(year), m_month(month), m_day(day) {}
void display() const {
cout << m_year << "-" << m_month << "-" << m_day << endl;
}
private:
int m_year, m_month, m_day;
};
class People {
public:
People(string number = "", char sex = ' ', Date birthday = Date(), string id = "") :
m_number(number), m_sex(sex), m_birthday(birthday), m_id(id) {}
People(const People& other) : m_number(other.m_number), m_sex(other.m_sex), m_birthday(other.m_birthday), m_id(other.m_id) {}
~People() {}
void setNumber(string number) { m_number = number; }
void setSex(char sex) { m_sex = sex; }
void setBirthday(Date birthday) { m_birthday = birthday; }
void setId(string id) { m_id = id; }
void display() const {
cout << "number: " << m_number << endl;
cout << "sex: " << m_sex << endl;
cout << "birthday: ";
m_birthday.display();
cout << "id: " << m_id << endl;
}
private:
string m_number; // 编号
char m_sex; // 性别
Date m_birthday; // 出生日期
string m_id; // 身份证号
};
```
这里定义了一个Date类,它内嵌在People类中,表示出生日期。People类包括了编号、性别、出生日期和身份证号等属性,并定义了相应的成员函数。在构造函数中,可以设置缺省值,以方便使用。在display函数中,调用了内嵌的Date类的display函数,输出出生日期。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.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)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)