实验二运算符的重载,体验深拷贝

#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()的内容?



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值