【C++初阶】类和对象(中)日期类详解和运算符重载的使用

文章目录

  • 简介
  • 日期计算器的实现
    • 构造、拷贝和析构的实现
    • 获取每月天数和打印的实现
    • 几个日期比较函数的实现
    • 几个单个日期运算函数的实现
    • 前置、后置 ++、--的实现
    • 自己实现类对象的cout和cin
    • 获取当天的星期的实现
  • 完整版日期类
    • Date.h
    • Date.cpp
    • Test.cpp


简介

这里主要是对日期计算器的实现,实现的同时,复习之前学过的四大默认函数,并且加深对运算符重载的认识和使用。

日期计算器的实现

在日常的生活中,日期对于我们是必不可少的。比如离过年还有多少天,前一周是几月几号。所以日期计算器就变的尤为重要。所以我们今天要来实现一个日期计算器。
【C++初阶】类和对象(中)日期类详解和运算符重载的使用_第1张图片
方法:通过自定义一个日期类来实现日期计算器。
1.我们要定义的日期类,包括了成员变量,成员函数的声明和定义。(声明在头文件中,定义在源文件中)
2.实现成员函数
3.对成员函数进行测试

先给日期类的声明给大家看看:

#pragma once
#include 
using namespace std;

//获取每个月的天数
int GetMonthDay(int year, int month);

class Date
{
	//自己实现对象的cout
	friend ostream& operator<<(ostream& out, const Date& d);
	//自己实现对象的cin
	friend istream& operator>>(istream& in, Date& d);
public:
	//构造函数
	Date(int year = 1, int month = 1, int day = 1);
	//拷贝函数
	Date(const Date& d);
	//析构函数
	~Date();
	//日期的打印
	void DatePrint();
	
	//日期的比较
	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);

	//单个日期的运算
	Date operator+(int day);
	Date operator-(int day);
	Date& operator+=(int day);
	Date& operator-=(int day);

	//日期的自增和自减
	//前置++
	Date& operator++();
	//后置++
	Date operator++(int);
	//前置--
	Date& operator--();
	//后置--
	Date operator--(int);

	//日期和日期相减
	int operator-(const Date& d);

	//获取当天的星期
	void GetWeekendDay();
private:
	int _year;
	int _month;
	int _day;
};

//自己实现对象的cout
ostream& operator<<(ostream& out,const Date& d);
//自己实现对象的cin
istream& operator>>(istream& in,Date& d);

这里自己写的cout和cin大家不明白为什么要在类里面和外面定义的话,大家可以去了解一下友元函数。

构造、拷贝和析构的实现

Date::Date(int year , int month , int day )
{
	_year = year;
	_month = month;
	_day = day;
}
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}
Date::~Date()
{
	_year = 0;
	_month = 0;
	_day = 0;
}

上面的几个函数都是默认函数,对于日期类来说大家不是实现也没什么影响,因为编译器自己实现的就够用来,但对于栈、队列这些,就不一定了。具体想要了解可以看我之前的文章。

获取每月天数和打印的实现

int GetMonthDay(int year, int month)
{
	int a[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	int day = a[month];
	//判断是否是闰年
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
	{
		day++;//进来了就说明是闰年的二月份有29天所以要++
	}
	return day;

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

因为每个月的天数都不同,闰年和平年也会有些不同,所以需要写个函数来获取每月的天数。

几个日期比较函数的实现

bool Date::operator>(const Date& d)
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year)
	{
		if (_month > d._month)
		{
			return true;
		}
		else if(_month == d._month)
		{
			if (_day > d._day)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}

	}
	else
	{
		return false;
	}
}
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 || *this == d;
}
bool Date::operator<=(const Date& d)
{
	return !(*this > d);
}
bool Date::operator!=(const Date& d)
{
	return !(*this == d);
}

这里只要实现>或者<和==就可以了,因为其它的逻辑和前两函数一样所以可以复用这两函数来实现。

几个单个日期运算函数的实现

Date Date::operator+(int day)
{

	Date ret = *this;

	if (day < 0)
	{
		ret -= -day;
	}
	else
	{
		ret._day += day;
		while (ret._day > GetMonthDay(ret._year, ret._month))
		{
			ret._day -= GetMonthDay(ret._year, ret._month);
			ret._month++;
			if (ret._month > 12)
			{
				ret._month = 1;
				ret._year++;
			}
		}
	}
	
	
	return ret;
}

