码图 ClockWithDate类(C++)

题 号: 155 ClockWithDate类(C++) 语言要求: 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);
}
Code:cpp
·#include"ClockAndDate.h"

void Date::NewDay()
{
	day = day + 1;
	switch (month)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		if (day>31)
		{
			day = 1; month = month + 1;
		}break;
	case 4:
	case 9:
	case 11:
	case 6:
		if (day>30)
		{
			day = 1; month++;
		}break;
	case 2:
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) && day>29)
		{
			day = 1; month++;
		}
		else if (day>28)
		{
			day = 1;
			month++;
		}

	}
	if (month>12)
	{
		year++;
		month = 1;
	}
}
int Date::days(int year, int month)
{
	int num;
	switch (month)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		num = 31; break;
	case 2:
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
			num = 29;
		else
			num = 28;
		break;
	case 4:
	case 6:
	case 9:
	case 11:
		num = 30;
		break;
	default:
		num = 31;
	}
	return num;
}

class ClockWithDate :public Clock, public Date {
private:
	int ye, mon, da, hou, min, sec;
public:
	ClockWithDate(int h, int mi, int s,int y, int m, int d ) : Clock(h, mi, s),Date(y, m, d){
		this->ye = y;
		this->mon = m;
		this->da = d;
		this->hou = h;
		this->min = mi;
		this->sec = s;
	}
	virtual void run() {
		sec = sec + 1;
		if (sec>59)
		{
			sec = 0;
			min += 1;
		}
		if (min>59)
		{
			min = 0;
			hou += 1;
		}
		if (hou>23)
		{
			hou = 0;
			da += 1;
			switch (mon)
			{
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				if (da>31)
				{
					da = 1; mon = mon + 1;
				}break;
			case 4:
			case 9:
			case 11:
			case 6:
				if (da>30)
				{
					da = 1; mon++;
				}break;
			case 2:
				if ((ye % 4 == 0 && ye % 100 != 0) || (ye % 400 == 0) && da>29)
				{
					da = 1; mon++;
				}
				else if (da>28)
				{
					da = 1;
					mon++;
				}

			}
			if (mon>12)
			{
				ye++;
				mon = 1;
			}
		}

	}
	void showTime() {
		cout << "Now:" << hou << ":" << min << ":" << sec << endl;
		cout << ye << "-" << mon << "-" << da << endl;
	}
};

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值