日期计算器:C++日期类的实现(赋值运算符重载实现)以及赋值运算符重载

日期类具体功能

1、获取每个月的天数
2、判断两个日期是否同一天
3、判断两个日期的先后
4、某日期在某天后的日期
5、某日期在某天前的日期
6、日期减日期所得天数

赋值运算符

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。

在C语言中,我们如果要实现两个数字相加,则需要定义一个函数,函数名为Add()或者有些取名不规范叫做Jia()或其他名字,并不是人人一眼都能看懂;但是c++中的赋值运算符重载就解决了这个问题,增强了代码的可读性。
举个例子:如果我们想用赋值运算符实现两数相加,可以定义函数为int operator+=(int),最后返回*this,在测试时先定义一个常量(如int a),在调用该赋值运算符函数时,直接a+=b即可(b为想要加上的数字)。

函数名字为:关键字operator后面接需要重载的运算符符号
函数原型:返回值类型 operator操作符(参数列表)。

注意:
1、不能通过连接其他符号来创建新的操作符:比如operator@ ;
2、重载操作符必须有一个类类型或者枚举类型的操作数用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不能改变其含义;
3、作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的
操作符有一个默认的形参this,限定为第一个形参;
4、(*)(::)(sizeof)(?:)(.) 注意以上5个运算符不能重载。这个经常在笔试选择题中出现。

日期类代码实现

头文件

#pragma once 
#include <iostream>
using namespace std;

class Date
{
public:
	//打印日期
	void Print();
	//获取每个月的天数
	int GetMonthDay(int year, int month);
	//构造函数
	Date(int year, int month, int day);
	//拷贝构造函数
	Date(const Date& d);

	//判断两个日期是否同一天
	bool operator==(Date& d);
	bool operator!=(Date& d);

	//判断两个日期的先后
	bool operator>(Date& d);
	bool operator<(Date& d);
	bool operator<=(Date& d);
	bool operator>=(Date& d);


	//某日期在某天后的日期
	Date operator+=(int day);
	//某日期在某天后的日期
	Date operator-=(int day);
	//日期减日期所得天数
	int operator-(Date& d);
	
private:
	int _year;
	int _month;
	int _day;
};

源文件

#include"Date.h"

void Date::Print()
{
	cout << _year << "-" << _month << "-" << _day << endl;
}
int Date::GetMonthDay(int year, int month)
{
	static int dayArr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	int days = dayArr[month];
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		days += 1;
	}

	return days;
}
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (!(year >= 0 && month > 0 && month < 13 && day > 0 && day <= GetMonthDay(year, month)))
	{
		cout << "日期非法" << endl;
	}
}
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}


//判断两个日期是否同一天
bool Date::operator==(Date& d)
{
	return d._year == _year && d._month == _month && d._day == _day;
}
bool Date::operator!=(Date& d)
{
	return !(*this == d);
}
//判断两个日期的先后
bool Date::operator>(Date& d)
{
	//return _year > d._year || _month > d._month || _day > d._day;
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year)
	{
		if (_month > d._month)
			return true;
	}
	else if (_year == d._year && _month == d._month)
	{
		if (_day > d._day)
			return true;
	}

	return false;
}
bool Date::operator<(Date& d)
{
	//return _year > d._year || _month > d._month || _day > d._day;
	if (_year < d._year)
	{
		return true;
	}
	else if (_year == d._year)
	{
		if (_month < d._month)
			return true;
	}
	else if (_year == d._year && _month == d._month)
	{
		if (_day < d._day)
			return true;
	}
	return false;
}
bool Date::operator<=(Date& d)
{
	return *this < d || *this == d;
}
bool Date::operator>=(Date& d)
{
	return *this > d || *this == d;
}
//某日期在某天后的日期
Date Date::operator+=(int 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)
{
	_day -= day;
	_month -= 1;
	while (_day < 1)
	{
		_day += GetMonthDay(_year, _month);
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
	}
	_month += 1;
	return *this;
}
//日期减日期所得天数
int Date::operator-(Date& d)
{
    //默认*this为大的天数
	Date max = *this, min = d;
	int flag = 1;
	//如果*this为小的天数,使flag=-1;最终相差天数也为负数
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		++n;
		min += 1;
	}
	return n * flag;
}

测试源文件(main函数)

#include"Date.h"
int main()
{
	Date d1(2000, 3, 30);
	d1.Print();
	cout << "该日期10000天后为";
	d1 += 10000;
	d1.Print();
	Date d2(2021, 2, 5);
	d2.Print();
	cout << "该日期200天前为";
	d2 -= 200;
	d2.Print();
	putchar('\n');

	Date d3(2000, 3, 30);
	Date d4(2021, 2, 5);
	d3.Print();
	d4.Print();
	cout << "上面两天相差"<< d4 - d3 << "天" << endl;
}

运行截图
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值