C++——运算符重载

#define _CRT_SECURE_NO_WARNINGS 1
#include 
using namespace std;
class Date
{
public:
	Date(int year = 2023, int month = 10, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
		if (_month < 1 || month > 12 || _day < 1 || _day > GetMonthDay(_year, _month))
		{
			cout << "日期不规范" << endl;
			//exit(-1);  终止程序
		}
	}
	//拷贝构造函数
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	bool operator== (const Date& d)
	{
		return (_year == d._year) && (_month == d._month) && (_day == d._day);
	}
	bool operator<(const Date& d)
	{
		if (_year < d._year)
		{
			return true;
		}

		else if ((_year == d._year) && (_month < d._month))
		{
			return true;
		}

		else if ((_year == d._year) && (_month == d._month) && (_day < d._day))
		{
			return true;
		}
		return false;
	}
	bool operator<=(const Date& d)
	{
		return *this < d || *this == d;
	}
	bool operator>=(const Date& d)
	{
		return !(*this < d);
	}
	bool operator>(const Date& d)
	{
		return !(*this <= d);
	}
	bool operator!=(const Date& d)
	{
		return !(*this == d);
	}
	void print() const //const 放在这里,指的是this指针为const Date*类型,这样就可以让const Date类型进行print了
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
	int GetMonthDay(int year, int month)//获取某面某月的天数
	{
		//加static,可以省去每次访问时都开辟数组空间。
		static int MonthArray[] = { 0,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;
		return MonthArray[month];
	}
	Date& operator+=(int day)
	{
		if (day < 0)
		{
			*this -= (-day);
			return *this;
		}
		*this = *this + day;
		return *this;
	}
	Date& operator-=(int day)
	{
		if (day < 0)
		{
			*this += (-day);
			return *this;
		}
		_day -= day;
		while (_day <= 0)
		{
			_month--;
			if (_month == 0)
			{
				--_year;
				_month = 12;
			}
			_day += GetMonthDay(_year, _month);
		}
		return *this;
	}
	Date operator+(int day)
	{
		Date tmp(*this);
		if (day < 0)
		{
			tmp -= (-day);
			return tmp;
		}
		tmp._day += day;
		while (tmp._day > GetMonthDay(tmp._year, tmp._month))
		{
			tmp._day -= GetMonthDay(tmp._year, tmp._month);
			tmp._month++;
			if (tmp._month == 13)
			{
				tmp._year++;
				tmp._month = 1;
			}
		}
		return tmp;
	}
	Date operator-(int day)
	{
		Date tmp(*this);
		tmp -= day;
		return tmp;
	}
	Date& operator++()//前置++,返回++后的结果
	{
		*this += 1;
		return *this;
	}
	Date operator++(int)//后置++,返回++前的结果,但是还是要++
	{                   //这里放置一个参数,只是为了和前置++进行区别,进行函数重载
		Date tmp(*this);
		*this += 1;
		return tmp;
	}
	int operator-(const Date& d)
	{
		Date max = *this;
		Date min = d;
		int flag = 1;
		if (*this < d)
		{
			max = d;
			min = *this;
			flag = -1;
		}
		int n = 0;
		while (max != min)
		{
			min++;
			n++;
		}
		return n * flag;
	}
	//赋值运算符
	//返回类型为Date,支持连续赋值
	Date& operator=(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
		return *this;//*this出了函数还在,就可以使用引用返回。
	}
private:
	int _year;
	int _month;
	int _day;


};
int main()
{
	Date d3(2007, 7, 20);
	Date d4(2003, 1, 10);
	int love = d3 - d4;
	const Date d5(2001, 1, 10);
	d5.print();
	cout << love << endl;
	return 0;
}

你可能感兴趣的:(c++)