设计一个用于人事管理的(人员)类。考虑到通用性,这里只抽象出所有类型人员都具有的属性:编号、性别、姓名、出生日期、身份证号等。其中“出生日期”声明为一个“日期”类内嵌子对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数和析构函数、复制构造函数、内联成员函数、带默认形参值得成员函数,类的组合。并写一个与之相匹配的main主函数,在每一段代码后进行注释
时间: 2023-11-22 09:52:41 浏览: 80
以下是一个实现人事管理的人员类的代码:
```
#include <iostream>
#include <string>
using namespace std;
class Date {
public:
Date() : year(0), month(0), day(0) {}
Date(int y, int m, int d) : year(y), month(m), day(d) {}
void setYear(int y) { year = y; }
void setMonth(int m) { month = m; }
void setDay(int d) { day = d; }
int getYear() const { return year; }
int getMonth() const { return month; }
int getDay() const { return day; }
private:
int year;
int month;
int day;
};
class Person {
public:
Person() : id(0), sex(""), name(""), idNum(""), birth(Date()) {}
Person(int i, string s, string n, string idN, Date b) : id(i), sex(s), name(n), idNum(idN), birth(b) {}
Person(const Person &p) : id(p.id), sex(p.sex), name(p.name), idNum(p.idNum), birth(p.birth) {}
void setId(int i) { id = i; }
void setSex(string s) { sex = s; }
void setName(string n) { name = n; }
void setIdNum(string idN) { idNum = idN; }
void setBirth(Date b) { birth = b; }
int getId() const { return id; }
string getSex() const { return sex; }
string getName() const { return name; }
string getIdNum() const { return idNum; }
Date getBirth() const { return birth; }
void inputInfo();
void displayInfo() const;
private:
int id;
string sex;
string name;
string idNum;
Date birth;
};
inline void Person::inputInfo() {
cout << "Please input the ID of the person: ";
cin >> id;
cout << "Please input the sex of the person: ";
cin >> sex;
cout << "Please input the name of the person: ";
cin >> name;
cout << "Please input the ID number of the person: ";
cin >> idNum;
cout << "Please input the birth year of the person: ";
int y;
cin >> y;
birth.setYear(y);
cout << "Please input the birth month of the person: ";
int m;
cin >> m;
birth.setMonth(m);
cout << "Please input the birth day of the person: ";
int d;
cin >> d;
birth.setDay(d);
}
inline void Person::displayInfo() const {
cout << "ID: " << id << endl;
cout << "Sex: " << sex << endl;
cout << "Name: " << name << endl;
cout << "ID number: " << idNum << endl;
cout << "Birth date: " << birth.getYear() << "-" << birth.getMonth() << "-" << birth.getDay() << endl;
}
int main() {
Person p1;
p1.inputInfo();
p1.displayInfo();
return 0;
}
```
这个程序定义了一个`Date`类和一个`Person`类,其中`Person`类包含了一个`Date`类的对象作为其属性。`Person`类包括了人员编号、性别、姓名、出生日期、身份证号等属性,同时也包括了对这些属性进行设置和获取的成员函数,以及对人员信息进行录入和显示的成员函数。
在main函数中,我们创建了一个`Person`类的对象`p1`,调用该对象的成员函数`inputInfo`和`displayInfo`分别对`p1`的属性进行了录入和显示。
这个程序中还包括了复制构造函数、内联成员函数、带默认形参值的成员函数和类的组合等特性。
相关推荐
![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)
![](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)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)