题目:设计一个用于人事管理的“人员”类。由于考虑到通用性,这里只抽象出所有类型人员都具有的属性:编号、姓名、性别、出生日期、身份证号等。其中“出生日期”定义为一个“日期”类内嵌子对象。用成员函数实现对人员信息的初始化和显示。要求包括: 构造函数和析构函数、复制构造函数、内联成员函数、带默认形参值的成员函数、类的组合。
这是我的代码:
#include <iostream>
using namespace std;
class date{
public:
date(int y1,int m1,int d1)
{
y=y1;
m=m1;
d=d1;
}
void show(){
cout<< y <<"-"<< m <<"-"<< d ;
}
~date(){}
private:
int y,m,d;
};
class person{
public:
person( int N,string N1,string S,date B,string N2){
No=N;
name=N1;
sex=S;
birth=B;
nameNo=N2;
}
person(const person & p) {
No=p.No;
name=p.name;
birth=p.birth;//
nameNo=p.nameNo;
}
void show(){
cout<<"编号"<< No <<endl;
cout << "姓名: " << name << endl;
cout << "性别: " << sex << endl;
cout<<"出生日期:";
birth.show(); //1094行
cout << "身份证号: " << nameNo << endl;
}
~person(){}
private:
int No;
string name;
string sex;
date birth;
string nameNo;
};
int main()
{
date birth(2000, 1, 1);
person man(1,"gaoshou","man",birth,"11451");
man.show();
person man1(man);
man1.show();
system("pause");
return 0;
}
不知道问题出在哪?
date 未定义无参构造函数
改为下面的代码可以正在运行
#include <iostream>
using namespace std;
class date{
public:
date(int y1,int m1,int d1)
{
y=y1;
m=m1;
d=d1;
}
void show(){
cout<< y <<"-"<< m <<"-"<< d ;
}
~date(){}
private:
int y,m,d;
};
class person{
public:
person(int N,string N1,string S,date* B,string N2){
No=N;
name=N1;
sex=S;
birth=B;
nameNo=N2;
}
person(const person & p) {
No=p.No;
name=p.name;
birth=p.birth;//
nameNo=p.nameNo;
}
void show(){
cout<<"编号"<< No <<endl;
cout << "姓名: " << name << endl;
cout << "性别: " << sex << endl;
cout<<"出生日期:";
birth->show(); //1094行
cout << "身份证号: " << nameNo << endl;
}
~person(){}
private:
int No;
string name;
string sex;
date* birth;
string nameNo;
};
int main()
{
date birth(2000, 1, 1);
person man(1,"gaoshou","man",&birth,"11451");
man.show();
person man1(man);
man1.show();
system("pause");
return 0;
}
试试去掉date()前面的