日期类实现

日期类的实现是C++中简单且适合检测基础的 代码,下面是日期类的具体实现:

头文件:

#pragma once
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

class Date
{
public:
	//默认构造函数
	Date(int year = 2022, int month = 5, int day = 19);
	//默认拷贝函数
	Date(const Date& d);
	//重载赋值运算符
	Date operator = (const Date& d);
	//打印
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}

	//重载比较运算符
	bool operator>(const Date& d);
	bool operator ==(const Date& d);


	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 || *this == d;
	}
	bool operator!=(const Date& d)
	{
		return !(*this == d);
	}

	//重载加减运算符
	//拿到每个月的天数
	int GetMonthDay();
	//判断是否是闰年
	bool IsLeapYear();

	Date& operator+=(int day);
	Date& operator-=(int day);

	Date operator-(int day)
	{
		Date tmp(*this);
		tmp -= day;
		return tmp;
	}
	Date operator+(int day)
	{
		Date tmp(*this);
		tmp += day;
		return tmp;
	}
	//相差几天
	int operator-(const Date& d) ;

	//前置后置++/--
	Date& operator++();//前置
	Date operator++(int );
	Date& operator--();
	Date operator--(int );

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

函数实现:

#include "Date.h"
//默认构造和拷贝函数
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}
Date Date::operator = (const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}


//日期加减——运算符重载
bool Date::operator>(const Date& d)
{
	if (_year > d._year)
		return true;
	else if (_year == d._year && _month > d._month)
		return true;
	else if (_year == d._year && d._month == d._month && _day > d._day)
		return true;
	else
		return false;
}
bool Date::operator==(const Date& d)
{
	if (_year == d._year && _month == d._month && _day == d._day)
		return true;
	return false;
}

//判断是否是闰年
bool Date::IsLeapYear()
{
	if (_year % 4 == 0 || _year % 100 != 0 && _year % 400 == 0)
		return true;
	return false;
}
//拿到每个月的天数
int Date::GetMonthDay()
{
	static int monthday[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (_month == 2 && IsLeapYear())
		return 29;
	else
		return monthday[_month];
}

//重载加减运算符
Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay())
	{
		_day -= GetMonthDay();
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}
Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day < 0)
	{
		_day += GetMonthDay();
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
	}
	return *this;
}

//记得加Date::
//int Date::operator -(const Date& d) const{}会有问题,因为前面的this都没有用const修饰
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 (max!=min)
	{
		min += 1;
		count++;
	}
	return flag*count;
}

//前置后置++/--
Date& Date::operator++()//前置
{
	*this += 1;
	return *this;
}
Date Date::operator++(int)
{
	Date tmp(*this);
	(*this)++;
	return tmp;
}
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
Date Date::operator--(int )
{
	Date tmp(*this);
	(*this)--;
	return tmp;
}

Test文件

#include "Date.h"
//测试构造函数和拷贝函数
void Test1()
{
	Date d1(2022, 5, 20);
	Date d2(d1);
	d1.Print();
	d2.Print();
}
//测试重载的比较运算符
void Test2()
{
	Date d1(2022, 5, 19);
	Date d2(2023, 5, 19);
	Date d3(2022, 6, 19);
	Date d4(2022, 5, 20);
	cout << (d1 < d2) << endl;
	cout << (d2 > d3) << endl;
	cout << (d3 >= d4) << endl;
	cout << (d4 <= d1) << endl;
	cout << (d2 == d3) << endl;
}
//测试重载的加减运算符
void Test3()
{
	Date d1(2022, 5, 19);
	Date d2 = d1 + 1000;
	d2.Print();

	d1 += 1000;
	d1.Print();
	

}
//测试减
void Test4()
{
	Date d1(2022, 5, 19);
	Date d2 = d1 + 10000;
	cout << (d1 - d2) << endl;
}
//测试加加减减
void Test5()
{
	Date d1(2022, 5, 19);
	Date d2(2022, 5, 30);
	cout <<"++d1:" ;
	(++d1).Print();
	cout << "d1++:";
	//Date d3 = ++d1(0);
	//d3.Print();

	cout << "d1--";
	(d1--).Print();
	cout << "--d1";
	(--d1).Print();
}
int main()
{
	//Test1();
	//Test2();
	//Test3();
	//Test4();
	Test5();
}

 笔记:

1.

//默认构造函数
//全缺省的函数不要在定义的时候再写缺省值

2.

//默认拷贝函数
//参数d可以使用private中的_year,_month,_day这是因为这个函数是类中的内置函数

3.

/判断是不是闰年
//这个函数可以自由使用类中的定义的元素吗?可以,
//这是在类中定义的函数,类中定义的函数可以自由使用类中的元素,
//因为他隐藏了一个this指针,哪个对象用这个函数用的就是谁的元素

4.

//进位题:全加到最低位再进位

//加减重载
//使用传值返回还要经过拷贝构造,所以效率低了;但是用传引用返回就不会经过拷贝的过程,更快。
Date& Date::operator+=(int day)

但是如果返回Date一定是没问题的。

5.

//没有写成d.f()的形式也可以用吗?

//别忘了这里隐藏了一个参数this,这个大函数也是有this的,所以自然就可以调用了

6.

//返回值是引用的,那么返回的东西一定是在外面存在的

7.

Date Date::operator+(int day)
{
    //拷贝一份
    Date ret(*this);
    ret._day += day;
    int monthday = GetMonthDay();
    while (ret._day > monthday)
    {
        ret._day -= monthday;
        ret._month++;
        if (ret._month == 13)
        {
            ret._month = 1;
            ret._year++;
        }
        monthday = GetMonthDay();
    }
    //这个时候没有返回ret而是返回的ret的临时拷贝,所以即使栈释放了,
    //内存中没有了ret但是寄存器中仍然存有ret的内容,以拷贝函数的形式返回赋给d2
    return ret;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值