Date Date::operator-(int day)
{
	Date ret = *this;
	if (day < 0)
	{
		ret += -day;
	}
	else
	{
		ret._day -= day;
		//ret._day += 1;
		while (ret._day < 0)
		{
			if (ret._month == 1)
			{
				ret._month = 12;
				ret._year--;
				ret._day += GetMonthDay(ret._year, ret._month);
			}
			else
			{
				ret._month--;
				ret._day += GetMonthDay(ret._year, ret._month);
			}

		}
	}
	return ret;
}

Date& Date::operator+=(int day)
{
	* this = *this + day;
	return *this;
}

Date& Date::operator-=(int day)
{
	*this = *this - day;
	return *this;
}

这里日期的+和-思路
主要是:
1.先再_day上面操作,+就把要加的天数全家到_day上,-就把要减的天数全减到_day上。
2.再对_month和_day进行操作,_day+(-)每个月的天数,_month+(-)
3.再判断_dayh和_month是否在正常范围内,_month不在正常范围内就对_year+(-),再把_month置为正常值(循环进行,直到_day为正常值为止)
4.最后返回计算好的日期
其它都是复用。

前置、后置 ++、–的实现

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

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

这里没什么新奇的,就是对之前实现过的函数进行复用。
这里计算两个日期之间差多少的实现就比较暴力,用count计数,直到从小的日期加到和大的日期相等为止。

自己实现类对象的cout和cin

ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "-" << d._month << "-" << d._day << endl;
	return out;
}
istream& operator>>(istream& in,Date& d)
{
	int year = 0;
	int month = 0;
	int day = 0;
	cin >> year>> month >> day;
	d._year = year;
	d._month = month;
	d._day = day;

	return cin;
}

获取当天的星期的实现

void Date::GetWeekendDay()
{

	const char* weekday[] = { "星期一","星期二" ,"星期三" ,"星期四" ,
		"星期五" ,"星期六" ,"星期日" };
	int count = *this - Date(1,1,1);
	cout << weekday[count % 7] << endl;

}

以公元元年(星期一)为基准,计数公元元年到要计数日期相差的天数,然后%7。

完整版日期类

Date.h

#pragma once
#include 
using namespace std;

int GetMonthDay(int year, int month);

class Date
{
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	Date(int year = 1, int month = 1, int day = 1);
	Date(const Date& d);
	~Date();
	void DatePrint();

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

	Date operator+(int day);
	Date operator-(int day);
	Date& operator+=(int day);
	Date& operator-=(int day);

	Date& operator++();
	Date operator++(int);
	Date& operator--();
	Date operator--(int);

	int operator-(const Date& d);

	void GetWeekendDay();
private:
	int _year;
	int _month;
	int _day;
};

ostream& operator<<(ostream& out,const Date& d);
istream& operator>>(istream& in,Date& d);

Date.cpp

#include "Date.h"
int GetMonthDay(int year, int month)
{
	int a[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	int day = a[month];
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
	{
		day++;
	}
	return day;

}
Date::Date(int year , int month , int day )
{
	_year = year;
	_month = month;
	_day = day;
}
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}
Date::~Date()
{
	_year = 0;
	_month = 0;
	_day = 0;
}
void Date::DatePrint()
{
	cout << _year << "-" << _month << "-" << _day << endl;
}

bool Date::operator>(const Date& d)
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year)
	{
		if (_month > d._month)
		{
			return true;
		}
		else if(_month == d._month)
		{
			if (_day > d._day)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}

	}
	else
	{
		return false;
	}
}
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 || *this == d;
}
bool Date::operator<=(const Date& d)
{
	return !(*this > d);
}
bool Date::operator!=(const Date& d)
{
	return !(*this == d);
}

Date Date::operator+(int day)
{

	Date ret = *this;

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

			correct code after revision
			//ret._month--;
			//if (ret._month < 1)
			//{
			//	ret._month = 12;
			//	ret._year--;
			//}
			//ret._day += GetMonthDay(ret._year, ret._month);

			//others people wrote correct code
			if (ret._month == 1)
			{
				ret._month = 12;
				ret._year--;
				ret._day += GetMonthDay(ret._year, ret._month);
			}
			else
			{
				ret._month--;
				ret._day += GetMonthDay(ret._year, ret._month);
			}

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

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

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

ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "-" << d._month << "-" << d._day << endl;
	return out;
}
istream& operator>>(istream& in,Date& d)
{
	int year = 0;
	int month = 0;
	int day = 0;
	cin >> year>> month >> day;
	d._year = year;
	d._month = month;
	d._day = day;

	return cin;
}

