1.首先我们先介绍一下 赋值重载
	void operator=(const Date& d)
	{//赋值拷贝 是拷贝给一个已经赋值好的对象 也叫赋值重载
		cout << "赋值重载" << endl;
		year = d.year;
		month = d.month;
		day = d.day;
		
	}如果我们想要拷贝函数连续拷贝,那我们肯定要返回一个类,这样才能两两一组逐个进行连续拷贝
class Date
{
public:
  Date(int _year , int _month , int _day )
{
	year = _year;
	month = _month;
	day = _day;
}
  /.....
 private:
  int year;
  int month;
  int day;
};
int main()
{ 
   Date d1(2024,07,01);
   Date d2,d3;
   d3=d2=d1;
   
}就像上面这样的,我们d1先给d2拷贝赋值,接着就如果没有任何返回那么,将无法对d3进行再调用拷贝函数,所以我们返回也得是一个类,既然d2已经被拷贝成功,那我们直接返回d2就可以,也就是*this。当然可以返回d ,前提是有写好的拷贝函数
class Date
{
public:
  Date(int _year , int _month , int _day )
{
	year = _year;
	month = _month;
	day = _day;
}
  Date operator=(const Date& d)
	{
         if(this!=d)
       {
		cout << "赋值重载" << endl;
		year = d.year;
		month = d.month;
		day = d.day;
       } return *this;
	}
  /.....
 private:
  int year;
  int month;
  int day;
};
int main()
{ 
   Date d1(2024,07,01);
   Date d2,d3;
   d3=d2=d1;
   
}2.传值返回和引用返回
Date& func()
{ 
	//如果是静态的
//	static Date d;
	Date d;
	return d;
}
Date func()
{
	Date d;
	
	return d;
}首先我们要看这两个返回,如果是第一个引用返回,返回的是d的引用,相当于返回d的别名,那么请问我们出了这个函数,我们的d还存在吗? 当然是不存在的,它的生命周期结束了,空间已经还回操作系统了,所以返回d的引用是不可以的,除非是在静态区上
其次我们看下面这个,为什么返回这个可以呢,因为传值返回是返回 它的拷贝的临时对象,就是d要先调用一下拷贝构造,然后接着呢,再把拷贝好的临时对象返回去,然后函数结束,析构函数释放d。
!:但是如果可以的情况下进行是用引用返回,可以减少很多戏的不必要的临时对象的拷贝;
int main()
{
	Date d1(2024, 6, 27);//默认构造
	Date d2 = d1;//拷贝构造
	Date d3(2024, 6, 28);
	d1 = d3;//赋值重载 赋值拷贝
	Date d4;
	d4 = d1 = d3;//赋值重载
	return 0;
}!:赋值拷贝和拷贝构造的区别就是 赋值拷贝是给已经赋值好的一个对象再拷贝赋值
拷贝构造是给一个没有赋值的对象进行拷贝赋值。
我们可以借助上面的代码观察一下传值返回和引用返回的差异


因为我们每次调用赋值重载的话,如果不是引用就每次调用完赋值重载的话,返回就是一个临时对象,就要拷贝一下,每次多拷贝的,每次都要析构函数的调用。
3.运算符重载
函数名字为:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型 operator操作符(参数列表)
	int GetMonthDay(int year, int month)//因为重复调用多次,所以写在声明的地方,声明的函数就是内敛函数方便调用
	{
		assert(month > 0 && month < 13);		int arr[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
		{
			return 29;
		}
		else
		{
			return arr[month];
		}
	}
Date& Date::operator+=(int _day)
{
	if (_day < 0)
	{
		return *this -= -_day;
	}
	day += _day;
	while (day > GetMonthDay(year, month))
	{
		day -= GetMonthDay(year, month);
		month++;
		if (month > 12)
		{
			year++;
			month = 1;
		}
		
	}
	return *this;
}实现+的话是什么情况呢,就是加上一个日期,返回新的日期,但自身的值不变,那我们就可以用一个临时变量进行计算,返回临时变量,自身的值不变
Date Date::operator+(int _day)
{
	Date d1 = *this;
	d1 += _day;
	return d1;
}用了一下c++里面的复用
!:值得注意的是我们的+=用的引用返回,但是+用的就是传值返回,+=的对象一直在类里面没有因为+=函数结束而销毁,所以用引用返回没问题,但是我们+用的是一个局部变量,会销毁,但是如果传值返回我们会形成一个临时对象的拷贝,所以我们防止计算出的结果的销毁,用传值返回,保证值的成功返回。
接着前置++和后置++在c++里面有一个区分
Date& Date:: operator++()//前置++
{
	*this += 1;
	return *this;
}
Date Date::operator++(int)//后置++
{
	Date d1(*this);
	*this += 1;
	return d1;
}运算符重载,用一个传值的个数区分。
5 const成员函数

我们发现对Print函数竟然调不动,这是为什么呢?就是因为在类的成员函数里面 默认的情况是
Date* const this 是对this进行了const的管理,但我们d1是const Date* const this此时的调用是权限的放大,所以不可以,我们只要按照规定,给Print函数加上对应的const就可以了。
c++的规定是 
这样就可以了。



















