C++:实现一个Date类

C++完成日期类的实现——Date //dateclass.h #pragma once #include<iostream> #include"assert.h" class Date { public: Date(int year = 1900, int month = 1, int day = 1) :_year(year) , _month(month) , _day(d 阅读详情

关于日期类,我们最基本的成员变量就是三个:年、月、日。关于成员函数我们要实现构造,拷贝构造,赋值,关于日期的比较大小,以及日期加天数,日期减天数,以及+=和- =,同时还要考虑能否复用,日期减日期,还有日期类的++和- -(分为前置和后置)等。

具体代码如下:详情请看代码注释

Date.h
#pragma once
#include <iostream>
class Date
{
public:
	void Print()const;
	int Date::GetMonthDay(int year, int month)const;
	//构造函数
	Date(int year = 1900, int month = 1, int day = 1);
	//析构函数
	~Date();
	//拷贝构造Date d2(d1);
	Date(const Date& d);
	//赋值运算符重载
	Date& operator=(const Date& d);
	//比较日期
	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)const;
	//关于日期的加减
	Date operator+(int day)const;//日期加天数,原日期不变,因此要构造临时对象,要传值返回
	Date& operator+=(int day);//日期加上天数,把日期改变,直接传引用
	Date operator-(int day)const;//日期减天数,原日期不变
	Date& operator-=(int day);//日期减去天数,把日期改变
	int operator-(Date& d)const;//日期减日期,返回值是天数int
	//前置++(d.operator++(&d))
	Date& operator++();
	//后置++(d.operator++(&d,0))
	Date operator++(int);
	//前置--(d.operator--(&d))
	Date& operator--();
	//后置--(d.operator--(&d,0))
	Date operator--(int);
private:
	int _year;
	int _month;
	int _day;
};
Date.cpp
#include "Date.h"
//打印
void Date::Print()const
{
	std::cout << _year << "-" << _month << "-" << _day << std::endl;
}

//获取月的天数
int Date::GetMonthDay(int year, int month)const
{
	static int array[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	//是闰年的话2月返回29天
	if ((month == 2) && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		return 29;
	}
	return array[month];
}

//构造函数
Date::Date(int year, int month, int day)
{
	//判断年月日是否合法
	if (year > 0 && 
		month > 0 && month<13 && 
		day>0 && day <= GetMonthDay(year, month))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		std::cout << "Date Invalid" << std::endl;
	}
}

//析构函数
Date::~Date()
{}

//拷贝构造Date d2(d1);
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

//赋值运算符重载
Date& Date::operator=(const Date& d)
{
	if (this != &d)//防止自己给自己赋值
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;//可以支持连续赋值
}

//判断日期大小或相等
bool Date::operator==(const Date&d)const
{
	return _year == d._year 
		&& _month == d._month 
		&& _day == d._day;
}
bool Date::operator!=(const Date&d)const
{
	return !(*this == d);
}
bool Date::operator>(const Date&d)const
{
	return (_year > d._year)
		|| (_year == d._year&&_month > d._month)
		|| (_year == d._year&&_month == d._month&&_day > d._day);
}
bool Date::operator<(const Date&d)const
{
	return !(*this > d);
}
bool Date::operator>=(const Date&d)const
{
	return (*this > d) || (*this == d);
}
bool Date::operator<=(const Date&d)const
{
	return (*this < d) || (*this == d);
}

//关于日期的加减

////d1+100----d1不发生改变
////不复用实现+
//Date Date::operator+(int day)const
//{
//	Date ret(*this);
//	ret._day += day;
//	while (ret._day > GetMonthDay(ret._year, ret._month))//大于该月天数就不合法
//	{
//		ret._day -= GetMonthDay(ret._year, ret._month);
//		ret._month++;
//		if (ret._month == 13)
//		{
//			ret._year++;
//			ret._month = 1;
//		}
//	}
//	return ret;
//}

//复用+=实现+
Date Date::operator+(int day)const
{
	Date ret(*this);
	ret += day;
	return ret;
}

