C++:日期类的实现

Date.h

#define _CRT_SECURE_NO_WARNINGS 1

#pragma once
#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1);
	Date(const Date& d);
	Date& operator=(const Date& d);
	

	void Print();
	inline static int GetMonthDay(int year, int month);


	Date operator+(int days);
	Date operator-(int days);
	Date& operator+=(int day);
	Date& operator-=(int day);
	int operator-(const Date& d);
	Date& operator++();
	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);

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

Daet.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"

int Date::GetMonthDay(int year, int month)
{ 
	static int monthArr[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)
	{
		return 29;
	}
	return monthArr[month];
}
Date::Date(int year, int month, int day)//构造函数
{
	if (year > 0 && month > 0 && month<13 && day > 0 && day <= GetMonthDay(year, month))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "Date Invalid" << endl;
 	}
}

void Date::Print()
{
	cout << _year << "-" << _month << "-" << _day << endl;
}

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

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

Date Date::operator+(int days)
{
	Date ret(*this);
	ret += days;
	return ret;
}
Date Date::operator-(int days)
{
	Date ret(*this);
	ret -= days;
	return ret;
}

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


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

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 n = 0;
	while (min != max)
	{
		++min;
		++n;
	}
	return n*flag;
}

Date& Date::operator++()
{
	*this += 1;
	return *this;
}
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)
{
	return _year > d._year || (_year == d._year && _month > d._month) || (_year == d._year && _month == d._month && _day > d._day);
}

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


int main()
{
	Date d1(2018, 11, 20);
	Date d2(2019, 4, 1);
	d1.Print();
	d2.Print();
	int ret = (d1 - d2);
	cout << ret << endl;
	//Date d4 = d1;//拷贝构造,不是赋值(赋值是把一个已经存在的对象赋值给拎一个对象)
	//d4.Print();
	//Date(2018, 11, 20).Print();//匿名对象,(只使用一次)   类名+(参数对象),匿名对象的生命周期在这一行。

	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值