用c++设计一个用于人事管理的 People(人员)类。考虑到通用性,这里只抽象出所有类型人员都具有的属性:number(编号)、sex(性别)、birthday(出生日期)、id(身份证号)等。其中"出生日期"定义为一个"日期"类内嵌子对象。用成员函数实现对人员信息的录入和显示。 要求包括:构造函数和析构函数、拷贝构造函数、内联成员函数、带缺省形参值的成员函数。
时间: 2023-12-16 22:06:22 浏览: 27
以下是一个简单的 People 类的实现:
```c++
#include <iostream>
#include <string>
using namespace std;
class Date {
public:
Date(int y = 1970, int m = 1, int d = 1) : year(y), month(m), day(d) {}
void display() const {
cout << year << "-" << month << "-" << day;
}
private:
int year, month, day;
};
class People {
public:
People(int num = 0, char s = 'M', const Date& b = Date(), const string& i = "") :
number(num), sex(s), birthday(b), id(i) {}
People(const People& other) : number(other.number), sex(other.sex), birthday(other.birthday), id(other.id) {}
~People() {}
inline void setNumber(int num) { number = num; }
inline void setSex(char s) { sex = s; }
inline void setBirthday(const Date& b) { birthday = b; }
inline void setId(const string& i) { id = i; }
void display() const {
cout << "Number: " << number << endl;
cout << "Sex: " << sex << endl;
cout << "Birthday: ";
birthday.display();
cout << endl;
cout << "ID: " << id << endl;
}
private:
int number;
char sex;
Date birthday;
string id;
};
int main() {
People p(1, 'M', Date(1990, 1, 1), "123456789");
p.display();
People p2(p);
p2.display();
p2.setNumber(2);
p2.setSex('F');
p2.setBirthday(Date(1995, 2, 2));
p2.setId("987654321");
p2.display();
return 0;
}
```
在上面的实现中,我们定义了两个类:People 和 Date。其中 Date 类用于记录日期信息,而 People 类则是整个人员信息的抽象。People 类的构造函数、析构函数、拷贝构造函数和成员函数 display() 都比较简单,不再赘述。需要注意的是,setNumber()、setSex()、setBirthday() 和 setId() 函数都是内联函数。此外,我们还在 main 函数中演示了如何创建对象、调用拷贝构造函数、修改对象的属性以及调用 display() 函数。