////d1-100----d1不发生改变
////不复用实现-
//Date Date::operator-(int day)const
//{
//	Date ret(*this);
//	ret._day -= day;
//	while (ret._day <= 0)//小于和等于0都不合法
//	{
//		--ret._month;//借的是上个月的天数
//		if (ret._month == 0)
//		{
//			ret._year--;
//			ret._month = 12;
//		}
//		ret._day += GetMonthDay(ret._year, ret._month);		
//	}
//	return ret;
//}

//复用-=实现-
Date Date::operator-(int day)const
{
	Date ret(*this);
	ret -= day;
	return ret;
}

//实现+=
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		return *this -= -day;
	}
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}

//实现-=
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += -day;
	}
	_day -= day;
	while (_day <= 0)
	{
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}

//d1-d2
int Date::operator-(Date& d)const//日期减日期,返回值是天数int
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d)
	{
		Date max = d;
		Date min = *this;
		flag = -1;
	}
	int num = 0;
	while (max != min)
	{
		min++;
		num++;
	}
	return num;
}

//前置++(d.operator++(&d))
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
//后置++(d.operator++(&d,0))
Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}

//前置--(d.operator--(&d))
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
//后置--(d.operator--(&d,0))
Date Date::operator--(int)
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}
main.cpp
#include "Date.h"

int main()
{
	Date d1;//默认构造
	d1.Print();
	Date d2(2019, 1, 1);//构造
	d2.Print();
	Date d3(d2);//拷贝构造
	d3.Print();
	Date d4;
	d4.Print();
	d4 = d3;//赋值
	d4.Print();

	Date (2016, 2, 29).Print();//匿名对象

	Date d5(2017, 10, 4);//日期加100天
	Date d6 = d5 + 100;
	d6.Print();

	Date d7(2018,1,12);//日期减100天
	Date d8 = d7 - 100;
	d8.Print();

	Date d9(1900, 11, 20);//+=
	d9 += 100;
	d9.Print();

	Date d10(1901, 2, 28);//-=
	d10 -= 100;
	d10.Print();

	Date d11(2019, 2, 27);//日期减日期
	Date d12(2019, 7, 15);
	int ret = d12 - d11;
	std::cout << ret << std::endl;

	//++
	Date d13(2019, 8, 8);
	(++d13).Print();//2019-8-9
	(d13++).Print();//2019-8-9
	d13.Print();//2019-8-10

	system("pause");
	return 0;
}
日期类(Date class)c++版 c++设计的日期类,包含以下方法:设置日期,加上一个天数获得另一个日期,求两个日期相差的天数,计算某天是星期几。(新手,不足之处,请多多指教) 立即下载

相关推荐

C++实现日期类(Date类)的方法

下面小编就为大家带来一篇C++实现日期类(Date类)的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

【C++】C++实现Date日期类

类和对象--日期类 C++学习笔记--类和对象4

m0_72563041的博客 2785

自由曲面与衍射光学元件DOE:从基础理论到工业应用的全景解析

本文全景解析了自由曲面与衍射光学元件的核心理论与工业应用。文章深入探讨了自由曲面在非对称光学设计中的灵活性及其数学描述与制造挑战,并阐述了衍射光学元件通过微纳结构进行波前相位调制的原理与多样化功能。两者与经典光学互补融合,共同构成了高端成像、智能照明、激光加工及AR/VR等前沿领域的底层技术支柱。

devops8pract的博客 392

【C++】完成Date类 ( 完整代码)

Date类完成的主要功能 1.四个成员函数;构造函数,析构函数,拷贝构造函数,赋值重载(=) 2.重载符号:+,-,+=,-=,&amp;amp;lt;,&amp;amp;gt;,&amp;amp;lt;=,&amp;amp;gt;=,==,!= 3.前置++,前置–,后置++,后置– 完整代码 #define _CRT_SECURE_NO_WARNINGS 1 #include &amp;amp;lt;iostream&amp;amp;gt;

ly_6699的博客 1227

Cadence Virtuoso IC617:从零构建传输门与反相器的Schematic设计实战

本文详细介绍了使用Cadence Virtuoso IC617进行传输门与反相器Schematic设计的完整流程,包括环境准备、原理图绘制、Symbol创建、仿真验证及实用技巧。通过分步指导和实战经验分享,帮助初学者快速掌握集成电路设计基础,提升EDA工具应用能力。

weixin_29194693的博客 600

OpenEvidence DeepConsult深度分析:技术架构、临床应用与行业影响

