C++:日期类的构建

目录

前言

功能简介

类的构建


前言

这是一篇练习博客,在学习和初步熟悉了类的构建,以及类的六个默认构造函数之后,构建了一个日期类,对日期进行各种操作。

功能简介

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;

};

类的构建

#pragma once
#include<iostream>
using namespace std;


class Date
{
public:
	int GetMonthDay(int year, int month)const;
	//全缺省默认构造函数
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	//拷贝构造函数,注意要加const,在遇到不能修改的数据时非常必要,临时变量具有常性
	Date(const Date& d1)
	{
		_year = d1._year;
		_month = d1._month;
		_day = d1._day;
	}

	//赋值操作符重载
	Date& operator=(const Date& d1)
	{
		_year = d1._year;
		_month = d1._month;
		_day = d1._day;
		return *this;
	}

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

	//日期加天数, + 重载
	Date operator+(int day)
	{
		Date d1(*this);
		d1._day += day;
		int temp = 0;
		while (d1._day > (temp = GetMonthDay(d1._year, d1._month)))
		{
			d1._day -= temp;
			if (d1._month < 12)
			{
				d1._month++;
			}
			else
			{
				d1._year++;
				d1._month = 1;
			}
		}
		return d1;
	}
	//日期减天数, -=
	Date& operator-=(int day)
	{
		while (_day <= day)
		{
			if (_month == 1)
			{
				_year--;
				_month = 12;
			}
			else
			{
				_month--;
			}
			day -= _day;
			_day = GetMonthDay(_year, _month);
		}
		_day -= day;
		return *this;
	}

	//日期减天数, -
	Date operator-(int day)const
	{
		Date d1(*this);
		while (d1._day <= day)
		{
			if (d1._month == 1)
			{
				d1._year--;
				d1._month = 12;
			}
			else
			{
				d1._month--;
			}
			day -= d1._day;
			d1._day = GetMonthDay(d1._year, d1._month);
		}
		d1._day -= day;
		return d1;
	}

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

		return d1;
	}

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

	//后置--
	Date operator--(int)
	{
		Date d1(*this);
		*this -= 1;

		return d1;
	}

	//运算符重载>
	bool operator>(const Date& d1)const
	{
		return (_year > d1._year)
			|| (_year == d1._year && _month > d1._month)
			|| (_year == d1._year && _month == d1._month && _day > d1._day);
	}

	//运算符重载==
	bool operator==(const Date& d1)const
	{
		return (_year == d1._year && _month == d1._month && _day == d1._day);
	}

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

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

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

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

	//日期相减
	int operator-(const Date& d1)const
	{
		int ret = 0;
		if (*this <= d1)
		{
			Date d2(*this);
			while (d2 != d1)
			{
				d2 += 1;
				ret--;
			}
			return ret;
		}
		else
		{
			Date d2(d1);
			while (d2 != *this)
			{
				d2 += 1;
				ret++;
			}
			return ret;
		}
	}

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



int Date::GetMonthDay(int year, int month)const
{
	static int MonthDayList[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	//int MonthDayList[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month == 2 && ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)))
	{
		return 29;
	}
	else
	{
		return MonthDayList[month];
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JDSZGLLL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值