C++日期类的实现

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

必须有的:年、月、日

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

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

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

cpp 复制代码
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}

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

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

1.拷贝构造函数

需要显示的写吗?

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

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

cpp 复制代码
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

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

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

cpp 复制代码
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.赋值运算符重载

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

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

cpp 复制代码
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.直接输入、输出日期

cpp 复制代码
    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指针,所以需要传两个参数.

具体代码

头文件

cpp 复制代码
#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;
};

函数的具体实现

cpp 复制代码
#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;
}

测试

cpp 复制代码
#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();
}
相关推荐
被程序耽误的胡先生3 分钟前
java中 kafka简单应用
java·开发语言·kafka
刀客1234 分钟前
python小项目编程-中级(1、图像处理)
开发语言·图像处理·python
卷卷的小趴菜学编程8 分钟前
c++之多态
c语言·开发语言·c++·面试·visual studio code
冷琴199628 分钟前
基于Python+Vue开发的反诈视频宣传管理系统源代码
开发语言·vue.js·python
楠枬36 分钟前
网页五子棋——对战后端
java·开发语言·spring boot·websocket·spring
OopspoO37 分钟前
C++ 标准库——函数对象和函数适配器
c++
kyle~38 分钟前
thread---基本使用和常见错误
开发语言·c++·算法
攻城狮7号1 小时前
【第三节】C++设计模式(创建型模式)-单例模式
c++·单例模式·设计模式
坚持就完事了1 小时前
Python之numpy
开发语言·python·numpy
贩卖纯净水.1 小时前
REACT学习DAY02(恨连接不上服务器)
服务器·学习·react.js