摘要:医疗AI平台OpenEvidence通过多智能体协作系统重构临床决策流程,解决了医学信息过载难题。其DeepConsult系统严格基于同行评审文献,5-10秒内提供循证决策支持,已覆盖40%美国医生,月咨询量达850万次。该平台采用"免费工具+广告变现"模式,通过调度、检索、摘要等模块化智能体分工,实现从文献分析到临床建议的闭环处理,在USMLE测试中准确率达90%。2025年获7500万美元融资后估值突破10亿美元,成为医疗AI领域的新范式。

AllenLV的博客 2556

C++设计一:日期类Date实现

本文设计并实现了一个基于C++的日期类Date,支持日期的基本操作与运算。

shuibuxingaaa的博客 2568

软考-高级-信息系统项目管理第四版(完整24章全笔记)

包 括:信息系统、业务应用信息系统、信息安全系统、信息系统工程、业务应用信息系统工程和 信息安全系统工程等业务应用信息系统是支撑业务运营的计算机应用信息系统,信息安全系统工程是指为了达到建设好信息安全系统的特殊需要而组织实施的工程。项目经理由执行组织委派, 负责领导团队实现项目目标。①制定项目章程:②制订项目管理计划:③指导与管理项目工作:④管理项目知识:⑤监控项目工作:⑥实施整体变更控制:项目范围管理过程包括:规划范围管理;收集需求;定义范围;创 建WBS;确认范围;控制范围;

weixin_44934104的博客 3万+

山东科技大学OJ Problem G: 日期类之七

英国(BritishStyle类)的日期格式是“日/月/年”、美国(AmericanStyle类)的日期格式是“月-日-年”、德国(GermanStyle类)的日期格式是“年-月-日”、西班牙(SpanishStyle类)的日期格式是“日-月-年”。Date::print()、BritishStyle::print()、AmericanStyle::print()、GermanStyle::print()、SpanishStyle::print()按格式输出日期。每组包括年、月、日三个整数。

m0_73899326的博客 291

解决windows10下无法安装.net framework 3.5(包括.net2.0和3.0)

入域电脑,win10系统中安装 .net framework 3.5(包括.net2.0和3.0)

God_me_1的博客 4万+

RAGFlow核心引擎DeepDoc之PDF解析大起底

本文深入解析RAGFlow中的deepdoc模块,重点介绍了pdf文档解析器的实现细节。deepdoc包含parser(文档解析器)和vision(视觉处理)两大核心功能,其中pdf解析是最复杂的部分。文章详细剖析了PdfParser类的设计架构,包含PlainParser(基础文本提取)、RAGFlowPdfParser(核心处理流水线)和VisionParser(视觉扩展)三个类,它们通过统一的__call__方法返回标准化结果。还具体讲解了初始化过程如何加载OCR引擎、布局识别模型、表格结构识别器等关

yxkong的专栏 2026

c语言 date如何使用方法,在C++中,date类访问日期的操作方法,都在这里了

date类的对外接口很像C语言中的tm结构,也可以获取它保存的年、月、日、星期等成分,但date还提供了更多的操作。成员函数year()、month()和day()分别返回日期的年、月、日:date d(2017,6,1);assert(d. year()==2017);assert(d. month()==6);assert(d. day()==1);成员函数year_month_day()返回...

weixin_33487811的博客 2649

大数据与宠物:健康监测数据分析

随着全球宠物市场规模的快速增长(2023年全球宠物产业规模已达3500亿美元),宠物健康管理需求呈现指数级上升。传统宠物医疗依赖定期体检和症状观察,存在滞后性和主观性缺陷。本文聚焦如何通过物联网(IoT)设备采集宠物生理数据(体温、心率、活动量等),利用大数据分析技术构建实时健康监测系统,实现宠物健康状态的动态评估、异常预警和疾病预测。研究范围涵盖数据采集技术、数据预处理方法、机器学习模型构建及实际应用场景落地。背景介绍:明确研究目的、目标读者及核心术语核心概念与联系。

2502_91592937的博客 1068

C++ : 实现一个Date类

