西加加:继承和派生练习2

代码实现: (很白痴的strcat 实在想不出来惹┌( ´_ゝ` )┐

#include <iostream>
#include <string.h>
using namespace std;
 
char *itoa(int n, char *s)
{
	static int i;
	if(n/10)
	{
		s=itoa(n/10,s);
	}
	else
	{
		i=0;
	}
	s[i++]=n%10+'0';
	s[i]='\0'; 
	return s;
} 

class Time
{
	int Hour, Minute, Second;
	
	public:
		Time(int hour, int minute, int second) : Hour(hour), Minute(minute), Second(second) {}
		void SetTime(int hour, int minute, int second)
		{
			Hour = hour;
			Minute = minute;
			Second = second;
		}
		void GetTime(char *q)
		{
			q[8] = '0';
			char h[2], m[2], s[2];
			itoa(Hour, h);
			itoa(Minute, m);
			itoa(Second, s);
			strcat(h,":");
			strcat(h,m);
			strcat(h,":"); 
			strcat(h,s);
			strcpy(q,h);
		}
};

class Date
{
	int Year, Month, Day;
	
	public:
		Date(int year, int month, int day): Year(year), Month(month), Day(day) {}
		void SetDate(int year, int month, int day)
		{
			Year = year;
			Month = month;
			Day = day;
		}
		void GetDate(char *p)
		{
			p[10] = '0';
			char y[4], m[2], d[2];
			itoa(Year, y);
			itoa(Month, m);
			itoa(Day, d);
			strcat(y,"/");
			strcat(y,m);
			strcat(y,"/");
			strcat(y,d);
			strcpy(p,y);
		}
};

class DateTime : public Date, public Time
{
	public:
		DateTime(int year, int month, int day, int hour, int minute, int second) : Date(year,month,day), Time(hour,minute,second) {}
		void SetDateTime(int year0, int month0, int day0, int hour0, int minute0, int second0)
		{
			SetDate(year0,month0,day0);
			SetTime(hour0,minute0,second0);
		}
		void GetDateTime(char *t)
		{
			
			t[18]= '0';
			char p[10], q[8];
			GetDate(p);
			GetTime(q);
			strcat(p," ");
			strcat(p,q);
			strcpy(t,p);
			
			 
		}
};

int main()
{		
	DateTime DT(2020, 05, 01, 12, 12, 12);
	DT.SetDateTime(2020, 05, 01, 12, 12, 12);
	char t[18];
	DT.GetDateTime(t); 
	cout << t;
	return 0;   
}

运行结果:(怎么会有这么麻烦的题目!!!我吐了(╯‵□′)╯︵┻━┻

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值