Date 日期类

Date 日期类

下面实现一个简单的日期类


类的定义

因为在类外面定义函数会导致一些函数访问private成员变量导致代码出问题,所以直接就把函数定义到类里面,类里面的成员函数是可以访问私有的成员变量的

#pragma once
class Date
{
public:
	// 获取某年某月的天数
	int GetMonthDay(int year, int month);
	//星期几
	void GetWeekday();
	//打印年月日
	void Print();
	// 全缺省的构造函数
	Date(int year = 1, int month = 1, int day = 1);
	// 拷贝构造函数
  // d2(d1)
	Date(const Date& d);
	// 赋值运算符重载
  // d2 = d3 -> d2.operator=(&d2, d3)
	Date& operator=(const Date& d);
	// 析构函数
	~Date();
	// 日期+=天数
	Date& operator+=(int day);
	// 日期+天数
	Date operator+(int day);
	// 日期-天数
	Date operator-(int day);
	// 日期-=天数
	Date& operator-=(int day);
	// 前置++(不做改变)
	Date& operator++();
	// 后置++(多定义一个int型参数,没实际作用,只是为了方便重载)
	Date operator++(int);
	// 前置--
	Date& operator--();
	// 后置--
	Date operator--(int);
	// >运算符重载
	bool operator>(const Date& d);
	// ==运算符重载
	bool operator==(const Date& d);
	// >=运算符重载
	bool operator>=(const Date& d);
	// <运算符重载
	bool operator<(const Date& d);
	// <=运算符重载
	bool operator<=(const Date& d);
	// !=运算符重载
	bool operator!=(const Date& d);
	// 日期-日期 返回天数
	int operator-(const Date& d);
private:
	int _year;
	int _month;
	int _day;
};


获取某年某月的天数

int Date::GetMonthDay(int year, int month)
{
	//先定义一个数组存储常见的12月份的天数,在单独判断是否为闰年
	static int monthdayarray[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};//数组不会改变,可以定义成静态
	int day = monthdayarray[month];
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		day += 1;//只有是二月这个特殊月份才需要判断是否为闰年,所以判断可以提前
	}
	return day;
}

打印年月日

void Date::Print()
{
	//不能光打印还得判断一下日期是否合理
	if (_month <= 12)//首先月份小于,等于12
	{
		//其次就是获取日期,进行比较
		int monthday = GetMonthDay(_year, _month);
		if (_day <= monthday)
			cout << _year << '-' << _month << '-' << _day << endl;
		else
		{
			cout << "日期输入错误->";
			cout << _year << '-' << _month << '-' << _day << endl;
		}
	}
	else
	{
		cout << "日期输入错误->";
		cout << _year << '-' << _month << '-' << _day << endl;
	}
}

构造函数(全缺省)

// 如果声明和定义分开,全缺省函数的缺省值要么在声明要么在定义
//不允许声明和定义都给缺省值,建议在声明的时候给值
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}


析构函数

Date::~Date()//日期类的话默认生成的就够了
{
     //因为日期类没有开辟堆上的空间
}

拷贝构造函数

// 拷贝构造函数
Date::Date(const Date& d)
{
    //记得拷贝构造传的是引用
	_year=d._year;
	_month = d._month;
	_day = d._day;
}

赋值运算符重载

Date& Date::operator= (const Date& d)
//*this在结束也不会销毁,所以可以传引用
{
	_year=d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}

日期+=天数

Date& Date::operator+=(int day)
{
//如果是负的天数,就相当于日期-=(-day)
	if (day < 0)
	{
		return *this -= (-day);
	}
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}

日期+天数

Date Date::operator+(int day)
{
//因为加不会改变原来的日期,所以可以定义一个新的类来存储加后的日期
//因为是局部变量所以结束时会销毁,用传值返回
	Date ret(*this);//拷贝构造一个类
	ret += day;
	return ret;
}

日期-=天数

Date& Date::operator-=(int day)
{
	//如果减的是负数的天数
	if (day < 0)
	{
		return *this += (-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++()
{
	*this += 1;
	return *this;
}

后置++

//因为++都是一个参数,所以为了区分,编译器规定在后置++的参数上加上
//一个int 类型的参数,没用实质性的作用,只是为了占位,方便运算符重载
//后置++是要先返回加前的值所以需要拷贝构造一个类来返回
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;
}

大于(>)和比较是否相等(==)的重载

// >运算符重载
bool Date::operator>(const Date& d)
{
	if ((_year > d._year) ||
		(_year == d._year && _month > d._month) ||
		(_year == d._year && _month == d._month && _day > d._day))
		return true;
	else
		return false;
}
// ==运算符重载
bool Date::operator==(const Date& d)
{
	return (_year == d._year) && (_month == d._month) && (_day == d._day);
}
//有了这两个运算符可以通过复用这两个操作符来实现其他的比较

其他的比较运算符的重载

//通过代码的复用可以很大程度的简化代码
//而且会让后续的代码的错误率降低,只要一开始实现的代码没有问题
//其他的实现逻辑没有问题,基本上复用实现的的代码不会出错
// >=运算符重载
bool Date::operator>=(const Date& d)
{
	return (*this > d) || (*this == d);
}
// <运算符重载
bool Date::operator<(const Date& d)
{
	return !(*this >= d);
}
// <=运算符重载
bool Date::operator<=(const Date& d)
{
	return !(*this > d);
}
// !=运算符重载
bool Date::operator!=(const Date& d)
{
	return !(*this == d);
}

日期-日期(返回天数)

//这里采用的是比较简单易懂的方法
//就是比较哪个日期大,然后让小的日期不断++,直到和打的日期相等
//返回加了多少天,flag是用来控制如果第一个日期小于第二个日期返回的天数就是负数
//第一个日期大于第二个日期就是正数
int Date::operator-(const Date& d)
{
	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::GetWeekday()
{
	Date start(1900,1,1);//起始日期星期一
	int count = *this - start;
	const char* dayarr[7] = { "星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
	cout << dayarr[count % 7] << endl;
}
  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值