ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++:时间日期类,增加若干秒

2022-01-17 21:59:22  阅读:144  来源: 互联网

标签:temp int 成员 C++ 日期 DateTimeType time date 若干


问题:

自定义一个日期时间类DateTimeType,它含有类DateType与类TimeType的类对象作为其数据成员,并具有所列的其他几个成员函数。而后编制主函数,说明DateTimeType的类对象,并对其成员函数以及二对象成员所属类的公有成员函数进行使用。

class DateTimeType {  //自定义的日期时间类 DateTimeType

DateType date; //类 DateType 的类对象 date 作为其数据成员

TimeType time; //类 TimeType 的类对象 time 作为其另一个数据成员

public:

DateTimeType(int y0=1, int m0=1, int d0=1, int hr0=0, int mi0=0, int se0=0);

//构造函数,设定 DateTimeType 类对象的日期时间,并为各参数设置了默认值

DateType& GetDate(){ return date; } //返回本类的私有数据对象 data

TimeType& GetTime(){ return time; } //返回本类的私有数据对象 time

void IncrementSecond(int s);  //增加若干秒,注意“进位”问题

void PrintDateTime(); //屏幕输出日期时间对象的有关数据

};

注意,每一个DateTimeType类对象中总包含有一个DateType类对象(对象成员)以及一个TimeType类对象(对象成员),编制与实现本程序时,也必须包含DateType与TimeType自定义类(类型)。

之所以设置了公有的类成员函数GetDate与GetTime,是为类外如主函数处使用该类的私有数据成员date与time提供方便(否则的话,类外无法直接访问该类的私有数据成员)。另外,两成员函数返回的都为引用,为的是可将返回对象当作一个独立变量来使用(如可以继续作左值等)。例如,假设编制了如下形式的主函数:

int main() {

DateTimeType dttm1(1999,12,31,23,59,59), dttm2;

(dttm1.GetDate()).PrintDate(); //调用对象成员所属类的公有成员函数

cout<<endl;

dttm1.PrintDateTime(); //调用本派生类的成员函数 PrintDateTime

dttm2.PrintDateTime();

dttm1.IncrementSecond(30) ; //调用本派生类成员函数

dttm1.PrintDateTime();

}

代码: 

#include<iostream>
using namespace std;
//自定义的日期类 
class DateType
{
	public:
		//年,月,日 
		int year,month,day;
		void PrintDate()
		{
			cout<<year<<"-"<<month<<"-"<<day;
		}
};
//自定义的时间类 
class TimeType
{
	public:
		//时,分,秒 
		int hour,minute,second;
};
//自定义的日期时间类 DateTimeType
class DateTimeType
 {  
 	//类 DateType 的类对象 date 作为其数据成员
	DateType date; 
	//类 TimeType 的类对象 time 作为其另一个数据成员
	TimeType time; 
	public:
		//构造函数,设定 DateTimeType 类对象的日期时间,并为各参数设置了默认值
		DateTimeType(int y0=1, int m0=1, int d0=1, int hr0=0, int mi0=0, int se0=0);
		//返回本类的私有数据对象 data
		DateType& GetDate()
		{ 
			return date; 
		} 
		//返回本类的私有数据对象 time
		TimeType& GetTime()
		{ 
			return time; 
		} 
		//增加若干秒,注意“进位”问题
		void IncrementSecond(int s);  
		//屏幕输出日期时间对象的有关数据
		void PrintDateTime(); 
};
//构造函数,设定DateTimeType类对象的日期时间 
DateTimeType::DateTimeType(int y0, int m0, int d0, int hr0, int mi0, int se0)
{
	date.year=y0;
	date.month=m0;
	date.day=d0;
	time.hour=hr0;
	time.minute=mi0;
	time.second=se0;
}

void DateTimeType::IncrementSecond(int s)
{
	int mon[13],temp,m=date.month;
	//每个月份的天数 
    mon[1]=mon[3]=mon[5]=mon[7]=mon[8]=mon[10]=mon[12]=31;
    mon[2]=28;
	mon[4]=mon[6]=mon[9]=mon[11]=30;
	//判断是否是闰年 
	if(date.year%100==0&&date.year%4==0||date.year%400==0)
		mon[2]=29;
	//增加若干秒 
    time.second+=s; 
    if(time.second>=60)
    {
    	temp=time.second/60;
    	time.minute+=temp;
    	time.second-=(60*temp);
    	if(time.minute>=60)
    	{
    		temp=time.minute/60;
    		time.hour+=temp;
    		time.minute-=(60*temp);
    		if(time.hour>=24)
    		{
    			temp=time.hour/24;
    			date.day+=temp;
				time.hour-=(24*temp);
				while(date.day>mon[date.month])
				{
					m++;
					temp=date.day/mon[date.month];
					date.day-=mon[date.month];
					if(date.month==12)
					   date.month=1;
					else date.month++;
				} 
				if(m>12)
				{
					temp=m/12;
					date.year+=temp;
				}
			}
		}
	}
}

void DateTimeType::PrintDateTime()
{
	cout<<date.year<<"-"<<date.month<<"-"<<date.day<<"  "<<time.hour<<":"<<time.minute<<":"<<time.second<<endl;
}

int main() 
{
	DateTimeType dttm1(1999,12,31,23,59,59), dttm2;
	//调用对象成员所属类的公有成员函数
	(dttm1.GetDate()).PrintDate(); 
	cout<<endl;
	//调用本派生类的成员函数 PrintDateTime
	dttm1.PrintDateTime(); 
	dttm2.PrintDateTime();
	 //调用本派生类成员函数
	dttm1.IncrementSecond(30) ;
	dttm1.PrintDateTime();
}

运行结果:

标签:temp,int,成员,C++,日期,DateTimeType,time,date,若干
来源: https://blog.csdn.net/qq_45959399/article/details/122516383

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有