-
Notifications
You must be signed in to change notification settings - Fork 0
/
前缀++和后缀++重载.txt
61 lines (61 loc) · 998 Bytes
/
前缀++和后缀++重载.txt
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
//时钟++运算符重载
//++time 和 time++
#include<iostream>
#include<string>
class Clock
{
private:
int hour;
int minute;
int second;
public:
Clock(int a=0,int b=0,int c=0)
{
if (a >= 0 && b >= 0 && c >= 0 && a < 24 && b < 60 && c < 60)
{
hour = a;
minute = b;
second = c;
}
else
{
std::cout << "输入错误,重置为0" << std::endl;
hour = 0;
minute = 0;
second = 0;
}
}
void display()
{
std::cout << hour << ":" << minute << ":" << second << std::endl;
}
Clock& operator++()//++Clock
{
second++;
if (second >= 60)
{
second -= 60;
if (++minute >= 60)
{
minute -= 60;
hour=(++hour)%24;
}
}
return *this;
}
Clock operator ++(int)//Clock++,后缀++的标识是参数为int
{
Clock old = *this;
++(*this);
return old;
}
};
int main()
{
Clock a{ 12,59,59 };
(a++).display();
a.display();
(++a).display();
a.display();
}
//参考CPlusPlusThings