void Date::GetWeekendDay()
{

	const char* weekday[] = { "星期一","星期二" ,"星期三" ,"星期四" ,
		"星期五" ,"星期六" ,"星期日" };
	int count = *this - Date(1,1,1);
	cout << weekday[count % 7] << endl;

}

Test.cpp

#include "Date.h"
void TestDate1()
{
	Date d1(2022, 10, 24);
	Date d2(2022, 10, 23);
	Date d3(2022, 10, 24);
	cout << "d1:";
	d1.DatePrint();
	cout << "d2:";
	d2.DatePrint();
	cout << "d3:";
	d3.DatePrint();

	cout << "Test>" << endl;
	cout << (d2>d1) << endl;
	cout << (d1 > d2) << endl << endl;

	cout << "Test==" << endl;
	cout << (d1 == d2) << endl;
	cout << (d1 == d3) << endl << endl;

	cout << "Test<" << endl;
	cout << (d1 < d3) << endl;
	cout << (d2 < d1) << endl << endl;

	cout << "Test>=" << endl;
	cout << (d1 >= d3) << endl;
	cout << (d2 >= d1) << endl << endl;

	cout << "Test<=" << endl;
	cout << (d1 <= d3) << endl;
	cout << (d2 <= d1) << endl << endl;

	cout << "Test!=" << endl;
	cout << (d1 != d3) << endl;
	cout << (d2 != d1) << endl << endl;
}
void TestDate2()
{
	Date d1(2022, 10, 24);
	
	cout << "Test+" << endl;
	Date ret1 = d1 + 4;
	ret1.DatePrint();
	Date ret2 = d1 + 30;
	ret2.DatePrint();
	Date ret3 = d1 + 150;
	ret3.DatePrint();
	Date ret4 = d1 + 365;
	ret4.DatePrint();
	Date ret5 = d1 + 1500;
	ret5.DatePrint();

	cout << "Test-" << endl;
	Date ret6 = d1 - 4;
	ret6.DatePrint();
	Date ret7 = d1 - 1500;
	ret7.DatePrint();

	cout << "Test+=" << endl;
	d1 += 1500;
	d1.DatePrint();

	cout << "Test-=" << endl;
	d1 -= 1500;
	d1.DatePrint();
}
void TestDate3()
{
	Date d1(2022, 10, 24);
	cout << "Testprev++" << endl;
	Date ret1 = ++d1;
	ret1.DatePrint();

	cout << "Testafter++" << endl;
	Date ret2 = d1++;
	ret2.DatePrint();
	d1.DatePrint();

	cout << "Testprev--" << endl;
	Date ret3 = --d1;
	ret3.DatePrint();

	cout << "Testafter--" << endl;
	Date ret4 = d1--;
	ret4.DatePrint();
	d1.DatePrint();
}
void TestDate4()
{
	Date d1(2022, 10, 23);
	Date d2(2022, 10, 24);
	cout << "Test Date - Date " << endl;
	cout << (d1 - d2) << endl;
	Date d3(1, 1, 1);
	cout << "Test Date - Date " << endl;
	cout << (d1 - d3) << endl;
	Date d4(1901, 1, 1);
	cout << "Test Date - Date " << endl;
	cout << (d1 - d4) << endl;

}
void TestDate5()
{
	Date d1(2022, 10, 24);
	Date d3(2012, 12, 31);
	//Date d2;

	//d1 << cout;
	//cout << d1;
	//cin >> d2;
	//d2.DatePrint();

	d1.GetWeekendDay();
	d3.GetWeekendDay();
}
void TestDate6()
{
	Date d1(2022, 10, 24);
	cout << "Test + " << endl;
	cout << (d1 + (-100));

	cout << "Test - " << endl;
	cout << (d1 - (-100));
}
int main()
{
	//TestDate1();
	//TestDate2();
	//TestDate3();
	TestDate4();
	//TestDate5();
	//TestDate6();
	
	return 0;
}

你可能感兴趣的:(C++初阶和高阶,1024程序员节)