#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//Date 类
class Date{
int year;
int month;
int day;
public:
//Date的构造函数:
Date(int y=0,int m=0,int d=0)
{
year=y;
month=m;
day=d;
}
//Date的复制构造函数
Date(const Date&D)
{
cout<<"copy..."<<endl;
year=D.year;
month=D.month;
day=D.day;
}
//Date的输入成员函数
void input()
{
cout<<"please enter the year,month,day:"<<endl;
cin>>year>>month>>day;
}
//Date的输出成员:
void show()
{
cout<<"the birthday is:"<<endl;
cout<<year<<"/"<<month<<"/"<<day<<endl;
}
//Date的析构函数
~Date()
{
cout<<"删除生日中..."<<endl;
}
};
//People类,其中包括类内嵌子对象Date,动态数组的指针averagesalary,构造函数和析构函数、拷贝构造函数
class People{
int number;//编号
string name;//姓名
char sex;//性别
Date birthday;//出生日期
int years;//入职年数
string id;//身份证号
int *averagesalary; //动态数组的指针averagesalary
public:
//People 的构造函数:
People()
{
number=0;
name='n';
sex='n';
years=0;
id='n';
averagesalary=new int[years];
}
//People 的复制构造函数:
People(const People&P)
{
cout<<"copying..."<<endl;
number=P.number;
name=P.name;
sex=P.sex;
birthday=P.birthday;
years=P.years;
id=P.id;
averagesalary=new int [years];
for(int i=0;i<years;i++)
{
averagesalary[i]=P.averagesalary[i];
}
}
//People的成员函数
inline void input_person();
inline void show_time();
//==运算符重载
bool operator==(People&p1)
{
if(id==p1.id)return true;
else return false;
}
//=运算符重载
People operator=(People&P1)
{
number=P1.number;
name=P1.name;
sex=P1.sex;
birthday=P1.birthday;
years=P1.years;
id=P1.id;
averagesalary=new int [years];
for(int i=0;i<years;i++)
{
averagesalary[i]=P1.averagesalary[i];
}
return *this;
}
//people 的析构函数:
~People()
{
cout<<"The end,thank you for watching."<<endl;
delete []averagesalary;
}
};
//People的show_time内联函数
void People::input_person()
{
cout<<"请输入他的编号:"<<endl;
cin>>number;
cout<<"请输入他的姓名:"<<endl;
cin>>name;
cout<<"请输入他的性别(F为男,M为女):"<<endl;
cin>>sex;
cout<<"请输入他的出生日期:"<<endl;
birthday.input();
cout<<"请输入他的入职年数:"<<endl;
cin>>years;
cout<<"请输入他这些年的每年的薪酬:"<<endl;
for(int i=0;i<years;i++)
{
cin>>averagesalary[i];
}
cout<<"请输入他的身份证号:"<<endl;
cin>>id;
cout<<"=============END INPUT================"<<endl;
}
void People::show_time()
{
cout<<"============SHOW TIME============"<<endl;
cout<<"他的编号是: "<<setw(4)<<setfill('0')<<number<<endl;
cout<<"他的姓名是: "<<name<<endl;
cout<<"他的性别是: "<<sex<<endl;
cout<<"他的出生日期是: "<<endl;
birthday.show();
cout<<"他的入职年数是: "<<years<<endl;
cout<<"他这些年的每年的薪酬是:"<<endl;
for(int i=0;i<years;i++)
{
cout<<averagesalary[i]<<endl;
}
cout<<"他的身份证号是: "<<id<<endl;
}
//主函数:
int main()
{
People p1;
p1.input_person();
p1.show_time();
cout<<endl;
People p2(p1);
p2.show_time();
if(p1==p2)
{
cout<<"the id is repetition"<<endl;
}
else
cout<<"the id is new"<<endl;
return 0;
}
心得体会:
1、写一个较长的代码的时候不要一气写完后在编译,要写一部分再编译一部分,否则会出现较多的报错无从下手。
2、当有new的时候一定要用深拷贝,深拷贝一定要在复制构造函数和构造函数中都写上new的内容,否则会出现编译通过但是运行出问题的结果。
3、averagesalary=new int[years]; 这个的意思是
定义了一个int类型的数组名是averagesalary的动态数组,数组的大小由years决定。
求解:
为什么编译运行的时候不会显示p1.showtime()的内容,但是调试的时候却可以有p1.showtime()的内容?