码图:155 ClockWithDate类(C++)

题目描述:

引入头文件ClockAndDate.h,它的内容如下:

#include <iostream>
using namespace std;
class Clock{
public:
	Clock(int h,int m,int s){
	 hour =(h>23? 0:h);
	 minute = (m>59?0:m);
	 second = (s>59?0:m);
	}
	virtual void run(){
		second = second+1;
		if (second>59)
		{
			second =0;
			minute+=1;
		}
		if (minute>59)
		{
			minute =0;
			hour+=1;
		}
		if (hour>23)
		{
			hour =0;
		}
	}
	virtual void showTime(){
		cout<<"Now:"<<hour<<":"<<minute<<":"<<second<<endl;
	}
	int getHour(){return hour;}
	int getMinute(){return minute;}
	int getSecond(){return second;}
	
Clock * createClockWithDate(int h,int m,int s,int year,int month,int day);
protected:
	int hour;
	int minute;
	int second;
};

class Date{
public:
	Date(int y=1996,int m=1,int d=1){
		day =d;
		year =y;
		month =m;
		if (m>12||m<1)
		{
			m=1;
		}
		if(d>days(y,m)){
			day = 1;
		}
	};
	int days(int year,int month);
	void NewDay();
	void display(){
		cout<<year<<"-"<<month<<"-"<<day<<endl;
	}
protected:
	int year;
	int month;
	int day;
};

需要实现Date类的days方法,根据年和月,返回该年该月对应的天数
实现Date类的NewDay方法,该方法将Date代表的日期增加一天。

实现ClockWithDate类,它继承至Clock类和Date类,记录时间和日期
重新实现ClockWithDate类的showTime方法和run方法。
showTime方法输出当的时间和日期,先输出时间再输出日期。
run方法每次将现在的时间增加一秒,并且当时间超过23:59:59时,更新日期。

必须实现Clock类的createClockWithDate方法,它的实现必须在ClockWithDate类的定义之后,它的内容如下:

Clock* Clock::createClockWithDate(int h,int m,int s,int year,int month,int day){
	return new ClockWithDate(h,m,s,year,month,day);
}

实现:

#include"ClockAndDate.h"

int Date::days(int year, int month)
{
	int monthsInLeapYear[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 };
	int monthsInNormalYear[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
		return monthsInLeapYear[month - 1];
	else
		return monthsInNormalYear[month - 1];
}
void Date::NewDay()
{
	int m_d[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
	day++;
	switch (month)
	{
	case 2:
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
		{
			if (day > 29)
			{
				day = 1; month = 3;
			}
		}
		else
		{
			if (day > 28)
			{
				day = 1; month = 3;
			}
		}
		break;
	default:
		if (day > m_d[month - 1])
		{
			day = 1; month++;
		}
		break;
	}
	if (month > 12)
	{
		month = 1; year++;
	}

}

class ClockWithDate :public Clock, public Date
{
public:
	ClockWithDate(int h, int m, int s, int year, int month, int day):Clock(h,m,s),Date(year,month,day){}
	virtual void showTime()
	{
		cout << "Now:" << hour << ":" << minute << ":" << second << endl << year << "-" << month << "-" << day << endl;
	}
	virtual void run()
	{
		second = second + 1;
		if (second > 59)
		{
			second = 0;
			minute += 1;
		}
		if (minute > 59)
		{
			minute = 0;
			hour += 1;
		}
		if (hour > 23)
		{
			hour = 0;
			NewDay();
		}
	
	}
};
Clock* Clock::createClockWithDate(int h, int m, int s, int year, int month, int day) {
	return new ClockWithDate(h, m, s, year, month, day);
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值