//构造一个People类来进行人员管理,要求用上内联,构造,析构,类的组合
#include<iostream>
#include<string>
using namespace std;
class Date{ //定义一个存储出生日期的类
int year,mouth,day;
public :
Date(int y,int m,int d);//构造函数
Date();//默认构造函数
Date(const Date&);//复制构造函数
~Date();//析构函数
void inputData();//输入数据
void showData();//显示数据
};
Date::Date(int y,int m,int d):year(y),mouth(m),day(d){
cout<<"构造了一个Date对象。。。"<<endl;
}
Date::Date():Date(0,0,0){cout<<"构造了一个Date对象。。。"<<endl;}
Date::Date(const Date& p)
{
year=p.year;
mouth=p.mouth;
day=p.day;
cout<<"构造了一个Date对象。。。"<<endl;
}
Date::~Date()
{
cout<<"析构一个Date对象...."<<endl;
}
inline void Date::inputData()
{
cin>>year>>mouth>>day;
}
inline void Date::showData()
{
cout<<year<<"-"<<mouth<<"-"<<day<<endl;
}
class People{ //定义一个People类来进行人员数据储存
string name;
string id;
Date birthday;
int number;
char sex;
public:
People(string name,string id,Date birthday,int number,char sex);//构造函数
~People();//析构函数
People(const People &)//复制构造函数;
void inputData();//输入数据
void showData();//显示数据
};
People::People(string n,string i,Date b,int num,char s)
{
name=n;
id=i;
birthday=b;
number=num;
sex=s;
cout<<"构造了一个People对象。。。。。"<<endl;
}
People::~People()
{
cout<<"析构一个People对象....."<<endl;
}
People::People(const People &p)
{
name=p.name;
id=p.id;
birthday=p.birthday;
number=p.number;
sex=p.sex;
cout<<"构造了一个People对象。。。。。"<<endl;
}
void People::inputData()
{
cout<<"输入员工名字:";
cin>>name;
cout<<"输入员工Id:";
cin>>id;
cout<<"输入员工出生日期:";
birthday.inputData();
cout<<"输入员工性别:";
cin>>sex;
cout<<"输入员工编号:";
cin>>number;
}
void People::showData()
{
cout<<"该员工编号:"<<number<<endl;
cout<<" 姓名:"<<name<<endl;
cout<<" 身份证:"<<id<<endl;
cout<<" 出生日期:";
birthday.showData();
cout<<" 性别:"<<sex<<endl;
}
int main()
{
Date b1;
People people1("name","id",b1,0,'m');
people1.inputData();
people1.showData();
people1.~People;
return 0;
}
构造的和析构的不对等.....找不出来问题....有点烦,标记着,学完后面回来 看看.....
people析构多出来一个是我自己调用了一次.......去掉后正常了...
感谢老师指导,给复制构造函数的输出区别于构造函数,现在清楚了
#include<iostream>
#include<string>
using namespace std;
class Date{
int year,mouth,day;
public :
Date(int y,int m,int d);
Date();
Date(const Date&);
~Date();
void getDate();
void showDate();
};
Date::Date(int y,int m,int d):year(y),mouth(m),day(d){
cout<<"构造了一个Date对象。。。"<<endl;
}
Date::Date():Date(0,0,0){cout<<"构造了一个Date对象。。。"<<endl;}
Date::Date(const Date& p)
{
year=p.year;
mouth=p.mouth;
day=p.day;
cout<<"复制构造了一个Date对象。。。"<<endl;
}
Date::~Date()
{
cout<<"析构一个Date对象...."<<endl;
}
inline void Date::getDate()
{
cin>>year>>mouth>>day;
}
inline void Date::showDate()
{
cout<<year<<"-"<<mouth<<"-"<<day<<endl;
}
class People{
string name;
string id;
Date birthday;
int number;
char sex;
public:
People(string name,string id,Date birthday,int number,char sex);
~People();
People(const People &);
void getDate();
void showDate();
};
People::People(string n,string i,Date b,int num,char s)
{
name=n;
id=i;
birthday=b;
number=num;
sex=s;
cout<<"构造了一个People对象。。。。。"<<endl;
}
People::~People()
{
cout<<"析构一个People对象....."<<endl;
}
People::People(const People &p)
{
name=p.name;
id=p.id;
birthday=p.birthday;
number=p.number;
sex=p.sex;
cout<<"复制构造了一个People对象。。。。。"<<endl;
}
void People::getDate()
{
cout<<"输入员工名字:";
cin>>name;
cout<<"输入员工Id:";
cin>>id;
cout<<"输入员工出生日期:";
birthday.getDate();
cout<<"输入员工性别:";
cin>>sex;
cout<<"输入员工编号:";
cin>>number;
}
void People::showDate()
{
cout<<"该员工编号:"<<number<<endl;
cout<<" 姓名:"<<name<<endl;
cout<<" 身份证:"<<id<<endl;
cout<<" 出生日期:";
birthday.showDate();
cout<<" 性别:"<<sex<<endl;
}
int main()
{
Date b1;
People people1("name","id",b1,0,'m');
people1.getDate();
people1.showDate();
return 0;
}