实现日期类

                                                

🔥个人主页北辰水墨

🔥专栏C++学习仓

Alt

本节内容我们来讲解日期类的实现,文末会赋上代码

一、日期类的实现框架:

class Date
{
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month);

  // 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1);


  // 拷贝构造函数
// d2(d1)
Date(const Date& d);

   
  // 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
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);

private:
    int _year;
    int _month;
    int _day;
};

 二、成员变量:

class Date
{
private:
    int _year;
    int _month;
    int _day;
};

 三、日期类的构造和析构

3.1 构造函数:

全缺省+初始化列表

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

3.2 拷贝构造: 

	// 拷贝构造函数 -- d2(d1)
	Date(const Date& d)
		:_year(d._year)
		,_month(d._month)
		,_day(d._day)
	{}

3.3 析构函数:

由于日期类的成员变量都是内置类型,不需要释放空间,就不需要写析构函数!

四、日期的加减:

4.1   日期+=天数

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

4.2  日期+天数 (复用operator+=)

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

 4.3 日期-=天数

(1)这一块不能复用d+= -day;

         由于operator+=内部是判断_day>  GetMonthDay(_year, _month) 一个大于0的数

         复用的话,会导致_day小于0

(2)operator-= 内部实现,需要先_month--;

          因为要获取上一个月的天数

Date& operator-=(int day)
{
	_day -= day;
	while (_day < 0)
	{
		_month--;
		if(_month==0) _day += GetMonthDay(_year, 12);
		else _day += GetMonthDay(_year, _month);
		while (_month == 0)
		{
			_month = 12;
			_year--;
		}
	}
	return *this;
}

 4.4  日期-天数 (复用operator-=)

// 日期-天数
Date operator-(int day)
{
	Date d=*this;
	d -= day;
	return d;
}

4.5 前置++和前置-- (复用operator+=和operator-=)

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

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

4.6 后置++和后置-- (参数int是起到标记作用,区分前置和后置)

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

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

五、日期的比较:

5.1  >运算符重载

	// >运算符重载
	bool operator>(const Date& d)
	{
		if (_year > d._year) return 1;
		else if (_year < d._year) return 0;
		else
		{
			if (_month > d._month) return 1;
			else if (_month < d._month) return 0;
			else
			{
				if (_day > d._day) return 1;
				else return 0;
			}
		}
	}

5.2  ==运算符重载

	// ==运算符重载
	bool operator==(const Date& d)
	{
		if (_year == d._year && _month == d._month && _day == d._day) return 1;
		else return 0;
	}

5.3 其他运算符重载都是复用operator> 和 operator==  

// >=运算符重载
bool operator >= (const Date& d)
{
	if (*this > d || *this == d) return 1;
	else return 0;
}

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

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

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

 六、日期类之间的比较

	// 日期-日期 返回天数
	int operator-(const Date& d)
	{
        //获得max和min的日期
		Date max = *this;
		Date min = d;
		int flage = 1;  // flage = 1 或者 -1 表示*this大于还是小于d
		if (*this < d)
		{
			max = d;
			min = *this;
			flage = -1;
		}

        //差值
		int n = 0;

        //不相等时,++小的日期,++差值。
		while (min != max)
		{
			++min;
			++n;
		}

        //返回
		return flage * n;
	}

 七、实现日期类:

 头文件 date.h

#pragma once
#include<iostream>
class Date
{
public:
	// 获取某年某月的天数
	int GetMonthDay(int year, int month)
	{
		int day[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		int runnian = 0;
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
		{
			runnian = 1;
		}
		if (_month == 2) return day[month] + runnian;
		else  return day[month];
	}


	// 全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1)
		:_year(year)
		,_month(month)
		,_day(day)
	{}

	// 拷贝构造函数
	 // d2(d1)
	Date(const Date& d)
		:_year(d._year)
		,_month(d._month)
		,_day(d._day)
	{}

	// 赋值运算符重载

  // d2 = d3 -> d2.operator=(&d2, d3)

	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)
			{
				_month = 1;
				_year++;
			}
		}
		return *this;
	}


	// 日期+天数
	Date operator+(int day)
	{
		Date d = *this;
		d += day;
		return d;
	}


	// 日期-天数
	Date operator-(int day)
	{
		Date d=*this;
		d -= day;
		return d;
	}


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


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

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

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

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

	// >运算符重载
	bool operator>(const Date& d)
	{
		if (_year > d._year) return 1;
		else if (_year < d._year) return 0;
		else
		{
			if (_month > d._month) return 1;
			else if (_month < d._month) return 0;
			else
			{
				if (_day > d._day) return 1;
				else return 0;
			}
		}
	}



	// ==运算符重载
	bool operator==(const Date& d)
	{
		if (_year == d._year && _month == d._month && _day == d._day) return 1;
		else return 0;
	}


	// >=运算符重载
	bool operator >= (const Date& d)
	{
		if (*this > d || *this == d) return 1;
		else return 0;
	}

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

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

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


	// 日期-日期 返回天数
	int operator-(const Date& d)
	{
		Date max = *this;
		Date min = d;
		int flage = 1;
		if (*this < d)
		{
			max = d;
			min = *this;
			flage = -1;
		}
		int n = 0;
		while (min != max)
		{
			++min;
			++n;
		}
		return flage * n;
	}

private:
	int _year;
	int _month;
	int _day;
};

 测试文件 test.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include"date.h"
using namespace std;
void test_GetMonthDay()
{
	Date d;
	cout << d.GetMonthDay(2024, 2) << endl;
}
void test_operator1()
{
	Date d1(2024,11,05);
	Date d2;
	Date d3;
	d3 = d2 = d1;
}
void test_operator2()
{
	Date d(2023,11,5);
	d += 100;
}
void test_operator3()
{
	Date d(2023, 11, 5);
	Date d2;
	d2 = d + 100;
}
void test_operator4()
{
	Date d(2023, 11, 5);
	d -= 1000;
}
void test_operator5()
{
	Date d(2023, 11, 5);
	Date d2;
	d2 = d - 100;
}
void test_operator6()
{
	Date d1(2023, 11, 05);
	Date d2(2022, 11, 06);
	cout << "d1 < d2 : "<< (d1 < d2) << endl;
	cout << "d1 > d2 : "<< (d1 > d2) << endl;
	cout << "d1 <= d2 : "<< (d1 <= d2) << endl;
	cout << "d1 >= d2 : "<< (d1 >= d2) << endl;
	cout << "d1 != d2 : "<< (d1 != d2) << endl;
	cout << "d1 == d2 : "<< (d1 == d2) << endl;
	cout << "d1 - d2 : " << (d1 - d2) << endl;
}
int main()
{
	test_GetMonthDay();
	test_operator1();
	test_operator2();
	test_operator3();
	test_operator4();
	test_operator5();
	test_operator6();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值