4、设计一个用于人事管理的People(人员)类。考虑到通用性,这里只抽象出所有类型人员都具有的属性:number(编号)、sex(性别)、birthday(出生日期)、id(身份证号)等等。其中"出生日期"定义为一个"日期"类内嵌子对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数和析构函数、拷贝构造函数、内联成员函数、带缺省形参值的成员函数。
时间: 2023-02-05 16:50:35 浏览: 121
以下是示例代码:
```
#include <iostream>
#include <string>
class Date {
public:
int year;
int month;
int day;
Date(int year, int month, int day) : year(year), month(month), day(day) {}
};
class People {
public:
People(int number, std::string sex, Date birthday, std::string id) :
number(number), sex(sex), birthday(birthday), id(id) {}
People(const People &other) :
number(other.number), sex(other.sex), birthday(other.birthday), id(other.id) {}
~People() {}
inline void input() {
std::cout << "请输入编号:";
std::cin >> number;
std::cout << "请输入性别:";
std::cin >> sex;
std::cout << "请输入出生日期(年 月 日):";
std::cin >> birthday.year >> birthday.month >> birthday.day;
std::cout << "请输入身份证号:";
std::cin >> id;
}
void display(int age) const {
std::cout << "编号:" << number << std::endl;
std::cout << "性别:" << sex << std::endl;
std::cout << "出生日期:" << birthday.year << "年" << birthday.month << "月" << birthday.day << "日" << std::endl;
std::cout << "身份证号:" << id << std::endl;
std::cout << "年龄:" << age << std::endl;
}
private:
int number;
std::string sex;
Date birthday;
std::string id;
};
int main() {
People p1(1, "男", Date(2000, 1, 1), "123456");
p1.display(23);
People p2(p1);
p2.display(23);
People p3;
p3.input();
p3.display(20);
return 0;
}
```
在这个示例代码中,我们设计了一个`People`类,其中包含了编号、性别、出生日期(使用日期类)和身份证号等信息。我们使用了构造函数、析
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![xlsm](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)