【C++】实现一个日期计算器


需要云服务器等云产品来学习Linux的同学可以移步/-->腾讯云<--/-->阿里云<--/-->华为云<--/官网,轻量型云服务器低至112元/年,新用户首次下单享超低折扣。


 目录

 一、日期计算器的功能

二、获取每个月的天数

三、Date类中的默认成员函数

1、构造函数

2、析构函数

3、拷贝构造

4、赋值运算符重载

5、取地址操作符重载和const取地址操作符重载

四、运算符重载

1、+=、+、-=、-

2、==、!=、>、>=、<、<=

3、前置++和--、后置++和--

4、<<流插入、>>流提取运算符

五、日期类代码


 一、日期计算器的功能

实现日期类的==、!=、+=、+、-=、-、>=、>、<=、<、前置++和--、后置++和--。

二、获取每个月的天数

int GetMonthDay(int year, int month)
{
    //静态数组,每次调用不用频繁在栈区创建数组
    static int monthArr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
    //判断是否闰年
    int day = monthArr[month - 1];
    if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
    {
        day = 29;
    }
    return day;
}

1、因为GetMonthDay这个函数需要在日期类中被频繁调用,所以将 monthArr存放至静态区,减少数组频繁开辟、销毁空间的开销。

三、Date类中的默认成员函数

1、构造函数

Date(int year = 1, int month = 1, int day = 1)
{
    if (year >= 1 && month >= 1 && day >= 1 && day <= GetMonthDay(year, month))
    {
        _year = year;
        _month = month;
        _day = day;
        //cout << "构造成功" << endl;
    }
    else
    {
        cout << "日期不合法" << endl;
    }
}

日期类的构造函数需要对日期的的合法性进行判断。

2、析构函数

~Date()//可不写
{
    ;
}

日期类因为没有申请资源(动态开辟空间、文件的打开等),所以无需写析构函数,系统默认生成的就可以。

3、拷贝构造

Date(const Date& d)//可不写
{
    _year = d._year;
    _month = d._month;
    _day = d._day;
    //cout << "拷贝构造成功" << endl;
}

系统默认生成的拷贝构造函数会对内置类型完成浅拷贝,所以内置类型也可以不用写,用系统默认生成的就可以。

后续处理有资源的对象时,需要深拷贝。

4、赋值运算符重载

Date& operator=(const Date& d)
{
    _year = d._year;
    _month = d._month;
    _day = d._day;
    //cout << "赋值成功" << endl;
    return *this;
}

也可不写,使用系统默认生成的即可。

拷贝构造和赋值运算符重载的区别在于拷贝构造用于对象构造时使用,而赋值运算符重载用于已存在对象赋值时使用。

后续处理有资源的对象时,需要先把旧空间释放,再开一块同样大小的空间,进行数据拷贝。

5、取地址操作符重载和const取地址操作符重载

Date* operator&()
{
    return this;
    //return nullptr;
}
const Date* operator&()const
{
    return this;
    //return nullptr;
}

不用自己写,除非想让别人通过取地址操作符获取到特定值(自己在重载函数内部写)或屏蔽类地址。 

四、运算符重载

1、+=、+、-=、-

Date& operator+=(int day)
	{
		if (day < 0)
			*this -= -day;
		else
		{
			_day += day;
			while (_day > GetMonthDay(_year, _month))
			{
				_day -= GetMonthDay(_year, _month);
				++_month;
				if (_month > 12)
				{
					_month = 1;
					++_year;
				}
			}
		}
		return *this;
	}
	Date operator+(int day)const
	{
		Date tmp(*this);
		return tmp += day;
	}
	Date& operator-=(int day)
	{
		if (day < 0)
			*this += -day;
		else
		{
			_day -= day;
			while (_day <= 0)
			{
				--_month;
				if (_month <= 0)
				{
					_month = 12;
					--_year;
				}
				_day += GetMonthDay(_year, _month);
			}
		}
		return *this;
	}
	Date operator-(int day)const
	{
		Date tmp(*this);
		return tmp -= day;
	}
	int operator-(const Date& d)const
	{
		Date max = *this, min = d;
		int flag = 1;//用于表示符号位
		if (*this < d)
		{
			max = d;
			min = *this;
			flag = -1;
		}
		int count = 0;//用于计数
		while (max > min)
		{
			++min;
			++count;
		}
		return count * flag;
	}

1、注意这几个运算符要防止外部传入的day是负数。例如+=传入的参数如果是负数,则去调用-=函数。

2、注意传值返回和传引用返回,当return对象出了作用域还存在时,可以用传引用返回,减少一次拷贝构造。

3、实现完+=、-=后,+、-运算符可复用逻辑。

4、对于*this不变的成员函数,函数后记得加上const。

2、==、!=、>、>=、<、<=

bool operator==(const Date& d)const
	{
		if (_year == d._year && _month == d._month && _day == d._day)
		{
			return true;
		}
		return false;
	}
	bool operator>(const Date& d)const
	{
		if (_year > d._year)
			return true;
		if (_year == d._year && _month > d._month)
			return true;
		if (_year == d._year && _month == d._month && _day > d._day)
			return true;
		return false;
	}
	bool operator>=(const Date& d)const
	{
		return *this > d || *this == d;
	}
	bool operator!=(const Date& d)const
	{
		return !(*this == d);
	}
	bool operator<(const Date& d)const
	{
		return !(*this >= d);
	}
	bool operator<=(const Date& d)const
	{
		return !(*this > d);
	}