C++ : 实现一个Date类 #include &quot;Date.h&quot; int Date::GetMonthDay(int year, int month) //获得月份有多少天 { //1、写12个if //2、switch语句 //3、 //年份判断考虑闰年:四年一闰,百年不闰,四百年再一闰地球公转) static const int monthArray[13] = { 0, 31...

TheWindRisesll的博客 782

AUTOSAR AP——SM模块

对AUTOSAR AP中状态管理模块SM,从模块定义、总结等多方面进行了介绍。

weixin_43627682的博客 284

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

C++日期类设计与实现详解:一个完整的Date类

这个Date类展示了C++面向对象设计和运算符重载的典型应用。通过这个类,我们可以:方便地创建和操作日期对象使用直观的运算符进行日期运算处理日期相关的常见需求虽然有一些可以优化的地方,但这个实现为学习C++类和运算符重载提供了一个很好的范例。理解这个类的设计和实现,对于掌握C++面向对象编程具有重要意义。注意:实际使用中,C++11及以后的标准库提供了<chrono>和日期时间库,建议在项目中使用标准库或成熟的第三方库(如Boost.DateTime)来处理日期时间,这些库经过了充分测试和优化。

qq_67169366的博客 592

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

《 C++ 修炼全景指南:七 》实现一个功能完备的 C++ Date 类详细指南,带你一次性搞定所有关于日期类编程题

在这篇博客中,我们深入探讨了一个完备的 C++ Data 类的实现过程,详细展示了其动态管理数据的能力和高级特性。通过对类的设计进行逐步讲解,包括内存管理、构造函数、析构函数、拷贝控制、以及数据成员的封装,我们展示了如何创建一个高效且可扩展的 Data 类。为了模拟标准库的特性,我们还实现了支持动态调整大小、深拷贝、迭代器支持等功能。本文适合希望加深 C++ 数据结构理解的开发者,既提供了深入的代码分析,也包含了实际应用中的优化建议。

Lenyiin 2312

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

c++--定义类并实现类的成员函数

在c++中,如何根据给定的要求定义类并实现类的成员函数呢? 首先我们说说类的定义,一般来说,类 = 属性+方法(成员函数)。 比如这样一个日期类, class Data //日期类 { public: int year; //年 int month; //月 int day; //日 void show_Data(); //显示日期 void data_...

blank_Dot的博客 2066

c++c创建一个时间类

#include using namespace std; class Date{ private: int year; int month; int day; public: Date(int year,int month,int day) { this->year=year; this->month=month; this->day=day; } void Judge( int year, int month, int day) { if(year%4==0&&

m0_67228478的博客 1148

c++第11题

#include using namespace std; class Date; class Time {        public: Time(int,int,int); void display(Date&); private: int hour; int minute; int sec; }; class Date { public: Date(int,

张言欣 303

OJ——输出日期时间--友元类

#include using namespace std; class Date; class Time { private: int hour; int minute; int second; public: Time (int h,int m,int s):hour(h),minute(m),second(

ytdxzhaomin的专栏 448

时间类

#include using namespace std; class Date { public:     void SetDate(int y,int m,int d)     {         Year=y;         Month=m;         Day=d;     }     void PrintDate()     {         cout

l724430310的博客 332

第3章 第11题

#include using namespace std; class Date; class Time { public: Time(int,int,int); void display(Date&); private: int hour; int minute; int sec; }; class Date { public: Date(int,int,int

樊坤 415

P121 10

#include using namespace std; class Date; class Time {public: Time(int,int,int); friend void display(const Date &,const Time &); private: int hour; int minute; int sec; }; Time::Time(int

YuSong4869的专栏 319

补1

一、问题及代码 #includeiostream>     #includeiomanip>     #includecstdlib>     using namespace std;     class Date; //对Date类的提前引用声明       class Time       {       public:           Time(int,in

zyr1215的博客 273

个人作业

#include using namespace std; class Time; class Date { public: Date(int ,int ,int); friend Time; private: int day; int month; int year; }; Date::Date(int d,int m,int y) {  int day=d;

zhangrongling的专栏 215
上一篇: C++:内存管理(new和delete的使用以及底层原理、定位new表达式、malloc/free和new/delete的区别,内存泄漏)
下一篇: C++:设计一个类,只在堆上 / 栈上创建对象
ETalien_
博客等级 码龄8年 154粉丝 199原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值