要求时钟类及其成员函数实现,改变其数据变化;
sleep(时间)函数使用;
Sleep函数
用法:Sleep(时间)
在Windows中时间是按毫秒算的!!!在Linux中时间是按秒算的!!!
清屏
system(“CLS”);
转发自老廖&&Gameshttp://lhwblog.top/index.php/archives/84/
.h文件
时钟类
#ifndef _TIMECLOCK_H
#define _TIMECLOCK_H
class TimeClock
{
public:
TimeClock();
TimeClock(int h,int m,int s);
TimeClock& operator++();
TimeClock operator++(int);
void print();
~TimeClock();
private:
int m_hours;
int m_minute;
int m_sec;
};
#endif
源文件
#include"TimeClock.h"
#include<iostream>
using namespace std;
TimeClock::TimeClock(){
}
TimeClock::TimeClock(int h, int m, int s) : m_hours(h), m_minute(m), m_sec(s)
{
}
TimeClock& TimeClock::operator++(){// 前置
m_sec++;
if (m_sec >= 60)
{
m_sec = 0;
m_minute++;
}
if (m_minute >= 60){
m_minute = 0;
m_hours++;
}
if (m_hours >= 24)
{
m_hours = 0;
}
return *this;
}
TimeClock TimeClock::operator++(int){//后置
TimeClock c;
c = *this;
m_sec++;
if (m_sec >= 60)
{
m_sec = 0;
m_minute++;
}
if (m_minute >= 60){
m_minute = 0;
m_hours++;
}
if (m_hours >= 24)
{
m_hours = 0;
}
return c;
}
void TimeClock::print(){
cout << m_hours<<":"<<m_minute<<":"<<m_sec << endl;
}
TimeClock::~TimeClock(){
}
#include<iostream>
#include"TimeClock.h"
using namespace std;
int main(){
TimeClock v(1, 20, 30);
TimeClock v1(2, 0, 0);
while (1)
{
v++;
++v1;
v1.print();
v.print();
_sleep(1000);
system("CLS");
}
system("pause");
return 0;
}