日期类的实现

日期类的实现

Data.h

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
class Date

{
public:

	// 获取某年某月的天数
	int GetMonthDay(int year, int month)    //由于这个函数会要频繁的调用,所以将这个函数写在类里面,本质为类函数
	{
		assert(month > 0 && month < 13);
		static int Monthday[] = { 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)))
		{
			return 29;
		}
		return Monthday[month];
	}

	// 全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;

	}
	// 拷贝构造函数

// d2(d1)

	Date(const Date& d)
	{

		_year = d._year;
		_month = d._month;
		_day = d._day;

	}
	// 赋值运算符重载

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

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

	// 析构函数

	~Date()
	{
		_year = 0;
		_month = 0;
		_day = 0;


	}
	// 日期+=天数

	Date& operator+=(int day);

	// 日期+天数

	Date operator+(int day);
	// 日期-=天数

	Date& operator-=(int day);
	// 日期-天数

	Date operator-(int day);
	// 前置++

	Date& operator++();
	// 后置++

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

	Date operator--(int);



	// 前置--

	Date& operator--();
	// >运算符重载

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

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

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

	bool operator != (const Date& d);
	void print()
	{
		cout << _year << "/" << _month << "/" << _day <<endl;
	}

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

Data.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"
Date& Date::operator+=(int day)
{
	_day += day;
	while(_day> GetMonthDay(_year,_month))
	{
		_day = _day - GetMonthDay(_year, _month);
		_month = _month + 1;
		if (_month==13)
		{
			_year++;
			_month = 1;
		}
	}

	return *this;
}

Date Date::operator+(int day)
{
	Date tem = *this;
	tem += day;
	return tem;


}
// 日期-=天数

Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{

		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}

		_day = _day + GetMonthDay(_year, _month);

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

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

}
// 后置--

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



// 前置--

Date& Date::operator--()
	{
	return *this -= 1;
	
	};
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 && _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;
	}
	else
		return false;


}
bool Date::operator >= (const Date& d)
{
	if (*this > d || *this == d)
	{
		return true;
	}
	return false;
}
bool Date::operator != (const Date& d)
{
	return !(*this == d);

}

2.日期类的补充

用上面的日期类测试下面的代码

 	const Date d1(2024, 3, 24);
	d1.print();

会发现printf会打印不了
原因:因为加上const以后d1不可以修改了,而传到print()函数以后由&d1 const Data变为了this Date,printf()没有const的权限,权限扩大所以会报错

解决的办法是将printf()函数也加上const,这样它们的权限就是一样的了

	void print() const
	{
		cout << _year << "/" << _month << "/" << _day <<endl;
	}

权限的扩大是不可以的,但是权限的缩小是可以的,像下面就是权限的缩小

	void print() const
	{
		cout << _year << "/" << _month << "/" << _day <<endl;
	}
	int main()
	{
    Date d1(2024, 3, 24);
	d1.print();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南子北游

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值