默认成员函数的练习之实现日期类

实现日期类

// Date.h
#define  _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<iostream>
#include<assert.h>

using namespace std;

class Date
{
public:
	//构造函数
	Date(int year = 1, int month = 1, int day = 1);
	// 因为日期类没有涉及资源开销,所以不需要写拷贝构造和析构函数

	//打印
	void Print();

	// 赋值重载
	Date& 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);
	bool operator!=(const Date& d);

	//查找每个月的天数,直接定义在类中,默认其是内联函数(因为要多次调用)
	int GetMonthDay(int year, int month)
	{
		assert(month > 0 && month < 13);
		//用static修饰,固定在静态区,避免每次调用函数都创建一个数组
		static int monthDayArray[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 monthDayArray[month];
	}

	//日期加等整数
	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);

private:
	int _year;
	int _month;
	int _day;
};
// Date.cpp
#define  _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"

Date::Date(int year, int month, int day) // 1.要加上访问限定符 2. 声明写缺省值,实现不写
{
	_year = year;
	_month = month;
	_day = day;
}

void Date::Print()
{
	cout << _year << "/" << _month << "/" << _day << endl;
}

Date& Date::operator=(const Date& d)// 1.加上返回值,可实现连续赋值 2.使用引用传参减少拷贝构造函数的调用
{
	if (this != &d) // 防止出现自己给自己赋值的情况
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	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
		&& _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)
{
	if (day < 0)
	{
		return *this -= -day; // 传入的日期为负数,复用-=(下面实现)
	}
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this; // 返回增加day天数后的日期
}

Date Date::operator+(int day) // 这里就不可以用引用做返回值了,原因是tmp出了函数作用域就被销毁
{
	Date tmp = *this; // 拷贝构造
	return tmp += day;
}

Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += -day;
	}
	_day -= day;
	while (_day < 0)
	{
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month); // 这个要放在后面
	}
	return *this;
}

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

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

Date Date::operator++(int) // 参数的目的是构成函数重载
{
	Date tmp = *this;
	*this += 1;
	return tmp; // 注意返回值
}

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

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

int Date::operator-(const Date& d)
{
	//假设一个大的
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d) // 说明假设错误
	{
		max = d;
		min = *this;
		flag = -1;
	}

	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}
	return n * flag;
}
//test.cpp
#define  _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"

int main()
{
	Date d1(2024, 9, 27);
	d1.Print();
	Date d2 = d1; // 使用默认的拷贝构造
	//Date d2(d1);
	d2.Print();

	Date d3(2024, 9, 28);
	//d3 = d1; // 测试赋值重载
	//d3.Print();
	Date d4(2024, 9, 30);
	d4 = d3 = d1; // 连续赋值
	d4.Print();

	bool ret = d3 > d4;
	cout << ret << endl; // 0

	Date d5(2024, 10, 1);
	Date ret2 = d5 -= 100;
	ret2.Print(); // 2024/6/23

	Date year(2025, 1, 29);
	int ret3 = d3 - year;
	cout << ret3 << endl;

	Date birth(2005, 8, 23); // 可计算你在这个世界上已经存活多少天
	int ret4 = d3 - birth;
	cout << ret4 << endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值