【C ++基础】第五篇 类和对象 日期计算器

【C ++基础】第五篇 类和对象 日期计算器

写在前面

更新情况记录:

最近更新时间更新次数
2022/10/141

参考博客与书籍以及链接:
(非常感谢这些博主们的文章,将我的一些疑问得到解决。)

参考博客链接或书籍名称
总目录:点击跳转

正文

1.前置知识

直接上我写的博客

C++类和对象

2.功能描述

日期的运算

日期+=天数、日期+天数、日期-天数、日期-=天数、日期前置++、日期后置++、日期前置–、日期后置++、日期-日期返回一个天数。

日期之前比较大小

==、>、>=、<、<=、!=。

3.功能接口及日期类的声明

Date.h

using namespace std;

class Date
{
public:
	// 获取某年某月的天数
	int GetMonthDay(int year, int month);
	//全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1);
	//拷贝构造函数
	Date(const Date& d);
	//赋值运算符重载
	Date& operator = (const Date & d);
	// 析构函数
	~Date();
	// 日期+=天数
	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);
	// <=运算符重载
	bool operator <= (const Date& d);
	// !=运算符重载
	bool operator != (const Date& d);
	// 日期-日期 返回天数
	int operator-(const Date& d);
	//打印日期
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

4.接口的实现

没有特别说明这里面代码都是放在Date.cpp中。

1.构造函数

写成全缺省的,不需要编译器自己构造

Date.h

Date(int year = 1900, int month = 1, int day = 1);

Date.cpp

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}
2.析构函数

又没有开辟空间,编译器自己的够用。

Date::~Date()
{
}
3.获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{
    //加static免得每次调用都要构造
	static int monthDayArray[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;
	}
	else
	{
		return monthDayArray[month];
	}
}
4.赋值运算符重载
Date& Date::operator = (const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}
5.日期+=天数
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;
}
6.日期+天数
Date Date::operator+(int day)
{
	Date ret(*this);
	ret += day;
	return ret;
}
7.日期-天数
Date Date::operator-(int day)
{
	Date ret(*this);
	ret -= day;
	return *this;
}
8.日期-=天数
Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		_day+= GetMonthDay(_year, _month);
		_month--;

		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
	}
	return *this;
}
9.前置++

效果:前置++,每次先++,然后再用

Date& Date::operator++()
{
	*this += 1;
	return *this;
}
10.后置++

效果:后置++,每次先用(保持原值),然后再++

Date Date::operator++(int)
{
	//返回的值没有加加
	Date tmp = *this;
	//但实际上已经加加了
	*this += 1;
	//返回的值没有加加
	return tmp;
}

一个小问题
编译器是如何辨别前置++与后置++的?

规定:后置++的形参加一个int,就像这样operator++(int);
假如没有的话,就会变成下面这个样子:

++d1;//转换成d1.operator++();
d1++;//转换成d2.operator++();
//这样就无法区分了

综上所述:需要加不加形参来辨别前置++与后置++;
下面的前置- -与后置- -也是同样的道理;

11.后置- -
Date Date::operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}
12.前置- -
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
13.>运算符重载
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;
	}
	return false;
}
14. ==运算符重载
bool Date::operator==(const Date& d)
{
	return (_year == d._year && _day == d._day && _month == d._month);
}
15. >=运算符重载

可以复用之前写的代码,来降低代码量。

bool Date::operator >= (const Date& d)
{
	return *this>d||*this==d;
}
16.<运算符重载
bool Date::operator < (const Date& d)
{
	return !(*this > d);
}
17.<=运算符重载
bool Date::operator <= (const Date& d)
{
	return *this < d || *this == d;
}
18.!=运算符重载
bool Date::operator != (const Date& d)
{
	return !(*this == d);
}
19.日期-日期 返回天数

不需要条件判断,用一个n来计数。

int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (max < min)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (max != min)
	{
		n++;
		min+=1;
	}
	return n * flag;
}

5.完整代码

1.Date.h
#pragma once
#include<iostream>
using namespace std;

class Date
{
public:
	 获取某年某月的天数
	int GetMonthDay(int year, int month);
	全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1);
	拷贝构造函数
	Date(const Date& d);
	赋值运算符重载
	Date& operator = (const Date & d);
	 析构函数
	~Date();
	/// 日期+=天数
	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);
	/// <=运算符重载
	bool operator <= (const Date& d);
	/// !=运算符重载
	bool operator != (const Date& d);
	/// 日期-日期 返回天数
	int operator-(const Date& d);
	///打印日期
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
2.Date.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}
Date::~Date()
{

}
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}
int Date::GetMonthDay(int year, int month)
{
	static int monthDayArray[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;
	}
	else
	{
		return monthDayArray[month];
	}
}

Date& Date::operator = (const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}
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)
{
	Date ret(*this);
	ret += day;
	return ret;
}
Date Date::operator-(int day)
{
	Date ret(*this);
	ret -= day;
	return *this;
}
Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		_day+= GetMonthDay(_year, _month);
		_month--;

		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
	}
	return *this;
}
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;
	}
	return false;
}
bool Date::operator==(const Date& d)
{
	return (_year == d._year && _day == d._day && _month == d._month);
}
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 || *this == d;
}
bool Date::operator != (const Date& d)
{
	return !(*this == d);
}
int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (max < min)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (max != min)
	{
		n++;
		min+=1;
	}
	return n * flag;
}

Date& Date::operator++()
{
	*this += 1;
	return *this;
}
Date Date::operator++(int)
{
	//返回的值没有加加
	Date tmp = *this;
	//但实际上已经加加了
	*this += 1;
	//返回的值没有加加
	return tmp;
}
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
Date Date::operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}

观众老爷们自己写测试用例吧。

6.补充

1.<<跟>>的运算符重载

在Date.h中

class Date
{
    //友元解决私有的问题。
    friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
};
//流插入
inline ostream& operator<<(ostream& out, Date& d)
{
	cout << 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;
}

本文完毕。

  • 21
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

潮.eth

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

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

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

打赏作者

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

抵扣说明:

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

余额充值