我是靠谱客的博主 霸气八宝粥,这篇文章主要介绍类的学习--clock类,现在分享给大家,希望可以做个参考。

建立一个Clock类,并实现其中的成员函数。

class Clock {

public:

 Clock(){};//构造函数

 Clock(int h,int m,int s){};//有参数的构造函数

 void setclock(){}; //设置新的时间

 int gethour(){};//获取小时

 int getmin(){};//获取分钟

 int getsecd(){};//获取秒

 int secondbetween(){}; //返回与另一个时间对象的时间间隔,单位为秒

Clock  clockadd(int h,int m,int s){};

Clock  clocksub(int h,int m,int s){};

void showtime(){};

//重载运算符

Clock operator+=(int seconds);

Clock operator-=(int seconds);

Clock operator++();

Clock operator++(int);

Clock operator—();

Clock operator—(int);

Clock operator+(int seconds);

Clock operator-(int seconds);

 //重载运算符双目

friend long operator-(const Clock &T1,const Clock &T2);

 //时间大小比较

friend bool operator>(const Clock &T1,const Clock &T2);

friend bool operator<(const Clock &T1, const Clock &T2);

friend bool operator==(const Clock &T1,const Clock &T2);

friend bool operator>=(const Clock &T1,const Clock& T2);

friend bool operator<=(const Clock &T1, const Clock &T2);

friend bool operator!=(const Clock &T1, const Clock &T2);

private:

  int hour;

  int min;

  int sec;

}

并编写一个主程序,对Clock类的各个方法进行调用测试。

