设计一个用于人事管理的“人员”类。由于考虑到通用性,这里只抽象出所有类型人员有具有的属性:姓名,性别,出生日期等。其中“出生日期”声明为一个“日期”类的内嵌子对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数和析构函数、复制构造函数、内联成员函数、带默认形参值的成员函数、类的组合。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#include<iostream>
using
namespace
std;
class
Time{
public
:
Time(
int
Year,
int
Month,
int
Day){
year=Year;month=Month;day=Day;
}
//构造函数
Time(Time &p){
year=p.year;month=p.month;day=p.day;
}
//复制构造函数
int
Gettime(){
cout<<year<<
"年 "
<<month<<
"月 "
<<day<<
"日 "
<<endl;
}
//返回出生日期
~Time(){
cout<<
"~Time"
<<endl;
}
//析构函数
private
:
int
year,month,day;
};
class
Renyuan{
public
:
Renyuan(Time a,
char
xingming[],
int
xingbie):riqi(a){
for
(
int
i=0;i<10;i++)
name[i]=xingming[i];
sex=xingbie;
}
int
Getxinxi(){
cout<<
"姓名:"
;
for
(
int
i=0;i<10;i++)
cout<<name[i];
cout<<endl;
cout<<
"性别:"
;
if
(sex==1)
//sex为1,表示是男性
cout<<
"男"
<<endl;
else
if
(sex==0)
//为0,表示是女性
cout<<
"女"
<<endl;
cout<<
"出生日期:"
;
riqi.Gettime();
}
~Renyuan(){
cout<<
"~Renyuan"
<<endl;
}
private
:
Time riqi;
char
name[10];
int
sex;
};
int
main(){
char
xingming[10]={
'x'
,
'i'
,
'a'
,
'o'
,
'z'
,
'h'
,
'a'
,
'n'
,
'g'
};
Time xiaozhang(1980,2,3);
Renyuan zhang(xiaozhang,xingming,1);
zhang.Getxinxi();
return
0;
}
|
本文转自 pangfc 51CTO博客,原文链接:http://blog.51cto.com/983836259/1338329,如需转载请自行联系原作者