当前位置: 首页 > 工具软件 > 2people > 使用案例 >

实验3.3 设计一个用于人事管理的People(人员)类

东方英豪
2023-12-01

题目

(选做)设计一个用于人事管理的People(人员)类。考虑到通用性,这里只抽象出所有类型人员都具有的属性:number(编号)、sex(性别)、birthday(出生日期)、id(身份证号)等等。其中“出生日期”定义为一个“日期”类内嵌子对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数和析构函数、拷贝构造函数、内联成员函数、聚集。

AC的C++代码如下:

#include<iostream> 
using namespace std; 

class date{ 
public: 
	date(int x=0,int y=0,int z=0) 
	{ 
	year=x;month=y;day=z; 
	} 
	date(date&d); 
	void setdate(); 
	void showdate(); 
private: 
	int year,month,day; 
}; 

date::date(date&d){ 
	year=d.year; 
	month=d.month; 
	day=d.day; 
} 

void date::setdate(){ 
	int a,b,c; cout<<"输入日期 "; 
	cin>>a>>b>>c; 
	year=a; 
	month=b; 
	day=c; 
} 

void date::showdate(){ cout<<year<<" 年"<<month<<" 月"<<day<<" 日"<<endl; } 

class people{ 
public: 
	people(date bd,long int nb,long int idnb,char sx); 
	people(people&p); 
	void setpeople(); 
	void showpeople(); 
private: 
	int number,idnumber; 
	char sex; 
	date birthday; 
}; 

people::people(date bd,long int nb=0,long int idnb=0,char sx='m'):birthday(bd)
{ 
	birthday=bd; 
	number=nb; 
	idnumber=idnb; 
	sex=sx; 
} 

people::people(people&p):birthday(p.birthday)
{ 
	number=p.number; 
	birthday=p.birthday; 
	sex=p.sex; 
	idnumber=p.idnumber; 
} 

void people::setpeople()
{ 
	date a; 
	long int b,c; 
	char d; 
	a.setdate(); cout<<"号码 :"; 
	cin>>b; 
	cout<<"id:"; 
	cin>>c; 
	cout<<"性别 :"; 
	cin>>d; 
	cout<<endl; 
	birthday=a; 
	number=b; 
	idnumber=c; 
	sex=d; 
} 
void people::showpeople()
{ 
	cout<<"number"<<number<<endl; 
	cout<<"idnumber"<<idnumber<<endl; 
	cout<<"sex"<<sex<<endl; 
	cout<<"birthday"; 
	birthday.showdate(); 
} 

int main() 
{ 
	date b; 
	b.setdate(); 
	people p1(b); 
	p1.setpeople(); 
	people p2(p1); 
	p1.showpeople(); 
	return 0; 
}
 类似资料: