设计一个用于人事管理的 People(人员)类。考虑到通用性,这里只抽象出所有
类型 人员都具有的属性:number(编号)、sex(性别)、birthday(出生日期)、id(身份
证号)等。
其中"出生日期"定义为一个"日期"类内嵌子对象。
用成员函数实现对人员信息 的录入和显示。
要求包括:构造函数和析构函数、拷贝构造函数、内联成员函数、带缺省形参值的成员函数
#include<iostream>
#include<string>
using namespace std;
class Date {
public:
Date(int newY, int newM, int newD);
Date() {
year = 2000;
mouth = 0;
day = 0;
};
Date(Date& p);
~Date(){}
void setdate(int newY, int newM,int newD);
void showdate();
private:
int year, mouth, day;
};
Date::Date(int newY, int newM, int newD) {
year=newY ;//!!!!!!!!!!!!注意这里的顺序,参数在右边
mouth= newM;
day= newD;
}
Date::Date(Date& p) {
year = p.year;
mouth = p.mouth;
day = p.mouth;
}
void Date::setdate(int newY, int newM,int newD) {
year = newY;
mouth = newM;
day = newD;
}
void Date::showdate() {
cout << year << " " << mouth << " " << day<<endl;
}
//注意身份证号用字符串类型
class People {
public:
People(int nb, char s, Date bd, string idnb);
People() {
number = 0;
sex = 'm';
Date x(2000, 0, 0);
birthday = x;
id = "";
};
People(People& p);
~People(){}
void setifm();
void showifm();
private:
int number; char sex; Date birthday; string id;
};
//people::people(date bd,long int nb=0,long int idnb=0,char sx='m'):birthday(bd)
People::People(int nb, char s, Date bd, string idnb) :birthday(bd) { //bd是birthday的形参,birthday才是类的数据成员
nb = number;
s = sex;
bd = birthday;
idnb = id;
}
//people::people(people&p):birthday(p.birthday)
People::People(People& p):birthday(p.birthday){
number = p.number;
sex = p.sex;
birthday = p.birthday;
id = p.id;
}
void People::setifm() {
cout << "请输入编号:";
int nb;
cin >> nb;
cout << "请输入性别:";
char s;
cin >> s;
cout << "请输入出生日期(形如:2000 4 22):";
int a, b, c;
cin >> a >> b >> c;
Date bd(a,b,c);//!!!!!!!!!!!!1这里直接Date bd(a,b,c) 和bd.setdate(a,b,c)都可
cout << "请输入身份证号:";
string idnb;
cin >> idnb;
number = nb;
sex = s;
birthday = bd;
id = idnb;
}
//这里要调用date的成员函数
void People::showifm() {
cout << "编号:" << number<<endl;
cout << "性别;" << sex << endl;
cout << "出生日期:";
birthday.showdate();
cout << "身份证号:" << id;
}
//不能直接读取一个类的对象
int main() {
People zhangsan;
zhangsan.setifm();
zhangsan.showifm();
}