-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path155.cpp
More file actions
102 lines (100 loc) · 1.83 KB
/
155.cpp
File metadata and controls
102 lines (100 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#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;
};
#include "ClockAndDate.h"
void Date::NewDay(){
day++;
if(day>days(year,month)){
month++;
day=1;
if(month>12){
month=1;
year++;
}
}
}
int Date::days(int year,int month){
int d[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(year%100){
if(!year%4)d[1]++;
}else{
if(year%400==0)d[1]++;
}
return d[month-1];
}
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){};
void run()override{
Clock::run();
if(hour+minute+second==0){
Date::NewDay();
}
}
void showTime()override{
Clock::showTime();
Date::display();
}
};
Clock* Clock::createClockWithDate(int h,int m,int s,int year,int month,int day){
return new ClockWithDate(h,m,s,year,month,day);
}