C++学习——日期类的实现

本文档展示了如何使用C++编写一个日期类,包括获取某月天数、构造函数、拷贝构造函数、赋值运算符重载以及日期的加减运算。此外,还实现了日期的比较运算符和友元函数,用于日期的流输入输出。通过这个例子,读者可以了解C++中类的设计和运算符重载的原理。
摘要由CSDN通过智能技术生成

系列文章目录

C++学习传送门


文章目录


日期类的实现难点在于运算符重载,而运算符重载的要点在于函数复用
代码如下:

#include<iostream>
using namespace std;
class Date
{
public:

	// 获取某年某月的天数

	int GetMonthDay(int year, int month)
	{
		int day[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2
			&& ((year % 4 == 0 && year % 100 != 0)
				|| (year % 400 == 0)))
		{
			day[2] = 29;
		}
		return day[month];
	}



	// 全缺省的构造函数

	Date(int year = 1900, int month = 1, int day = 1)
		:_year(year), _month(month), _day(day)
	{
		//cout << "构造函数" << endl;
	}



	// 拷贝构造函数

	// d2(d1)

	Date(const Date& d)
	{
		//cout << "拷贝构造函数" << endl;
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}



	// 赋值运算符重载

  // d2 = d3 -> d2.operator=(&d2, d3)

	Date& operator=(const Date& d)
	{
		//cout << "赋值重载函数" << endl;
		if (&d != this)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}



	// 析构函数

	~Date()
	{
		//cout << "析构函数:" << _year << ' ' << _month << ' ' << _day << endl;
		_year = 0;
		_month = 0;
		_day = 0;
	}

	// 日期+天数

	Date operator+(int day)
	{
		if (day < 0)
		{
			return (*this) - (-day);
		}
		Date tem(*this);
		tem._day += day;
		while (tem._day > GetMonthDay(tem._year, tem._month))
		{
			tem._day -= GetMonthDay(tem._year, tem._month);
			tem._month++;
			if (tem._month == 13)
			{
				tem._year++;
				tem._month = 1;
			}
		}
		return tem;
	}

	// 日期+=天数

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



	// 日期-天数

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

		}
		return tem;
	}




	// 日期-=天数

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



	// 前置++

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



	// 后置++

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



	// 后置--

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



	// 前置--

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



	// >运算符重载

	bool 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;
		return false;
	}



	// ==运算符重载

	bool operator==(const Date& d)
	{
		return _year == d._year && _month == d._month && _day == d._day;
	}



	// >=运算符重载

	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);
	}



	// !=运算符重载

	bool operator != (const Date& d)
	{
		return !(*this == d);
	}



	// 日期-日期 返回天数

	int operator-(const Date& d)
	{
		int flag = 1;
		Date max = (*this);
		Date min = d;
		if ((*this) < d)
		{
			max = d;
			min = (*this);
			flag = -1;
		}
		int count = 0;
		while (max != min)
		{
			++min;
			++count;
		}
		return flag * count;
	}

	//友元声明(可以放在类的任何地方,不受三大限定符的影响)
	friend ostream& operator<<(ostream& out, const Date& d);

	friend istream& operator>>(istream& ipt, Date& d);

	void Print()
	{
		cout << _year << ' ' << _month << ' ' << _day << endl;
	}
private:

	int _year;

	int _month;

	int _day;

};

//流插入重载
inline ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year <<' ' << d._month <<' ' << d._day << endl;
	return out;
}


//流提取重载
inline istream& operator>>(istream& ipt, Date& d)
{
	ipt >> d._year >> d._month >> d._day;
	return ipt;
}

//测试
void test()
{
	Date d1(2003, 6, 18);
	Date d2(d1);
	Date d3;
	d1.Print();
	d2.Print();
	d3.Print();
	if (d1 == d2)
	{
		cout << "d1==d2" << endl;
	}
	if (d1 != d3)
	{
		cout << "d1!=d3" << endl;
	}
	if (d1 > d3)
	{
		cout << "d1>d3" << endl;
	}
	if (d1 >= d3)
	{
		cout << "d1>=d3" << endl;
	}
	if (d3 < d1)
	{
		cout << "d3<d1" << endl;
	}
	if (d3 <= d1)
	{
		cout << "d3<=d1" << endl;
	}
	d1 += 1000;
	d1.Print();
	d1 -= 1000;
	d1.Print();
	Date tem1 = d1++;
	Date tem2 = ++d1;
	Date tem3 = d1--;
	Date tem4 = --d1;
	tem1.Print();
	tem2.Print();
	tem3.Print();
	tem4.Print();

	cout << d1 - d3 << endl;


	Date d5;
	cout << "输入时间(年:月:日)" << endl;
	cin >> d5;
	cout << d5;
}

int main()
{
	test();
	return 0;
}

运行结果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值