复制代码
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <iostream> #include <cmath> #include <stdlib.h> using namespace std; class Clock { public: //默认构造函数 Clock() { hour = 0, minute = 0, second = 0; }; //带参数的构造函数 Clock(int h, int m, int s); //复制构造函数 Clock(const Clock& C); //析构函数 ~Clock() {}; //设置时间 void setclock(int newh, int newm, int news) { hour = newh, minute = newm, second = news; if (newh >= 12 || newm >= 60 || news >= 60) { cout << "您输入的数据不合法!" << endl; cout << "时钟重置为00:00:00" << endl; hour = 0, minute = 0, second = 0; } }; int gethour() { return hour; }; int getminute() { return minute; }; int getsecond() { return second; }; //时钟转换为秒 int TotalSecond(); void showtime() { cout << hour << ":" << minute << ":" << second << endl; }; //返回与另一个时间对象的时间间隔,单位为秒 int secondbetween(Clock c); //返回值为Clock类型 add 两个时钟的和 sub 两个时钟的差 Clock clockadd(int h, int m, int s); Clock clocksub(int h, int m, int s); //重载运算符 Clock operator += (int seconds); Clock operator -= (int seconds); Clock operator ++(); //前置++ Clock operator ++(int); //后置++ Clock operator --(); Clock operator --(int); Clock operator +(int seconds); Clock operator -(int seconds); //friend友元函数 //重载运算符双目 friend Clock operator -(const Clock& T1, const Clock& T2); //重载右移运算符 friend ostream& operator<<(ostream& cout, Clock C); //时间大小比较 friend bool operator >(const Clock& T1, const Clock& T2); friend bool operator <(const Clock& T1, const Clock& T2); friend bool operator ==(const Clock& T1, const Clock& T2); friend bool operator >=(const Clock& T1, const Clock& T2); friend bool operator <=(const Clock& T1, const Clock& T2); friend bool operator !=(const Clock& T1, const Clock& T2); private: int hour, minute, second; }; Clock::Clock(int h, int m, int s) { hour = h, minute = m, second = s; if (h >= 12 || m >= 60 || s >= 60) { cout << "您输入的数据不合法!" << endl; cout << "时钟重置为00:00:00" << endl; hour = 0, minute = 0, second = 0; } } //构造函数不可指定返回值类型 且无返回值 Clock::Clock(const Clock& C) { hour = C.hour; minute = C.minute; second = C.second; } int Clock::TotalSecond() { int t; t = hour * 60 * 60 + minute * 60 + second; return t; } int Clock::secondbetween(Clock c) { int t1, t2; t1 = this->hour * 60 * 60 + this->minute * 60 + this->second; t2 = c.hour * 60 * 60 + c.minute * 60 + c.second; //cout << "时间间隔:"<< abs(t1 - t2) << endl; return abs(t1 - t2); } ostream& operator<<(ostream& cout, Clock C) { cout << C.hour << ":" << C.minute << ":" << C.second; return cout; } Clock Clock::clockadd(int h, int m, int s) { int tm = minute, ts = second; if ((s + second) >= 60) { second = (s + second) % 60; minute += ((s + ts) / 60); tm = minute; } else second += s ; if ((m + minute) >= 60) { minute = (m + minute) % 60; hour += (m + tm) / 60; } else minute += m; if ((hour + h) >= 12) { hour = (hour + h) % 12; } else hour += h; return * this; } Clock Clock::clocksub(int h, int m, int s) { int th = h, tm = m, ts = s % 60; //运算符模式 把秒转换为时钟 if ((h == 0) & (m == 0) & (s > 60)) { tm = s / 60; //大于60分钟 if ((s / 60) >= 60) { tm = (s / 60) % 60; th = s / 3600; //大于12小时 if (((s / 60) / 60) >= 12) { th = (s / 3600) % 12; } } //cout << "转换后:"; //cout << th << ":" << tm << ":" << ts << endl; } if ((second - ts) < 0) { second = second - ts + 60; minute -= 1; } else second -= ts; if ((minute - tm) < 0) { minute = minute - tm + 60; hour -= 1; } else minute -= tm; if ((hour - th) < 0) { hour = hour + 12 - th; } else hour -= th; return * this; } Clock Clock:: operator += (int seconds) { return clockadd(0, 0, seconds); } Clock Clock::operator -= (int seconds) { return clocksub(0, 0, seconds); } Clock Clock:: operator + (int seconds) { Clock C = *this; return C.clockadd(0, 0, seconds); } Clock Clock::operator - (int seconds) { Clock C = *this; return C.clocksub(0, 0, seconds); } Clock operator -(const Clock& T1, const Clock& T2) { Clock C1 = T1, C2 = T2; C1.clocksub(T2.hour, T2.minute, T2.second); return C1; } Clock Clock::operator ++() { return clockadd(0, 0, 1); } Clock Clock::operator ++(int) { Clock C = *this; //保存当前值 clockadd(0, 0, 1); //当前值+1 return C; //返回没有+1的值 } Clock Clock::operator --() { return clocksub(0, 0, 1); } Clock Clock::operator --(int) { Clock C = *this; clocksub(0, 0, 1); return C; } bool operator >(const Clock& T1, const Clock& T2) { Clock C1 = T1, C2 = T2; return C1.TotalSecond() > C2.TotalSecond(); } bool operator <(const Clock& T1, const Clock& T2) { Clock C1 = T1, C2 = T2; return C1.TotalSecond() < C2.TotalSecond(); } bool operator ==(const Clock& T1, const Clock& T2) { Clock C1 = T1, C2 = T2; return C1.TotalSecond() == C2.TotalSecond(); } bool operator >=(const Clock& T1, const Clock& T2) { Clock C1 = T1, C2 = T2; return C1.TotalSecond() >= C2.TotalSecond(); } bool operator <=(const Clock& T1, const Clock& T2) { Clock C1 = T1, C2 = T2; return C1.TotalSecond() <= C2.TotalSecond(); } bool operator !=(const Clock& T1, const Clock& T2) { Clock C1 = T1, C2 = T2; return C1.TotalSecond() != C2.TotalSecond(); } int main() { //构造函数 Clock myclock; Clock youclock(0, 0, 0); Clock heclock(8, 30, 30); Clock herclock = heclock; myclock.setclock(7, 15, 0); herclock.setclock(4, 30, 30); //显示函数 cout << "-----showtime函数显示时间------" << endl << endl; cout << "myclock的时间为:"; myclock.showtime(); cout << "youclock的时间为:"; youclock.showtime(); cout << "heclock的时间为:"; heclock.showtime(); cout << "herclock的时间为:"; herclock.showtime(); cout << endl << endl; //获取小时 cout << "调用myclock.gethour()函数获取hour "<<endl; cout << "myclock.hour = "; cout << myclock.gethour() << endl; //时钟转换秒 cout << "调用herclock.TotalSecond()函数把herclock转换成秒 " << endl; cout << "herclock的时间转换为秒为:"; cout << herclock.TotalSecond() << endl; //时间间隔 cout << "调用myclock.secondbetween(youclock)函数获取myclock与youclock的时间间隔 " << endl; cout << "myclock和youclock的时间间隔为:" << myclock.secondbetween(youclock) << "秒" << endl; cout << endl << endl; //时钟加时钟 cout << "-----clockadd函数实现时钟的加法------" << endl; cout << "heclock + 3:29:29 = "; heclock.clockadd(3, 29, 29); heclock.showtime(); //时钟减时钟 cout << "-----clocksub函数实现时钟的减法------" << endl; cout << "herclock - 4:30:32 = "; herclock.clocksub(4, 30, 32); herclock.showtime(); cout << endl << endl; //运算符重载 单位为秒 cout << "-----运算符重载------" << endl << endl; cout << "myclock的时间为:" << myclock << endl; cout << "youclock的时间为:" << youclock << endl; cout << "heclock的时间为:" << heclock << endl; cout << "herclock的时间为:" << herclock << endl << endl; //时钟 += 运算符 cout << "********** += 运算符***********" <<endl << "myclock += 17101 的值为: "; myclock += 17101; myclock.showtime(); //时钟 -= 运算符 cout << "********** -= 运算符***********" << endl << "youclock -= 1 的值为:"; youclock -= 1; youclock.showtime(); cout << endl; //时钟自增1s cout << "**********自增自减运算符***********" << endl; cout << "++heclock = " << ++heclock << endl; cout << "heclock++ = " << heclock++ << endl; cout << "显示heclock : " << heclock << endl << endl; //时钟自减1s cout << "--youclock =" << --youclock << endl; cout << "youclock-- = " << youclock-- << endl; cout << "显示youclock :" << youclock << endl << endl; //时钟 + 运算符 myclock = youclock + 183; cout << "********** + 运算符***********" << endl; cout << "myclock = youclock + 183 = "<< youclock << " + 183 = " << myclock << endl; //时钟 - 运算符 youclock = heclock - 350; cout << "********** - 运算符***********" << endl; cout << "youclock = heclock - 350 = " << heclock << " - 350 = " << youclock << endl; //双目 - 运算符 myclock.setclock(11, 20, 30); cout << "重置myclock的时间为:" << myclock << endl; youclock.setclock(1, 12, 30); cout << "重置youclock的时间为:" << youclock << endl; cout << "********** - 双目运算符***********" << endl; cout << "myclock - youclock = " << myclock - youclock << endl; cout << endl << endl; //时间比较 cout << "-----时间比较------" << endl << endl; heclock.setclock(8, 0, 0); herclock.setclock(8,30,0); cout << "heclock的时间为:" << heclock << endl; cout << "herclock的时间为:" << herclock << endl; cout << "比较heclock和herclock的大小:" << endl << endl; if (heclock > herclock) cout << "heclock大于herclock" << endl; if (heclock < herclock) cout << "heclock小于herclock" << endl; if (heclock >= herclock) cout << "heclock大于等于herclock" << endl; if (heclock <= herclock) cout << "heclock小于等于herclock" << endl; if (heclock != herclock) cout << "heclock不等于herclock" << endl; if (heclock == herclock) cout << "heclock等于herclock" << endl; return 0; }

最后

以上就是霸气八宝粥最近收集整理的关于类的学习--clock类的全部内容,更多相关内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(91)

评论列表共有 0 条评论

立即
投稿
返回
顶部