forked from Light-City/CPlusPlusThings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
时钟++运算符重载.cpp
93 lines (83 loc) · 1.81 KB
/
时钟++运算符重载.cpp
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
#include<iostream>
#include<cmath>
using namespace std;
/*
* 时钟类
*/
class Clock
{
private:
int Hour, Minute, Second;
public:
Clock(int h=0, int m=0, int s=0);
void ShowTime();
Clock& operator ++();
Clock operator ++(int);
};
/*
* 时钟类构造函数
*/
Clock::Clock(int h,int m, int s)
{
if(h>=0 && h<24 && m>=0 && m<60 && s>=0 && s<60)
{
Hour = h;
Minute =m;
Second= s;
}
else
cout<<"输入的时间格式错误!"<<endl;
}
/*
* 显示时间
*/
void Clock::ShowTime()
{
cout<<Hour<<":"<<Minute<<":"<<Second<<endl;
}
/*
* 时间递增一秒(重载前缀++运算符)
*/
Clock& Clock::operator ++()
{
Second++;
if (Second >= 60)
{
Second = Second - 60;
Minute++;
if (Minute >= 60)
{
Minute = Minute - 60;
Hour++;
Hour = Hour % 24;
}
}
return *this;
}
/*
* 时间递增一秒(重载后缀++运算符)
*/
Clock Clock::operator ++(int)
{
Clock old = *this;
++(*this);
return old;
}
/*
* 主函数
*/
int main()
{
Clock myClock(23,59,59);
cout<<"初始化显示时间为:\t\t";
myClock.ShowTime();
cout<<"执行myClock++后的时间为:\t";
//先执行ShowTime(),输出myClock=23:59:59,
//再执行myClock++,此时myClock=00:00:00
(myClock++).ShowTime();
cout<<"执行++myClock后的时间为:\t";
//先执行++myClock,此时myClock=00:00:01
//再执行ShowTime(),输出myClock=00:00:01
(++myClock).ShowTime();
system("pause");
}