所有的运算符重载

class Date
{
	friend ostream& operator<<(ostream& _cout, const Date& d);  //设置<<重载运算符为友元函数
public:
	//获取某年是不是闰年
	bool GetYear(int year)
	{
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
		{
			return true;
		}
		return false;
	}
	// 获取某年某月的天数
	int GetMonthDay(int year, int month)
	{
		int arr[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (GetYear(year) && month == 2)
		{
			return arr[month] + 1;
		}
		return arr[month];
	}
	// 全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_count++;
		_year = year;
		_month = month;
		_day = day;
		if (year < 0 || month < 1 || month>12 || day<1 || day>GetMonthDay(year, month))
		{
			_year = 1900;
			_month = 1;
			_day = 1;
		}
	}
	// 拷贝构造函数
  // d2(d1)
	Date(const Date& d)
	{
		_count++;
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	// 赋值运算符重载
  // d2 = d3 -> d2.operator=(&d2, d3)
	Date& operator=(const Date& d)
	{
		if (this == &d)
		{
			return *this;
		}
		_year = d._year;
		_month = d._month;
		_day = d._day;
		return *this;
	}
	 析构函数
	~Date()
	{
		_count--;
	}
	// 日期+=天数
	Date& operator+=(int day)
	{
		*this = *this + day;
		return *this;
	}
	// 日期+天数
	Date operator+(int day)
	{
		if (day < 0)
		{
			return *this - (0 - day);
		}
		Date tmp(*this);
		tmp._day += day;
		while (tmp._day > GetMonthDay(tmp._year, tmp._month))
		{
			tmp._day -= GetMonthDay(tmp._year, tmp._month);
			tmp._month+=1;
			if (tmp._month > 12)
			{
				tmp._month = 1;
				tmp._year++;
			}
		}
		return tmp;
	}
	// 日期-天数
	Date operator-(int day)
	{
		if (day < 0)
		{
			return *this + (0 - day);
		}
		Date tmp(*this);
		tmp._day -= day;
		while (tmp._day <= 0)
		{
			tmp._day += GetMonthDay(tmp._year, tmp._month);
			tmp._month--;
			if(tmp._month < 1)
			{
				tmp. _year--;
				tmp._month = 12;
			}
		}
		return tmp;
	}
	// 日期-=天数
	Date& operator-=(int day)
	{
		*this = *this - day;
		return *this;
	}
	// 前置++
	Date& operator++()  //返回值是引用,因为返回this原来的值
	{
		*this = *this + 1;
		return *this;
	}
	// 后置++           //参数列表是一个int类型的,没有用,只是为了和前置++区分
	Date operator++(int)//返回值,因为temp是局部变量,所以要通过拷贝构造函数,局部变量也能传进去
	{
		Date temp = *this;
		*this=*this+1;
		return temp;
	}
	// 后置--
	Date operator--(int)
	{
		Date tmp = *this;
		*this = *this - 1;
		return tmp;
	}
	// 前置--
	Date& operator--()
	{
		*this = *this - 1;
		return *this;
	}
	// >运算符重载
	bool operator>(const Date& d)
	{
		if (_year > d._year)
		{
			return true;
		}
		else if (_year < d._year)
		{
			return false;
		}
		else
		{
			if (_month > d._month)
			{
				return true;
			}
			else if (_month < d._month)
			{
				return false;
			}
			else
			{
				_day > d._day ? true : false;
			}
		}
	}
	// ==运算符重载
	bool operator==(const Date& d)
	{
		return _year == d._year && _month == d._month && _day == d._day;
	}
	// >=运算符重载
	inline bool operator >= (const Date& d)
	{
		if (_year > d._year)
		{
			return true;
		}
		else if (_year < d._year)
		{
			return false;
		}
		else
		{
			if (_month > d._month)
			{
				return true;
			}
			else if (_month < d._month)
			{
				return false;
			}
			else
			{
				_day >= d._day ? true : false;
			}
		}
	}
	// <运算符重载
	bool operator < (const Date& d)
	{
		if (_year < d._year)
		{
			return true;
		}
		else if (_year > d._year)
		{
			return false;
		}
		else
		{
			if (_month < d._month)
			{
				return true;
			}
			else if (_month > d._month)
			{
				return false;
			}
			else
			{
				return _day < d._day ? true : false;
			}
		}
	}
	// <=运算符重载
	bool operator <= (const Date& d)
	{
		if (_year < d._year)
		{
			return true;
		}
		else if (_year > d._year)
		{
			return false;
		}
		else
		{
			if (_month < d._month)
			{
				return true;
			}
			else if (_month > d._month)
			{
				return false;
			}
			else
			{
				_day <= d._day ? true : false;
			}
		}
	}
	// !=运算符重载
	bool operator != (const Date& d)
	{
		if (_year != d._year)
		{
			return true;
		}
		else
		{
			if (_month != d._month)
			{
				return true;
			}
			else
			{
				_day == d._day ? false : true;
			}
		}
	}
	// 日期-日期 返回天数
	int operator-(const Date& d)
	{
		Date min = *this;
		Date max = d;
		if (*this > d)
		{
			min = d;
			max = *this;
		}
		int res = 0;
		while (min < max)
		{
			min++;
			res++;
		}
		return res;
	}
	void print()
	{
		cout << _year << "年" << _month << "月" << _day << "日" << endl;
	}                                      
	//ostream& operator<<(ostream& _cout)   //有返回值是为了能够连续输出
	//{                                      //<<运算符不能在类里重载,因为类里的第一个参数是this指针,所以调用的时候要这么调d<<cout;
	//	_cout << _year << _month << _day;   //这个函数第一个参数是this指针,所以在以后调用的时候
	//}                                    //要这么写d<<cout,这不符合常理,所以这个重载要在类外
	 int Getcount()                  //加这个函数的原因是,虽然_count是静态的,但是还是私有的
	{                                //如果将这个函数设置为静态的,那么必须创建对象后才能调用
		return _count;
	}
private:
	int _year;
	int _month;
	int _day;
	static int _count;   //静态成员函数在类中只是声明,要在类外定义
};
int Date::_count = 0;
ostream& operator<<(ostream& _cout, const Date& d)  //在类里声明为友元函数,就能访问类里的私有成员
{                                                   //不是类里的函数
	_cout << d._year << d._month << d._day;         //在类外不能访问
	return _cout;
}
void text()//测试
{
	Date dd;
	cout << dd.Getcount() << endl;
	Date ddd;
	cout << dd.Getcount()<< endl;
}
int main()
{
	//Date d(2022, 3, 20);
	//Date d1 = d - 100; //这里-100可能出错,因为100可能会被构造函数转成一个类,
	//cout<<d1 - d<<endl;//调用哪个函数就不知道了
	//d1.print();
	//cout << d << endl;
	//cout << d << d << endl;//实际是operator(cout,)
	Date d;
	Date d1;
	text();
	cout << d.Getcount() << endl;
	cout << d << d1 << endl;//operator<<(operator<<(cout,d),d1),和这个等价
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值