我与C++的爱恋:日期计算器

本文详细介绍了如何在C++中通过类Date实现一个日期计算器,包括成员函数如获取月份天数、日期合法性检查,以及各种运算符重载,如比较、加减运算和自增自减操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

🔥个人主页guoguoqiang. 🔥专栏我与C++的爱恋

Alt

朋友们大家好啊,在我们学习了默认成员函数后,我们通过上述内容,来实现一个简易的日期计算器。


头文件的声明

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

#include <assert.h>
class Date {
   
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	Date(int year = 1900, int month = 1, int day = 1);
	void Print() const;
	//直接定义类里面,它默认是inline
	//频繁调用
	int GetMonthDay(int year,int month) {
   
		assert(month > 0 && month < 13);
		static int monthDayArray[13] = {
    -1,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 monthDayArray[month];
		}
	}
	bool CheckDate();

	bool operator<(const Date& d)const;
	bool operator<=(const Date& d)const;
	bool operator>(const Date& d)const;
	bool operator>=(const Date& d)const;
	bool operator==(const Date& d)const;
	bool operator!=(const Date& d)
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值