【C++教程从0到1入门编程】第十二篇:日期类

本文详细介绍了C++中Date类的默认成员函数,包括构造函数、析构函数、拷贝构造、赋值运算符以及日期的加减运算和打印功能。

一、类的6个默认成员函数

#define _CRT_SECURE_NO_WARNINGS   1
#include<iostream>
using namespace std;
class Date
{
public:
	//获取某年某月的天数
	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 = day + 1;
		}
		return day;
	}

	//全缺省构造函数
	Date(int year = 0, int month = 1, int day = 1)  //构造函数
		//:_year(year)
		//, _month(month)
		//, _day(day)
		//{}
	{
		if ((year >= 0) && (month >= 1 && month <= 12) && (day <= GetMonthDay(year, month)))
		{
			_year = year;
			_month = month;
			_day = day;
		}
		else
		{
			cout << "非法日期!" << endl;
		}
	}

	//析构函数
	~Date()
	{
		;
	}
	//d2(d1)   拷贝构造函数
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	//d2=d1   赋值运算符重载
	Date& operator=(const Date& d)
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}

	//d1+10 d1+100 d1+1000
	//日期+天数
	//条件运算符的重载
	Date operator+(int day)
	{
		//d1+10 d不变 d+=10 d才变
		Date tmp = *this;    //拷贝构造一个tmp ----Date tmp(*this)
		tmp._day += day;
		while (tmp._day > GetMonthDay(tmp._year, tmp._month))
		{
			//如果日期天不合法就要往月进位
			tmp._day -= GetMonthDay(tmp._year, tmp._month);
			tmp._month++;
			if (tmp._month > 12)
			{
				tmp._year++;
				tmp._month -= 12;
			}
		}
		return tmp;

		//可以复用
		//Date tmp(*this) Date tmp=*this;
		//tmp+=day;
	}
	//日期+=天数
	Date& operator+=(int day)
	{
		if (day < 0)               //day为负数要考虑到
		{
			return *this -= -day;
		}
		_day += day;
		if (day > GetMonthDay(_year, _month))
		{
			day -= GetMonthDay(_year, _day);
			_month++;
			if (_month > 12)
			{
				_year++;
				_month -= 12;
			}
		}
		return *this;
	}
	//日期-天数  d1-10 d1.operator-(&d1,10)
	Date operator-(int day)
	{
		//Date tmp(*this);
		Date tmp = *this;
		tmp._day -= day;
		while (tmp._day <= 0)
		{
			--tmp._month;          //注意是前置--
			if (tmp._month == 0)
			{
				--tmp._year;       //注意是前置--
				tmp._month = 12;
			}
			tmp._day += GetMonthDay(tmp._year, tmp._month);   //注意这个步骤写最后面
		}
		return tmp;
	}
	//日期-=天数 d1-=10 d1.operator-=(&d1,10)
	Date& operator-=(int day)
	{
		if (day < 0)           //day为负数要考虑一下
		{
			return *this += -day;
		}
		_day -= day;
		while (_day <= 0)            //不合法的天数 循环继续  ,注意有等于
		{
			--_month;
			if (_month == 0)
			{
				--_year;
				_month = 12;
			}
			_day += GetMonthDay(_year, _month);   //注意这个步骤
		}
		return *this;
	}
	//日期-日期
	int operator-(const Date& d) const
	{
		int flag = 1;
		//小的日期加到大的日期就是相差的天数
		Date max = *this;
		Date min = d;
		if (*this < d)
		{
			max = d;           //operator =
			min = *this;
			flag = -1;         //*this小于d时候flag就是负数
		}
		int count = 0;
		while (min != max)
		{
			min++;
			count++;
		}
		return count * flag;
	}
	//采用复用形式
	//前置++  *d1++;
	Date& operator++()
	{
		*this = *this + 1;
		return *this;
	}
	//后置++
	Date operator++(int)
	{
		Date tmp = *this;
		*this += 1;
		return tmp;    //返回之前的日期,后置--不会变
	}
	//前置--
	Date& operator--()
	{
		*this = *this - 1;
		return *this;
	}
	//后置--
	Date operator--(int)
	{
		Date tmp(*this);
		*this = *this - 1;
		return tmp;   //返回之前的日期
	}
	//比较运算符重载
	//d1=d2 d.operator(&d1,d2)
	bool operator==(const Date& d)
	{
		return _year == d._year && _day == d._day && _month == d._month;
	}
	//d1!=d2
	bool operator!=(const Date& d)
	{
		return !(*this == d);
	}
	//d1<d2 d1.operator(&d1,d2)
	bool operator<(const Date& d) const
	{
		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;
		}
		else
		{
			return false;
		}
	}
	//d1<=d2
	bool operator<=(const Date& d) const
	{
		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;
		}
		else
		{
			return false;
		}

		//return *this < d || *this == d;  复用<和==
	}

	//d1>d2 d.operator(&d1,d2)
	bool operator>(const Date& d) const
	{
		return !(*this <= d);    //!逻辑取反  ~位取反
	}
	//d1>d2
	bool operator>=(const Date& d)
	{
		return !(*this < d);
	}

	void  print()                                  //打印函数
	{
		cout << "year=" << _year << ":" << "month=" << _month << ":" << "day=" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1(2021, 7, 14);
	Date d2(d1);
	d1.print();
	d2.print();
	Date d3;
	d3 = d2;
	d3.print();
	//比较运算符重载
	cout << (d1 < d2) << endl;
	cout << (d1 > d2) << endl;
	cout << (d1 == d2) << endl;
	cout << (d1 != d2) << endl;
	cout << (d1 >= d2) << endl;
	cout << (d1 <= d2) << endl;
	//条件运算符重载,看这个运算符是否有意义
	Date d4 = d1 + 30;
	d1.print();
	d4.print();
	Date d5 = d1 - 20;
	d5.print();
	d1.print();
	Date d6 = d1 += 10;
	d6.print();
	d1.print();
	Date d7 = d1 -= 10;
	d7.print();
	d1.print();
	Date d8(2020, 7, 15);
	Date d9(2020, 7, 15);
	cout << (d8 - d9) << endl;
	return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值