题 号: 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);
}