用C/C++实现一个日期类,重载运算符=,==,+,-,++,--,>,>=,<,<=等

#include<iostream>
#include<windows.h>

using namespace std;

class Date
{
public:
	Date(int year, int month, int day) //构造函数
		:_year(year)
		,_month(month)
		,_day(day)
	{}
	Date(Date & d)   //拷贝构造
		:_year(d._year)
		, _month(d._month)
		, _day(d._day)
	{}
	Date & operator = (const Date &d) //赋值运算符的重载
	{//将实例d的所有成员变量值全部赋值给this,这里不存在指针问题,就不用考虑内存问题
		_year = d._year;
		_month = d._month;
		_day = d._day;
		return *this;
	}
	bool operator == (const Date& d)//重载 ==
	{
		return this->_year == d._year
			&& this->_month == d._month
			&& this->_day == d._day;
	}
	bool operator <(const Date& d) //重载 <
	{
		if (_year < d._year)//判断年
		{
			return true;
		}
		else if (_year == d._year)//年相等,就判断月
		{
			if (_month<d._month)
			{
				return true;
			}
			else if (_month == d._month)//月相等,判断天
			{
				if (_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);
	}
	Date operator+ (int day)//重载+
	{
		int ret = GetMonthDay(_year, _month);//调用函数获取当年当月天数
		Date tmp = *this;//用this创建一个临时对象
		while ((_day + day) > ret)//循环条件:当总天数大于一个月的天数时
		{
			if ((tmp._month + 1) > 12)//当月份大于12时
			{
				tmp._year++;//年份加一
				tmp._month = 0;//月份置零
			}
			else//否则,月份加一
			{
				tmp._month++;
			}
			day -= ret;//总天数减去当月的天数
			ret = GetMonthDay(_year, tmp._month);//获取下一月的天数
		}
		tmp._day += day;
		return tmp;
	}
	Date& operator+= (int day)//重载+=(复用+)
	{
		*this = *this + day;
		return *this;
	}

	Date operator- (int day)//重载-
	{
		int ret = GetMonthDay(_year, _month);
		Date tmp = *this;
		while (day > tmp._day)//当需要减的天>当月的天数时进行循环
		{
			while (day > ret)
			{
				if (tmp._month > 1)
				{
					tmp._month--;
				}
				else
				{
					tmp._year--;
					tmp._month = 11;
				}
				day -= ret;
				ret = GetMonthDay(tmp._year, tmp._month);
			}
			day -= ret;
		}
		tmp._day -= day;
		return tmp;
	}
	Date& operator-= (int day)//重载-=(复用-)
	{
		*this = *this - day;
		return *this;
	}

	Date operator++()
	{
		return *this += 1;
	}
	Date operator++(int)
	{
		return *this + 1;
	}

	Date operator--()
	{
		return *this -= 1;
	}
	Date operator--(int)
	{
		return *this - 1;
	}

	int operator-(const Date& d)//日期间日期
	{
		Date tmp = *this;
		int day = 0;
		int ret = 0;
		if (tmp > d)
		{
			while (tmp._year > d._year)
			{
				if (tmp._month > 2)
				{
					if (IsLeapYear(tmp._year))
					{
						day += 366;
						tmp._year--;
					}
				}
				else
				{
					day += 365;
					tmp._year--;
				}
			}
			while (tmp._month > d._month)
			{
				ret = GetMonthDay(tmp._year, tmp._month);
				day += ret;
			}
			if (tmp._day > d._day)
			{
				day += tmp._day - d._day;
			}
			else
			{
				day -= d._day - tmp._day;
			}

		}
		return day;
	}
	void Display()
	{
		cout << _year << "\t" << _month << "\t" << _day << endl;
	}
protected:
	bool IsLeapYear(int year)//判断是否为闰年
	{
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
		{
			return true;
		}
		return false;
	}
	int GetMonthDay(int year, int month)//获得当年当月天数
	{
		int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

		int day = monthArray[month];

		if (month == 2 && IsLeapYear(year))
		{
			day += 1;
		}
		return day;
	}
private:
	int _year;
	int _month;
	int _day;
};



  • 6
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个简单的C++类,包含重载+、-、*、/、==、>、<运算符,以及友元函数。同时,还提供了一个使用该类的示例。 ```c++ #include <iostream> class MyClass { public: MyClass(int a = 0, int b = 0) : x(a), y(b) {} MyClass operator+(const MyClass& other) const { return MyClass(x + other.x, y + other.y); } MyClass operator-(const MyClass& other) const { return MyClass(x - other.x, y - other.y); } MyClass operator*(const MyClass& other) const { return MyClass(x * other.x, y * other.y); } MyClass operator/(const MyClass& other) const { return MyClass(x / other.x, y / other.y); } bool operator==(const MyClass& other) const { return (x == other.x) && (y == other.y); } bool operator>(const MyClass& other) const { return (x > other.x) && (y > other.y); } bool operator<(const MyClass& other) const { return (x < other.x) && (y < other.y); } friend std::ostream& operator<<(std::ostream& os, const MyClass& obj); private: int x; int y; }; std::ostream& operator<<(std::ostream& os, const MyClass& obj) { os << "(" << obj.x << ", " << obj.y << ")"; return os; } int main() { MyClass a(1, 2); MyClass b(3, 4); MyClass c = a + b; MyClass d = a - b; MyClass e = a * b; MyClass f = a / b; std::cout << "a = " << a << ", b = " << b << std::endl; std::cout << "a + b = " << c << std::endl; std::cout << "a - b = " << d << std::endl; std::cout << "a * b = " << e << std::endl; std::cout << "a / b = " << f << std::endl; std::cout << "a == b? " << (a == b) << std::endl; std::cout << "a > b? " << (a > b) << std::endl; std::cout << "a < b? " << (a < b) << std::endl; return 0; } ``` 输出结果为: ``` a = (1, 2), b = (3, 4) a + b = (4, 6) a - b = (-2, -2) a * b = (3, 8) a / b = (0, 0) a == b? 0 a > b? 0 a < b? 1 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值