1、注意右操组数一定要加上&,减少一次传参时的拷贝构造;再加上const,防止被引用的对象被改变。

2、写完==和>函数,其他运算符都可以复用逻辑。

3、前置++和--、后置++和--

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

1、因为++和--是单操作数的运算符,在重载时,无法区分是前置的重载还是后置的重载,所以C++规定:前置重载与普通运算符重载一致,后置重载需要在参数列表中加入一个无用的参数。这个参数必须是int类型(用别的类型编译器报错)。

2、前置++--可以使用传引用返回,但后置++--因为返回值暂时不改变,所以只能传值返回。这也是使用前置++--性能优于后置++--的原因。

4、<<流插入、>>流提取运算符

//类内声明为友元函数
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
//需要在类外定义
inline ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << " " << d._month << " " << d._day << endl;
	return out;
}
inline istream& operator>>(istream& in, Date& d)
{
	in >> d._year >>d._month >> d._day;
	return in;
}

需要在类外定义,在类内声明为友元函数。

五、日期类代码

class Date
{
public:
	int GetMonthDay(int year, int month)
	{
		//静态数组,每次调用不用频繁在栈区创建数组
		static int monthArr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
		//判断是否闰年
		int day = monthArr[month - 1];
		if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
		{
			day = 29;
		}
		return day;
	}
	//构造函数
	Date(int year=1 , int month = 1, int day = 1)
	{
		if (year >= 1 && month >= 1 && day >= 1 && day <= GetMonthDay(year, month))
		{
			_year = year;
			_month = month;
			_day = day;
			//cout << "构造成功" << endl;
		}
		else
		{
			cout << "日期不合法" << endl;
		}
	}
	//析构函数
	~Date()
	{
		cout << "析构成功" << endl;;
	}
	//拷贝构造
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
		cout << "拷贝构造成功" << endl;
	}
	//赋值运算符重载
	Date& operator=(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
		cout << "赋值成功" << endl;
		return *this;
	}
	//运算符重载
	Date& operator+=(int day)
	{
		if (day < 0)
			*this -= -day;
		else
		{
			_day += day;
			while (_day > GetMonthDay(_year, _month))
			{
				_day -= GetMonthDay(_year, _month);
				++_month;
				if (_month > 12)
				{
					_month = 1;
					++_year;
				}
			}
		}
		return *this;
	}
	Date operator+(int day)const
	{
		Date tmp(*this);
		return tmp += day;
	}
	Date& operator-=(int day)
	{
		if (day < 0)
			*this += -day;
		else
		{
			_day -= day;
			while (_day <= 0)
			{
				--_month;
				if (_month <= 0)
				{
					_month = 12;
					--_year;
				}
				_day += GetMonthDay(_year, _month);
			}
		}
		return *this;
	}
	Date operator-(int day)const
	{
		Date tmp(*this);
		return tmp -= day;
	}
	int operator-(const Date& d)const
	{
		Date max = *this, min = d;
		int flag = 1;//用于表示符号位
		if (*this < d)
		{
			max = d;
			min = *this;
			flag = -1;
		}
		int count = 0;//用于计数
		while (max > min)
		{
			++min;
			++count;
		}
		return count * flag;
	}
	bool operator==(const Date& d)const
	{
		if (_year == d._year && _month == d._month && _day == d._day)
		{
			return true;
		}
		return false;
	}
	bool operator>(const Date& d)const
	{
		if (_year > d._year)
			return true;
		if (_year == d._year && _month > d._month)
			return true;
		if (_year == d._year && _month == d._month && _day > d._day)
			return true;
		return false;
	}
	bool operator>=(const Date& d)const
	{
		return *this > d || *this == d;
	}
	bool operator!=(const Date& d)const
	{
		return !(*this == d);
	}
	bool operator<(const Date& d)const
	{
		return !(*this >= d);
	}
	bool operator<=(const Date& d)const
	{
		return !(*this > d);
	}
	Date& operator++()
	{
		++_day;
		if (_day > GetMonthDay(_year, _month))
		{
			_day = 1;
			++_month;
			if (_month > 12)
			{
				_month = 1;
				++_year;
			}
		}
		return *this;
	}
	Date operator++(int)
	{
		Date tmp(*this);
		++* this;
		return tmp;
	}
	Date& operator--()
	{
		--_day;
		if (_day <= 0)
		{
			--_month;
			if (_month == 0)
			{
				_month = 12;
				--_year;
			}
			_day += GetMonthDay(_year, _month);
		}
		return *this;
	}
	Date operator--(int)
	{
		Date tmp(*this);
		--* this;
		return tmp;
	}
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
	//取地址重载和const取地址重载
	Date* operator&()
	{
		return this;
	}
	const Date* operator&()const
	{
		return this;
	}
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& in, Date& d)
{
	in >> d._year >>d._month >> d._day;
	return in;
}

因为函数的声明和定义全部放在类中,会被编译器当成内联函数处理。所以可以根据自身需要,将部分调用不频繁、稍长的函数的声明写在类中,而定义写在类外。

CSDN话题挑战赛第2期
参赛话题:学习笔记

评论 69
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

蒋灵瑜

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

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

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

打赏作者

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

抵扣说明:

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

余额充值