1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 class Date //日期类 6 { 7 private: 8 int year; 9 int month; 10 int day; 11 public: 12 Date(){} //默认构造 13 Date(int y,int m,int d) //带参构造 14 { 15 year=y; 16 month=m; 17 day=d; 18 } 19 void set() //设置数据函数 20 { 21 cin>>year>>month>>day; 22 } 23 void display() //显示函数 24 { 25 cout<<year<<"年"<<month<<"月"<<day<<"日"; 26 } 27 }; 28 29 class Person //人员类 30 { 31 private: 32 int num; 33 char sex; 34 Date birthday; 35 char ID[18]; 36 public: 37 Person(){} //默认构造 38 Person(int n,int y,int m,int d,char id[18],char s=‘m‘):birthday(y,m,d) 39 { 40 num=n; 41 sex=s; 42 strcpy(ID,id); 43 } //有默认值的带参构造 44 Person(Person& p) //拷贝构造 45 { 46 num=p.num; 47 sex=p.sex; 48 birthday=p.birthday; 49 strcpy(ID,p.ID); 50 } 51 void input() //输入函数 52 { 53 cout<<"录入数据:"<<endl; 54 cout<<"编号:"; 55 cin>>num; 56 cout<<"性别(m/f):"; 57 cin>>sex; 58 cout<<"生日:"; 59 birthday.set(); 60 cout<<"身份证号:"; 61 cin>>ID; 62 ID[18]=‘\0‘; 63 cout<<endl; 64 } 65 void output() //输出函数 66 { 67 cout<<"编号:"<<num<<endl; 68 cout<<"性别:"<<sex<<endl; 69 cout<<"生日:"; 70 birthday.display(); 71 cout<<endl; 72 cout<<"身份证号:"<<ID<<endl; 73 } 74 ~Person() //析构函数 75 { 76 cout<<" "<<num<<"号人员已经录入"<<endl; 77 } 78 }; 79 80 int main() 81 { 82 Person p1; 83 p1.input(); 84 p1.output(); 85 return 0; 86 }
时间: 2024-11-01 13:23:43