闹钟程序!

#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();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值