6.【CPP】Date类的实现

Date.h

cpp 复制代码
#pragma once
using namespace std;
#include<iostream>

class Date
{
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	//构造函数会被频繁调用,放在类里面作为inline
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	//d1=d3
	Date& operator=(const Date& d)//引用作返回值,不用调用拷贝构造存临时变量
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
		return *this;
	}

	bool operator==(const Date& d)const;
	bool operator!=(const Date& d)const;
	bool operator>(const Date& d)const;
	bool operator>=(const Date& d)const;
	bool operator<(const Date& d)const;
	bool operator<=(const Date& d)const;
	
	//写在类中作为内联
	int GetMonthDay(int year, int month)
	{
		static int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		int day = days[month];
		if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
		{
			day += 1;
		}
		return day;
	}

	Date operator+(int day);
	Date& operator+=(int day);
	Date& operator-=(int day);
	Date operator-(int day);
	Date& operator++(int);
	Date& operator++();
	Date& operator--(int);
	Date& operator--();
	int operator-(const Date& d)const;
	void PrintWeekDay()const;

	void Print()const;

private:
	int _year;
	int _month;
	int _day;
};

inline ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "/" << d._month << "/" << d._day;
	return out;
}

inline istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}

Date.cpp

cpp 复制代码
#include"Date.h"
using namespace std;

//任何一个类,只需要写一个> ==或者< ==重载,剩下的运算符重载复用即可
bool Date::operator==(const Date& d)const
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}

bool Date::operator!=(const Date& d)const
{
	/*return _year != d._year
		|| _month != d._month
		|| _day != d._day;*/
	return !(*this == d);
}
bool Date::operator>(const Date& d)const
{
	return (_year > d._year)
		|| ((_year == d._year) && (_month > d._month))
		|| (_year == d._year) && (_month == d._month) && (_day > d._day);
}
bool Date::operator>=(const Date& d)const
{
	return (*this > d) || (*this == d);
}
bool Date::operator<(const Date& d)const
{
	return !(*this >= d);
}
bool Date::operator<=(const Date& d)const
{
	return !(*this > d);
}

Date Date::operator+(int day)
{
	Date ret(*this);//拷贝构造
	//Date ret=*this;也是拷贝构造,不是赋值,因为开始变量不存在
	ret += day;

	return ret;
}
Date& Date::operator+=(int day)//支持连续加等,所以有返回值
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;
		if (_month==13)
		{
			_year++;
			_month = 1;
		}
	}

	return *this;
}

void Date::Print()const
{
	cout << _year <<"-" << _month<<"-" << _day << endl;
}

Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}
Date Date::operator-(int day)
{
	Date ret = (*this);
	ret -= day;

	return ret;
}

Date& Date::operator++(int)
{
	Date ret(*this);
	*this += 1;

	return ret;
}
Date& Date::operator++()
{
	*this += 1;
	return *this;
}

Date& Date::operator--(int)
{
	Date ret(*this);
	*this -= 1;

	return ret;
}
Date& Date::operator--()
{
	*this += 1;
	return *this;
}

int Date::operator-(const Date& d)const//日期减日期
{
	//默认第一个大,第二个小
	Date max = *this;
	Date min = d;
	int flag = 1;

	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}

	int count = 0;
	while (min != max)
	{
		min++;
		count++;
	}

	return count * flag;
}

void Date::PrintWeekDay()const
{
	const char* week[] = { "周一","周二","周三","周四","周五","周六","周日" };
	Date start(1900, 1, 1);
	int count = *this-start;
	cout << week[count % 7] << endl;
}
相关推荐
Evand J13 分钟前
【MATLAB例程】AOA与TDOA混合定位例程,适用于三维环境、4个锚点的情况,附下载链接
开发语言·matlab
机器视觉知识推荐、就业指导13 分钟前
Qt 与Halcon联合开发八: 结合Qt与Halcon实现海康相机采图显示(附源码)
开发语言·数码相机·qt
DoraBigHead30 分钟前
小哆啦解题记——映射的背叛
算法
mit6.82441 分钟前
[vroom] docs | 输入与问题定义 | 任务与运输工具 | json
c++·自动驾驶
Heartoxx41 分钟前
c语言-指针与一维数组
c语言·开发语言·算法
hqxstudying43 分钟前
Java创建型模式---原型模式
java·开发语言·设计模式·代码规范
charlie1145141911 小时前
如何使用Qt创建一个浮在MainWindow上的滑动小Panel
开发语言·c++·qt·界面设计
孤狼warrior1 小时前
灰色预测模型
人工智能·python·算法·数学建模
京东云开发者1 小时前
京东零售基于国产芯片的AI引擎技术
算法
神仙别闹1 小时前
基于Python实现LSTM对股票走势的预测
开发语言·python·lstm