C++日期类的实现

要实现一个日期类我们先考虑一下他有什么成员?

必须有的:年、月、日

需要显示写构造函数和析构函数吗?

Date类的成员类型都是内置类型,编译器不一定处理

我们可以显示的写一个全缺省的构造函数

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

成员都是内置类型,那么也就是没有资源需要清理也就不需要显示的写析构函数

接下来我们再来考虑下日期类一般会有什么方法?

1.拷贝构造函数

需要显示的写吗?

不需要,编译器会自动调用默认的拷贝构造

如果要显示的写拷贝构造函数该怎么实现?

Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

拷贝构造函数名就是类名,只有一个参数,形参是类类型的引用

2.获取某个月的具体天数

int Date::GetMonthDay(int year, int month)
{
	static int MonthDay[13] = { -1,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 MonthDay[month];
	}
}

将数组的长度设置为13,这样数组的下标刚好就与月份能对应得上

别忘了,闰年的二月有29天

3.赋值运算符重载

编译器有默认的,可以不用显示写

那显示的如何实现?要注意什么?

Date& Date::operator=(const Date& d)
{
	//检查自赋值
	if (this == &d)
	{
		return *this;
	}
	_year = d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}

返回值类型:类类型的引用

形参:只有一个,形参类型也是类类型的引用

函数体中返回的是*this,this指针指向当前对象(当前日期),存放当前对象的地址,所以要返回当前对象就是返回this指针的解引用

4.各种运算符重载,不一一说明

想看详细一点的可以阅读:http://t.csdnimg.cn/ZE13g

里面一个核心的思想就是复用

5.直接输入、输出日期

    friend ostream& operator<<(ostream& os, Date& d)
	{
		os << d._year << "/" << d._month << "/" << d._day << endl;
		return os;
	}
	friend istream& operator>>(istream& is, Date& d)
	{
		is >> d._year >> d._month >> d._day;
		return is;
	}

需要注意的是他们不是日期类的成员函数,而是日期类的友元函数,返回值类型前面加了关键字friend来修饰,注意友元函数虽然可以访问类的所有成员,但是由于他本身不是类的成员函数也就没有了隐藏的this指针,所以需要传两个参数.

具体代码

头文件

#pragma once
#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 0, int month = 0, int day = 0);
	// 拷贝构造函数
	// d2(d1)
	Date(const Date& d);

	// 获取某年某月的天数
	int GetMonthDay(int year, int month);

	// 赋值运算符重载
  // d2 = d3 -> d2.operator=(&d2, d3)
	Date& 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--(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()const;
	// 析构函数(日期类无需清理资源,析构函数不必显示写)
	//~Date()
	//{
		//cout << "~Date()" << endl;
	//}
	friend ostream& operator<<(ostream& os, Date& d)
	{
		os << d._year << "/" << d._month << "/" << d._day << endl;
		return os;
	}
	friend istream& operator>>(istream& is, Date& d)
	{
		is >> d._year >> d._month >> d._day;
		return is;
	}
private:
	int _year, _month, _day;
};

函数的具体实现

#include"Date.h"
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;
}
// 赋值运算符重载
  // d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d)
{
	//检查自赋值
	if (this == &d)
	{
		return *this;
	}
	_year = d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}
int Date::GetMonthDay(int year, int month)
{
	static int MonthDay[13] = { -1,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 MonthDay[month];
	}
}
void Date::print()const
{
	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;
			}
		}
	}
	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);
}

// <=运算符重载
bool Date::operator <= (const Date& d)
{
	return !(*this > d);
}

// !=运算符重载
bool Date::operator != (const Date& d)
{
	return !(*this == d);
}
// 日期+=天数
Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
		_day -= GetMonthDay(_year, _month);
	}
	return *this;
}

// 日期+天数
Date Date::operator+(int day)
{
	Date tmp;
	tmp += day;
	return tmp;
}

// 日期-天数
Date Date::operator-(int day)
{
	Date tmp;
	tmp -= day;
	return tmp;
}

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

// 前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;
}

// 后置++
Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}

// 后置--
Date Date::operator--(int)
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}

// 前置--
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
	Date max(*this);
	Date min(d);
	int flag = 1;
	int count = 0;
	if (*this < d)
	{
		flag = -1;
		max = d;
		min = *this;
	}
	while (min < max)
	{
		min++;
		count++;
	}
	return count * flag;
}

测试

#include"Date.h"
void Test1()
{
	Date d1(2023, 10, 5), d2(2024, 6, 10);
	cout << (d1 == d2) << endl;
	cout << (d1 != d2) << endl;
	cout << (d1 <= d2) << endl;
	cout << (d1 >= d2) << endl;
	cout << (d1 < d2) << endl;
	cout << (d1 > d2) << endl;
}
void Test2()
{
	Date d1(2024, 6, 10), d2(2024, 6, 05);
	/*Date d3 = d1--;
	Date d4 = d2++;
	d3.print();
	d4.print();
	Date d5 = --d3;
	Date d6 = ++d4;
	d5.print();
	d6.print();*/
	int num = d1 - d2;
	cout << num << endl;
	cout << d1;
	cin >> d2;
}
int main()
{
	Test2();
}

相关推荐

  1. C++日期实现

    2024-06-11 21:54:02       10 阅读
  2. c++日期实现

    2024-06-11 21:54:02       6 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-11 21:54:02       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-11 21:54:02       20 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-11 21:54:02       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-11 21:54:02       20 阅读

热门阅读

  1. python数据处理分析库(二)

    2024-06-11 21:54:02       9 阅读
  2. 目前常用的后端技术

    2024-06-11 21:54:02       5 阅读
  3. 每天一个数据分析题(三百五十四)-分析报表

    2024-06-11 21:54:02       8 阅读
  4. leetcode67:二进制求和

    2024-06-11 21:54:02       7 阅读
  5. GPS原理与接收机设计

    2024-06-11 21:54:02       9 阅读
  6. vue 之 h() 函数

    2024-06-11 21:54:02       9 阅读
  7. 超宽输送带与普通输送带的区别是什么

    2024-06-11 21:54:02       9 阅读
  8. 日常练习——[BJ2011.X5] 选小寿星

    2024-06-11 21:54:02       7 阅读
  9. 设计模式大作业-服装管理系统(包调试成功)

    2024-06-11 21:54:02       9 阅读