#ifndef CLOCK_H
#define CLOCK_H
class Clock
{
public:
Clock(int hour = 0, int minute = 0, int second = 0);
Clock(const Clock& c);
void RunClock();
void SetTime(int hour, int minute, int second);
void ShowTime();
~Clock();
protected:
int hour;
int minute;
int second;
};
#endif // !CLOCK_H
#include <iostream>
#include "Clock.h"
using namespace std;
Clock::Clock(int hour, int minute, int second)
{
this->hour = hour;
this->minute = minute;
this->second = second;
}
Clock::Clock(const Clock& c)
{
this->hour = c.hour;
this->minute = c.minute;
this->second = c.second;
}
Clock::~Clock()
{
}
void Clock::RunClock()
{
this->second = this->second + 1;
if (this->second % 60 == 0)
{
this->minute = this->minute + 1;
if (this->minute %60 == 0)
{
this->hour = this->hour + 1;
if (this->hour % 24 == 0)this->hour = 0;
this->minute = 0;
}
this->second = 0;
}
}
void Clock::SetTime(int hour, int minute, int second)
{
this->hour = hour;
this->minute = minute;
this->second = second;
}
void Clock::ShowTime()
{
cout << "当前时间:" << this->hour << ":" << this->minute << ":" << this->second << endl;
}
#ifndef ALARM_H
#define ALARM_H
#include "Clock.h"
class Alarm:public Clock
{
public:
Alarm(int hour = 0, int minute = 0, int second = 0, int timerHour = 0, int timerMinute = 0, int timerSecond = 0);
~Alarm();
void CheckAlarm();
void RingAlarm();
void ShowTimer();
void SetTimer(int hour, int minute, int second);
private:
int timerHour;
int timerMinute;
int timerSecond;
};
#endif // !ALARM_H
#include <iostream>
#include <windows.h>
#include "Alarm.h"
using namespace std;
Alarm::Alarm(int hour, int minute, int second, int timerHour, int timerMinute, int timerSecond):Clock(hour,minute,second)
{
this->timerHour = timerHour;
this->timerMinute = timerMinute;
this->timerSecond = timerSecond;
cout << "生成定时器:" << this->timerHour << ":" << this->timerMinute << ":" << this->timerSecond << endl;
}
Alarm::~Alarm()
{
}
void Alarm::CheckAlarm()
{
if ((timerHour == hour) && (timerMinute == minute) && (timerSecond == second))
{
RingAlarm();
exit(0);
}
}
void Alarm::RingAlarm()
{
cout << "Ring...." << endl;
for(int i =0;i<4;i++){
cout << '\a';
Sleep(1000);
}
}
void Alarm::ShowTimer()
{
cout << "定时时间:" << this->timerHour << ":" << this->timerMinute << ":" << this->timerSecond << endl;
}
void Alarm::SetTimer(int hour, int minute, int second) {
this->timerHour = hour;
this->timerMinute = minute;
this->timerSecond = second;
}
#include<iostream>
#include "Alarm.h"
#include "Circle.h"
using namespace std;
int main() {
Alarm a(0,0,0,10,0,0);
a.SetTime(9, 0, 0);
a.ShowTime();
a.SetTimer(10, 0, 0);
a.ShowTimer();
while (1)
{
a.RunClock();
a.ShowTime();
a.CheckAlarm();
}
}