c++实现日期类(class Date) 构造函数 拷贝构造 操作符重载(输入输出 比较操作 算数运算 自增自减)

本文档提供了一个C++实现的日期类,包括构造函数、拷贝构造函数以及输入输出、比较和算术运算的操作符重载。通过主函数进行了一些基本测试,适合初学者学习。
摘要由CSDN通过智能技术生成


注释比较详细,可以直接跑起来,直接上代码(vs2012 win7)


一、头文件


/**************
	Date.h
***************/

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

class Date
{
private:
	int my_iYear;
	int my_iMonth;
	int my_iDay;
	int GetOneMonthDays(int year, int month)const;//获取某个月的天数

public:
	Date()
		:my_iYear(1900)
		,my_iMonth(1)
		,my_iDay(1)
	{
	}
	Date(int year, int month, int day);// 构造函数
	Date(const Date& d);		       // 拷贝构造函数
	~Date(){}					       // 析构函数
	
	int GetYear()const;		// 返回年份
	int GetMonth()const;	// 返回月份
	int GetDay()const;		// 返回天数

	void Print()const;			        // 输出日期
	bool IsLeapYear()const;				// 判断当前对象年是否是闰年
	bool IsLeapYear(const int y)const;  // 判断指定年份是否是闰年

	// 操作符重载部分

	// 天数 + 日期
	friend Date operator+(const  int d, const Date date);

	// 日期 +  天数
	friend Date operator + (const Date date, const   int d);

	// 前置 ++ 
	friend Date& operator ++ (Date& date);

	// 后置 ++   多一个int参数与前置区别
	friend Date operator ++ (Date& date, int);

	// 重载 +=
	friend Date operator +=(Date& date, const int d);

	// 日期 - 天数
	friend Date operator - (const Date date, const int d);

	// 天数 - 日期
	friend Date operator - (const int d, const Date date);

	// 前置 --
	friend Date& operator -- (Date& date);

	// 后置 -- 
	friend Date operator -- (Date& date, int);
	
	// 重载 -=
	friend Date operator -=(Date& date, const int d);

	// 日期 - 日期
	friend int operator - (const Date a, const Date b);

	// 重载比较操作符
	friend bool operator< (const Date a, const Date b);

	friend bool operator<= (const Date a, const Date b);

	friend bool operator> (co
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值