用面向对象思想实现时钟C++描述的实例代码:
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
|
# include <iostream>
# include <time.h>
# include <iomanip>
# include <windows.h>
//# include <unistd.h>
using
namespace
std;
//初始化的数据来自系统,以后的逻辑运算及显示自实现
class
Clock
{
public
:
Clock()
{
time_t
t =
time
(NULL);
struct
tm
ti = *
localtime
(&t);
hour = ti.tm_hour;
min = ti.tm_min;
sec = ti.tm_sec;
}
void
run()
{
while
(1)
{
system
(
"cls"
);
show();
//完成显示
tick();
//数据更新
}
}
private
:
void
show()
{
//system("cls");
cout << setw(2) << setfill(
'0'
) << hour <<
":"
;
cout << setw(2) << setfill(
'0'
) << min <<
":"
;
cout << setw(2) << setfill(
'0'
) << sec <<
":"
;
}
void
tick()
{
Sleep(1);
if
(++sec == 60)
{
sec = 0;
min += 1;
if
(++min == 60)
{
min = 0;
hour += 1;
if
(++hour == 24)
{
hour = 0;
}
}
}
}
int
hour = 0;
int
min = 0;
int
sec = 0;
};
int
main(
void
)
{
Clock c;
c.run();
cout <<
" Hello World "
<< endl;
return
0;
}
|
本文转自 liam2199 博客,原文链接: http://blog.51cto.com/liam2199/1972448
如需转载请自行联系原作者