65,211
社区成员
发帖
与我相关
我的任务
分享
//employee.h
#include <iostream>
class Date
{
public:
Date(int NewY=0,int NewM=0,int NewD=0);//构造函数
~Date(); //析构函数
void Setbirthday(int NewY,int NewM,int NewD);
void Showbirthday();
int GetYear() {return Year;}//隐式的构造函数 //这里只是普通的成员函数,不是什么隐式的构造函数
int GetMonth() {return Month ;}
int GetDay() {return Day;}
Date & operator = (const Date &date); //这里要加一个赋值函数
private:
int Year ,Month, Day;
};
class employee
{
public:
employee();
employee(const employee &obj); //拷贝构造函数
~employee();
void SetindividualEmpNO(int EmpNO); //函数命名要规范
void SetSex(char sex);
void SetIDcardNO(char *IDcardNO);
void SetBirthday(Date birthday);
int GetindividualEmpNO();
char Getsex();
char *GetIDcardNO();
Date GetBirthday();
protected:
int individualEmpNO;
char sex;
char IDcardNO[20];
Date birthday;
};
//employee.cpp
#include "employee.h"
using namespace std;
//函数成员的实现
Date::Date(int NewY,int NewM,int NewD)
{
Year = NewY;
Month = NewM;
Day = NewD;
}
Date::~Date()
{
}
void Date::Setbirthday(int NewY,int NewM,int NewD)
{
Year = NewY;
Month = NewM;
Day = NewD;
}
void Date::Showbirthday()
{
cout<<Year<<" "<<Month<<" "<<Day;
}
//赋值函数
Date & Date::operator = (const Date &date)
{
this->Year = date.Year;
this->Month = date.Month;
this->Day = date.Day;
return *this;
}
//如何实现将‘出生日期’声明为一个‘日期’类的内嵌子对象进而实现类的组合
employee::employee()
{
individualEmpNO = 0000;
sex = 0;
//birthday(0,0,0);
strncpy(IDcardNO, "000000", sizeof(IDcardNO)/sizeof(IDcardNO[0])); //员工的身份证号如何实现初始化
}
employee::~employee()
{
}
//拷贝构造函数又如何实现
employee::employee(const employee &obj)
{
this->individualEmpNO = obj.individualEmpNO;
this->sex = obj.sex;
this->birthday = obj.birthday;
strncpy(this->IDcardNO, obj.IDcardNO, sizeof(obj.IDcardNO)/sizeof(obj.IDcardNO[0]));
}
void employee::SetindividualEmpNO(int EmpNO)
{
this->individualEmpNO = EmpNO;
}
void employee::SetSex(char sex)
{
this->sex = sex;
}
void employee::SetIDcardNO(char *IDcardNO)
{
if(IDcardNO == NULL)
return;
int length = strlen(IDcardNO);
if((length + 1) > 20)
return;
strcpy(this->IDcardNO, IDcardNO);
}
void employee::SetBirthday(Date birthday)
{
this->birthday = birthday;
}
int employee::GetindividualEmpNO()
{
return this->individualEmpNO;
}
char employee::Getsex()
{
return this->sex;
}
char *employee::GetIDcardNO()
{
return this->IDcardNO;
}
Date employee::GetBirthday()
{
return this->birthday;
}
//主程序
#include <iostream>
#include "employee.h"
using namespace std;
void InputMessage(employee &e);
void OutputMessage(employee &e);
int main ()
{
employee A;
employee B;
employee C;
employee D;
InputMessage(A);
OutputMessage(A);
system("pause");
return 0;
}
//这里输入员工信息
void InputMessage(employee &e)
{
cout<<"输入员工的信息"<<endl;
cout<<"请输入编号:";
int EmpNO;
cin>>EmpNO;
cout<<endl;
cout<<"请输入性别(0 男,1 女):";
char sex;
cin>>sex;
cout<<endl;
int year,month,day;
cout<<"请输入生日:"<<endl;
cout<<"年份:";
cin>>year;
cout<<"月份:";
cin>>month;
cout<<"天:";
cin>>day;
Date date(year, month, day);
cout<<"请输入身份证号:";
char *IDNO = new char[20];
cin>>IDNO;
e.SetindividualEmpNO(EmpNO);
e.SetSex(sex);
e.SetBirthday(date);
e.SetIDcardNO(IDNO);
delete [] IDNO;
}
//这里输出员工信息
void OutputMessage(employee &e)
{
cout<<endl;
cout<<"编号"<<"\t"<<"性别"<<"\t"<<"出生日期"<<"\t"<<"身份证号"<<endl;
cout<<e.GetindividualEmpNO()<<"\t"<<e.Getsex()<<"\t";
e.GetBirthday().Showbirthday();
cout<<"\t"<<e.GetIDcardNO()<<endl;
}