设计一个用于人事管理的People(人员)类。考虑到通用性,只抽象出所有类型人员都具有的属性:number(编号)、sex(性别)、birthday(出生日期)、id(身份证号)、ntotalnumber(人员总数)。其中“人员总数”用静态成员变量表示,并用一个静态成员函数输出总人数;“出生日期”声明为一个“日期类(calendar)”成员对象,作为People(人员)类的封闭类对象。用成员函数实现对人信息的录入和显示。要求包括:构造函数和析构函数、复制构造函数、内敛成员函数、静态成员函数。
时间: 2023-05-28 09:01:58 浏览: 70
class Calendar {
private:
int year, month, day;
public:
Calendar(int y = 0, int m = 0, int d = 0) : year(y), month(m), day(d) {}
void display() {
cout << year << "-" << month << "-" << day << endl;
}
};
class People {
private:
int number;
char sex;
char id[19];
Calendar birthday;
static int ntotalnumber;
public:
People(int num = 0, char s = ' ', char* i = "", int y = 0, int m = 0, int d = 0)
: number(num), sex(s), birthday(y, m, d) {
strcpy(id, i);
ntotalnumber++;
}
People(const People& p) : number(p.number), sex(p.sex), birthday(p.birthday) {
strcpy(id, p.id);
ntotalnumber++;
}
~People() {
ntotalnumber--;
}
inline void display() {
cout << "Number:" << number << endl
<< "Sex:" << sex << endl
<< "ID:" << id << endl
<< "Birthday:";
birthday.display();
}
static void displayTotalNumber() {
cout << "Total Number:" << ntotalnumber << endl;
}
};
int People::ntotalnumber = 0;
int main() {
People::displayTotalNumber();
People p1(1, 'M', "123456789012345678", 1990, 1, 1);
People::displayTotalNumber();
p1.display();
People p2(p1);
People::displayTotalNumber();
p2.display();
return 0;
}
阅读全文