2024年最全用C++实现一个日期类_c++怎么格式化输出月份,2024年最新C C++开发项目面试描述

img
img

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

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

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

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

tem += day;
return tem;

}

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

int Date::operator-(Date&d)const
{
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max)
{
++n;
++min;
}

return n\*flag;

}

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

//前置–
Date& Date::operator–()

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

.(img-0o6u2oBp-1715617341677)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 14
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值