用C++实现一个日期类_c++怎么格式化输出月份,2024年最新2024年阿里大数据开发岗面试必问

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新大数据全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

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

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

如果你需要这些资料,可以添加V获取:vip204888 (备注大数据)
img

正文

那就默认是前置++(–),如果发现参数中有一个 int 类型的形参,那么就会处理成后置的++(–),但是这里的 int形参什么事情都不做,只是为了区分前置和后置


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

3. 具体代码实现

头文件

//date.h文件
#pragma once 
#include<iostream>
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)


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

**需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注大数据)**
![img](https://img-blog.csdnimg.cn/img_convert/45197b537f2955cc0db601cf57656e17.png)

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

operator-=(int day)


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

**需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注大数据)**
[外链图片转存中...(img-nwlcGAcf-1713417673461)]

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

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值