C C++最新用C++实现一个日期类_c++怎么格式化输出月份(1),踩坑了

img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

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;
};


## 3. 具体代码实现


头文件



//date.h文件
#pragma once
#include
using namespace std;

class Date
{
//友员声明
//流输出
friend ostream& operator<<(ostream& out, const Date&d);
//流输入
friend istream& operator>>(istream& in, Date&d);
public:
Date(int year = 1970, int month = 1, int day = 1)
:_year(year)
, _month(month)
, _day(day)
{}
// 拷贝构造,这里不显式写法也够用
//Date(const Date&d)
//{
// _year = d._year;
// _month = d._month;
// _day = d._day;
//}

//析构函数,这里不写也够用
//~Date()
//{
// \_year = 0;
// \_month = 0;
// \_day = 0;
//}
int GetMonthDay(int year, int month)
{
	static int MonthDayArry[13] = { 0, 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 MonthDayArry[month];
	}
}

bool operator==(Date&d)const;

bool operator!=(Date&d)const;

bool operator>(Date&d)const;

bool operator>=(Date&d)const;

bool operator<(Date&d)const;

bool operator<=(Date&d)const;

Date& operator+=(int day);

Date& operator-=(int day);

Date operator+(int day)const;

Date operator-(int day)const;

int operator-(Date&d)const;

//前置++
Date& operator++();
//后置++
Date operator++(int);

//前置--
Date& operator--();
//后置--
Date operator--(int);

Date& operator=(const Date& d);

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


功能函数实现文件



#include"Date.h"

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

bool Date::operator!=(Date&d)const
{
return !(*this == d);
}

bool Date::operator>(Date&d)const
{
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;
}
else
{
return false;
}
}

bool Date::operator>=(Date&d)const
{
return *this == d
|| *this > d;
}

bool Date::operator<(Date&d)const
{
return !(*this>=d);
}

bool Date::operator<=(Date&d)const
{
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)
{
_day -= day;
while (_day <= 0)
{
_month–;
if (_month == 0)
{
_year–;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}

Date Date::operator+(int day)const
{
Date tem(*this);

img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

W-1715731498926)]
[外链图片转存中…(img-iQdF4VOh-1715731498927)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值