C++:实现一个Date类

关于日期类,我们最基本的成员变量就是三个:年、月、日。关于成员函数我们要实现构造,拷贝构造,赋值,关于日期的比较大小,以及日期加天数,日期减天数,以及+=和- =,同时还要考虑能否复用,日期减日期,还有日期类的++和- -(分为前置和后置)等。

具体代码如下:详情请看代码注释

Date.h
#pragma once
#include <iostream>
class Date
{
public:
	void Print()const;
	int Date::GetMonthDay(int year, int month)const;
	//构造函数
	Date(int year = 1900, int month = 1, int day = 1);
	//析构函数
	~Date();
	//拷贝构造Date d2(d1);
	Date(const Date& d);
	//赋值运算符重载
	Date& operator=(const Date& d);
	//比较日期
	bool operator==(const Date&d)const;
	bool operator!=(const Date&d)const;
	bool operator>(const Date&d)const;
	bool operator<(const Date&d)const;
	bool operator>=(const Date&d)const;
	bool operator<=(const Date&d)const;
	//关于日期的加减
	Date operator+(int day)const;//日期加天数,原日期不变,因此要构造临时对象,要传值返回
	Date& operator+=(int day);//日期加上天数,把日期改变,直接传引用
	Date operator-(int day)const;//日期减天数,原日期不变
	Date& operator-=(int day);//日期减去天数,把日期改变
	int operator-(Date& d)const;//日期减日期,返回值是天数int
	//前置++(d.operator++(&d))
	Date& operator++();
	//后置++(d.operator++(&d,0))
	Date operator++(int);
	//前置--(d.operator--(&d))
	Date& operator--();
	//后置--(d.operator--(&d,0))
	Date operator--(int);
private:
	int _year;
	int _month;
	int _day;
};
Date.cpp
#include "Date.h"
//打印
void Date::Print()const
{
	std::cout << _year << "-" << _month << "-" << _day << std::endl;
}

//获取月的天数
int Date::GetMonthDay(int year, int month)const
{
	static int array[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	//是闰年的话2月返回29天
	if ((month == 2) && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		return 29;
	}
	return array[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
	{
		std::cout << "Date Invalid" << std::endl;
	}
}

//析构函数
Date::~Date()
{}

//拷贝构造Date d2(d1);
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;//可以支持连续赋值
}

//判断日期大小或相等
bool Date::operator==(const Date&d)const
{
	return _year == d._year 
		&& _month == d._month 
		&& _day == d._day;
}
bool Date::operator!=(const Date&d)const
{
	return !(*this == d);
}
bool Date::operator>(const Date&d)const
{
	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)const
{
	return !(*this > d);
}
bool Date::operator>=(const Date&d)const
{
	return (*this > d) || (*this == d);
}
bool Date::operator<=(const Date&d)const
{
	return (*this < d) || (*this == d);
}

//关于日期的加减

d1+100----d1不发生改变
不复用实现+
//Date Date::operator+(int day)const
//{
//	Date ret(*this);
//	ret._day += day;
//	while (ret._day > GetMonthDay(ret._year, ret._month))//大于该月天数就不合法
//	{
//		ret._day -= GetMonthDay(ret._year, ret._month);
//		ret._month++;
//		if (ret._month == 13)
//		{
//			ret._year++;
//			ret._month = 1;
//		}
//	}
//	return ret;
//}

//复用+=实现+
Date Date::operator+(int day)const
{
	Date ret(*this);
	ret += day;
	return ret;
}

d1-100----d1不发生改变
不复用实现-
//Date Date::operator-(int day)const
//{
//	Date ret(*this);
//	ret._day -= day;
//	while (ret._day <= 0)//小于和等于0都不合法
//	{
//		--ret._month;//借的是上个月的天数
//		if (ret._month == 0)
//		{
//			ret._year--;
//			ret._month = 12;
//		}
//		ret._day += GetMonthDay(ret._year, ret._month);		
//	}
//	return ret;
//}

//复用-=实现-
Date Date::operator-(int day)const
{
	Date ret(*this);
	ret -= day;
	return ret;
}

//实现+=
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 == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}

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

//d1-d2
int Date::operator-(Date& d)const//日期减日期,返回值是天数int
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d)
	{
		Date max = d;
		Date min = *this;
		flag = -1;
	}
	int num = 0;
	while (max != min)
	{
		min++;
		num++;
	}
	return num;
}

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

//前置--(d.operator--(&d))
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
//后置--(d.operator--(&d,0))
Date Date::operator--(int)
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}
main.cpp
#include "Date.h"

int main()
{
	Date d1;//默认构造
	d1.Print();
	Date d2(2019, 1, 1);//构造
	d2.Print();
	Date d3(d2);//拷贝构造
	d3.Print();
	Date d4;
	d4.Print();
	d4 = d3;//赋值
	d4.Print();

	Date (2016, 2, 29).Print();//匿名对象

	Date d5(2017, 10, 4);//日期加100天
	Date d6 = d5 + 100;
	d6.Print();

	Date d7(2018,1,12);//日期减100天
	Date d8 = d7 - 100;
	d8.Print();

	Date d9(1900, 11, 20);//+=
	d9 += 100;
	d9.Print();

	Date d10(1901, 2, 28);//-=
	d10 -= 100;
	d10.Print();

	Date d11(2019, 2, 27);//日期减日期
	Date d12(2019, 7, 15);
	int ret = d12 - d11;
	std::cout << ret << std::endl;

	//++
	Date d13(2019, 8, 8);
	(++d13).Print();//2019-8-9
	(d13++).Print();//2019-8-9
	d13.Print();//2019-8-10

	system("pause");
	return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值