C++,帮忙补上一个构造函数
设计一个用语人事管理的“人员”类。由于考虑通用性,这里只抽象出所有类型人员都具有的属性:编号,性别,出生日期,身份证号等。其中“出生日期”声明为一个“日期”类内嵌对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数和析构函数,拷贝构造函数,内联成员函数,带默认形参值的成员函数,类的组合。
我写了一下,就是人员类的构造函数和拷贝构造函数,写不好,如果不写的话我就可以运行,请高手帮忙补上构造函数,小弟谢谢了,要多少分,自己说(只要我给的起)。
#include<iostream>
using namespace std;
class date //出生日期
{
public:
date(int y=1980,int m=1,int d=1){year=y;month=m;day=d;}
date(date &p);
~ date(){}
void setdate();
void outputdate();
private:
int year;
int month;
int day;
};
void date::setdate()
{
int newy;
int newm;
int newd;
cout<<"请输入出生 年 月 日 :";
cin>>newy>>newm>>newd;
year=newy;
month=newm;
day=newd;
}
void date::outputdate ()
{
cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
}
class person//人员类
{
public:
void setinformation();
void showinformation();
private:
long number;
char sex;
date c;
long idno;
};
void person::setinformation()
{
long num;
char s;
long id;
cout<<"请输入员工信息 :"<<"1 编号:"<<endl;
cin>>num;
cout<<"2 性别(男 M,女 F)"<<endl;
cin>>s;
cout<<"3 身份证号"<<endl;
cin>>id;
number=num;
sex=s;
c.setdate ();
idno=id;
}
void person::showinformation()
{
cout<<"编号:"<<number<<endl;
cout<<"性别:"<<sex<<endl;
cout<<"出生日期";
c.outputdate();
cout<<"身份证号"<<idno<<endl;
}
int main()
{person m1;
person m2;
m1.setinformation();
m1.showinformation();
m2.showinformation ();
}
参考答案:估计能满足你
class CDate
{
private:
int m_nYear;
int m_nMonth;
int m_nDay;
public:
CDate(int y=1985,int m=12,int d=13);
void Modify(int ye,int mo,int da);
void disp() const;
};
CDate::CDate(int y/* =1985 */,int m/* =12 */,int d/* =13 */)
{
m_nYear=y;
m_nMonth=m;
m_nDay=d;
}
void CDate::Modify(int ye,int mo,int da)
{
m_nYear=ye;
m_nMonth=mo;
m_nDay=da;
}
void CDate::disp() const
{
cout<<m_nYear<<"年"<<m_nMonth<<"月"<<m_nDay<<"日"<<endl;
}
class CStaff
{
private:
int m_nNum;
char m_bSex;
double m_dID;
CDate m_BirthDay;
public:
CStaff(); //默认构造函数
CStaff(int n,char s,double i, CDate d);
CStaff(const CStaff& staff); //拷贝构造函数
~CStaff(); //析构函数
void show() const;
};
CStaff::CStaff()
{
cout<<"构造函数!"<<endl;
}
CStaff::CStaff(int n, char s,double i,CDate d)
{
m_nNum=n;
m_bSex=s;
m_BirthDay=d;
m_dID=i;
}
CStaff::CStaff(const CStaff& staff)
{
m_nNum=staff.m_nNum;
m_bSex=staff.m_bSex;
m_BirthDay=staff.m_BirthDay;
m_dID=staff.
}
CStaff::~CStaff()
{
cout<<"析构函数!"<<endl;
}
void CStaff::show()const
{
cout<<"编号:"<<m_nNum<<endl;
cout<<"性别:"<<m_bSex<<endl;
cout<<"出生年月:"<<endl;
m_BirthDay.disp();
cout<<"身份证号:"<<m_dID<<endl;
}
void main()
{
int y,m,d,no;
char sex;
double ID;
CDate test;
cout<<"请输入编号,性别,身份证号:\n";
cin>>no>>sex>>ID; //需要:using namespace std;,否则有错!!!
cout<<"请输入出生年月日:\n";
cin>>y>>m>>d;
test.Modify(y,m,d);
CStaff staff(no,sex,ID,test);
staff.show();
}