C++ 计算n天后的日期

C++ 计算n天后的日期,第1张

概述C++ 计算n天后日期

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

#include <iostream>class cdate {private:	int m_nDay; // Range: 1 - 30 (lets assume all months have 30 days!)	int m_nMonth; // Range: 1 - 12	int m_nYear;	voID AddDays(int nDaysToAdd) {		m_nDay += nDaysToAdd;		if (m_nDay > 30) {			AddMonths(m_nDay / 30);			m_nDay %= 30; // rollover 30th -> 1st		}	}	voID AddMonths(int nMonthsToAdd) {		m_nMonth += nMonthsToAdd;		if (m_nMonth > 12) {			AddYears(m_nMonth / 12);			m_nMonth %= 12; // rollover dec -> jan		}	}	voID AddYears(int m_nYearsToAdd) {		m_nYear += m_nYearsToAdd;	}public:	// Constructor that initializes the object to a day,month and year	cdate(int nDay,int nMonth,int nYear) :			m_nDay(nDay),m_nMonth(nMonth),m_nYear(nYear) {	}	;	cdate operator +(int nDaysToAdd) {		cdate newDate(m_nDay,m_nMonth,m_nYear);		newDate.AddDays(nDaysToAdd);		return newDate;	}	voID displayDate() {		std::cout << m_nDay << " / " << m_nMonth << " / " << m_nYear;	}};int main() {	// Instantiate and initialize a date object to 25 May 2008	cdate mDate(25,6,2008);	std::cout << "The date object is initialized to: ";	// display initial date	mDate.displayDate();	std::cout << std::endl;	std::cout << "Date after adding 10 days is: ";	// Adding 10 (days)...	cdate datePlus10(mDate + 10);	datePlus10.displayDate();	return 0;}

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的C++ 计算n天后的日期全部内容,希望文章能够帮你解决C++ 计算n天后的日期所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1232627.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存