• C++&QT-day4


    1. #include //运算符重载
    2. using namespace std;
    3. class Person
    4. {
    5. // //全局函数实现+运算符重载
    6. // friend const Person operator+(const Person &L,const Person &R);
    7. // //全局函数实现-运算符重载
    8. // friend const Person operator-(const Person &L,const Person &R);
    9. // //全局函数实现*运算符重载
    10. // friend const Person operator*(const Person &L,const Person &R);
    11. // //全局函数实现/运算符重载
    12. // friend const Person operator/(const Person &L,const Person &R);
    13. // //全局函数实现%运算符重载
    14. // friend const Person operator%(const Person &L,const Person &R);
    15. // //全局函数实现>运算符重载
    16. // friend bool operator>(const Person &L, const Person &R);
    17. // //全局函数实现>=运算符重载
    18. // friend bool operator>=(const Person &L, const Person &R);
    19. // //全局函数实现<运算符重载
    20. // friend bool operator<(const Person &L, const Person &R);
    21. // //全局函数实现<=运算符重载
    22. // friend bool operator<=(const Person &L, const Person &R);
    23. // //全局函数实现==运算符重载
    24. // friend bool operator==(const Person &L, const Person &R);
    25. // //全局函数实现!=运算符重载
    26. // friend bool operator!=(const Person &L, const Person &R);
    27. // //全局函数实现=运算符重载
    28. // friend Person &operator=(Person &L, const Person &R);
    29. // //全局函数实现+=运算符重载
    30. // friend Person &operator+=(Person &L, const Person &R);
    31. // //全局函数实现-=运算符重载
    32. // friend Person &operator-=(Person &L, const Person &R);
    33. // //全局函数实现*=运算符重载
    34. // friend Person &operator*=(Person &L, const Person &R);
    35. // //全局函数实现/=运算符重载
    36. // friend Person &operator/=(Person &L, const Person &R);
    37. // //全局函数实现%=运算符重载
    38. // friend Person &operator%=(Person &L, const Person &R);
    39. private:
    40. int a;
    41. int b;
    42. public:
    43. Person(){}
    44. Person(int a, int b):a(a),b(b){}
    45. /****************成员函数实现+运算符重载*******************/
    46. const Person operator+(const Person &p)const
    47. {
    48. Person temp;
    49. temp.a = a + p.a;
    50. temp.b = b + p.b;
    51. return temp;
    52. }
    53. /****************成员函数实现-运算符重载*******************/
    54. const Person operator-(const Person &p)const
    55. {
    56. Person temp;
    57. temp.a = a - p.a;
    58. temp.b = b - p.b;
    59. return temp;
    60. }
    61. /****************成员函数实现*运算符重载*******************/
    62. const Person operator*(const Person &p)const
    63. {
    64. Person temp;
    65. temp.a = a * p.a;
    66. temp.b = b * p.b;
    67. return temp;
    68. }
    69. /****************成员函数实现/运算符重载*******************/
    70. const Person operator/(const Person &p)const
    71. {
    72. Person temp;
    73. if(0==p.a && 0==p.b)
    74. cout << "除数为0" << endl;
    75. else if(0==p.a && 0!=p.b)
    76. {
    77. cout << "a除数为0" << " b = " << b << endl;
    78. temp.a = a / p.b;
    79. }
    80. else if(0!=p.a && 0==p.b)
    81. {
    82. temp.b = b / p.a;
    83. cout << "a = " << a << " b除数为0" << endl;
    84. }
    85. else
    86. {
    87. temp.b = b / p.a;
    88. temp.a = a / p.b;
    89. cout << "a = " << a << " b = " << b << endl;
    90. }
    91. return temp;
    92. }
    93. /****************成员函数实现%运算符重载*******************/
    94. const Person operator%(const Person &p)const
    95. {
    96. Person temp;
    97. temp.a = a % p.a;
    98. temp.b = b % p.b;
    99. return temp;
    100. }
    101. /***************成员函数实现>关系运算符重载***************/
    102. bool operator>(const Person &R)const
    103. {
    104. if(a>R.a && b>R.b)
    105. {
    106. return true;
    107. }
    108. else
    109. {
    110. return false;
    111. }
    112. }
    113. /***************成员函数实现>=关系运算符重载***************/
    114. bool operator>=(const Person &R)const
    115. {
    116. if(a>=R.a && b>=R.b)
    117. {
    118. return true;
    119. }
    120. else
    121. {
    122. return false;
    123. }
    124. }
    125. /***************成员函数实现<关系运算符重载***************/
    126. bool operator<(const Person &R)const
    127. {
    128. if(a
    129. {
    130. return true;
    131. }
    132. else
    133. {
    134. return false;
    135. }
    136. }
    137. /***************成员函数实现<=关系运算符重载***************/
    138. bool operator<=(const Person &R)const
    139. {
    140. if(a<=R.a && b<=R.b)
    141. {
    142. return true;
    143. }
    144. else
    145. {
    146. return false;
    147. }
    148. }
    149. /***************成员函数实现==关系运算符重载***************/
    150. bool operator==(const Person &R)const
    151. {
    152. if(a==R.a && b==R.b)
    153. {
    154. return true;
    155. }
    156. else
    157. {
    158. return false;
    159. }
    160. }
    161. /***************成员函数实现!=关系运算符重载***************/
    162. bool operator!=(const Person &R)const
    163. {
    164. if(a!=R.a && b!=R.b)
    165. {
    166. return true;
    167. }
    168. else
    169. {
    170. return false;
    171. }
    172. }
    173. /***************成员函数实现=赋值运算符重载***************/
    174. Person &operator=(const Person &R)
    175. {
    176. a = R.a;
    177. b = R.b;
    178. return *this;
    179. }
    180. /***************成员函数实现+=赋值运算符重载***************/
    181. Person &operator+=(const Person &R)
    182. {
    183. a += R.a;
    184. b += R.b;
    185. return *this;
    186. }
    187. /***************成员函数实现-=赋值运算符重载***************/
    188. Person &operator-=(const Person &R)
    189. {
    190. a -= R.a;
    191. b -= R.b;
    192. return *this;
    193. }
    194. /***************成员函数实现*=赋值运算符重载***************/
    195. Person &operator*=(const Person &R)
    196. {
    197. a *= R.a;
    198. b *= R.b;
    199. return *this;
    200. }
    201. /***************成员函数实现/=赋值运算符重载***************/
    202. Person &operator/=(const Person &R)
    203. {
    204. if(0==R.a && 0==R.b)
    205. cout << "除数为0" << endl;
    206. else if(0==R.a && 0!=R.b)
    207. {
    208. cout << "a除数为0" << " b = " << b << endl;
    209. a = a / R.b;
    210. }
    211. else if(0!=R.a && 0==R.b)
    212. {
    213. b = b / R.a;
    214. cout << "a = " << a << " b除数为0" << endl;
    215. }
    216. else if(0!=R.a && 0!=R.b)
    217. {
    218. a = a / R.a;
    219. b = b / R.b;
    220. cout << "a = " << a << " b = " << b << endl;
    221. }
    222. return *this;
    223. }
    224. /***************成员函数实现%=赋值运算符重载***************/
    225. Person &operator%=(const Person &R)
    226. {
    227. a %= R.a;
    228. b %= R.b;
    229. return *this;
    230. }
    231. void show()
    232. {
    233. cout << "a = " << a << " b = " << b << endl;
    234. }
    235. };
    236. ///****************全局函数实现+运算符重载*******************/
    237. //const Person operator+(const Person &L,const Person &R)
    238. //{
    239. // Person temp;
    240. // temp.a = L.a + R.a;
    241. // temp.b = L.b + R.b;
    242. // return temp;
    243. //}
    244. ///****************全局函数实现-运算符重载*******************/
    245. //const Person operator-(const Person &L,const Person &R)
    246. //{
    247. // Person temp;
    248. // temp.a = L.a - R.a;
    249. // temp.b = L.b - R.b;
    250. // return temp;
    251. //}
    252. ///****************全局函数实现*运算符重载*******************/
    253. //const Person operator*(const Person &L,const Person &R)
    254. //{
    255. // Person temp;
    256. // temp.a = L.a * R.a;
    257. // temp.b = L.b * R.b;
    258. // return temp;
    259. //}
    260. ///****************全局函数实现/运算符重载*******************/
    261. //const Person operator/(const Person &L,const Person &R)
    262. //{
    263. // Person temp;
    264. // if(0==R.a && 0==R.b)
    265. // cout << "除数为0" << endl;
    266. // else if(0==R.a && 0!=R.b)
    267. // {
    268. // cout << "a除数为0" << " b = " << L.b << endl;
    269. // temp.a = L.a / R.b;
    270. // }
    271. // else if(0!=R.a && 0==R.b)
    272. // {
    273. // temp.b = L.b / R.a;
    274. // cout << "a = " << L.a << " b除数为0" << endl;
    275. // }
    276. // else if(0!=R.a && 0!=R.b)
    277. // {
    278. // temp.b = L.b / R.a;
    279. // temp.a = L.a / R.b;
    280. // cout << "a = " << L.a << " b = " << L.b << endl;
    281. // }
    282. // return temp;
    283. //}
    284. ///****************全局函数实现%运算符重载*******************/
    285. //const Person operator%(const Person &L,const Person &R)
    286. //{
    287. // Person temp;
    288. // temp.a = L.a % R.a;
    289. // temp.b = L.b % R.b;
    290. // return temp;
    291. //}
    292. ///***************全局函数实现>关系运算符重载***************/
    293. //bool operator>(const Person &L, const Person &R)
    294. //{
    295. // if(L.a>R.a && L.b>R.b)
    296. // {
    297. // return true;
    298. // }
    299. // else
    300. // {
    301. // return false;
    302. // }
    303. //}
    304. ///***************全局函数实现>=关系运算符重载***************/
    305. //bool operator>=(const Person &L, const Person &R)
    306. //{
    307. // if(L.a>=R.a && L.b>=R.b)
    308. // {
    309. // return true;
    310. // }
    311. // else
    312. // {
    313. // return false;
    314. // }
    315. //}
    316. ///***************全局函数实现<关系运算符重载***************/
    317. //bool operator<(const Person &L, const Person &R)
    318. //{
    319. // if(L.a
    320. // {
    321. // return true;
    322. // }
    323. // else
    324. // {
    325. // return false;
    326. // }
    327. //}
    328. ///***************全局函数实现>关系运算符重载***************/
    329. //bool operator<=(const Person &L, const Person &R)
    330. //{
    331. // if(L.a<=R.a && L.b<=R.b)
    332. // {
    333. // return true;
    334. // }
    335. // else
    336. // {
    337. // return false;
    338. // }
    339. //}
    340. ///***************全局函数实现==关系运算符重载***************/
    341. //bool operator==(const Person &L, const Person &R)
    342. //{
    343. // if(L.a==R.a && L.b==R.b)
    344. // {
    345. // return true;
    346. // }
    347. // else
    348. // {
    349. // return false;
    350. // }
    351. //}
    352. ///***************全局函数实现!=关系运算符重载***************/
    353. //bool operator!=(const Person &L, const Person &R)
    354. //{
    355. // if(L.a!=R.a && L.b!=R.b)
    356. // {
    357. // return true;
    358. // }
    359. // else
    360. // {
    361. // return false;
    362. // }
    363. //}
    364. ///***************全局函数实现=赋值运算符重载***************/
    365. //Person &operator=(Person &L, const Person &R)
    366. //{
    367. // L.a = R.a;
    368. // L.b = R.b;
    369. // return L;
    370. //}
    371. ///***************全局函数实现+=赋值运算符重载***************/
    372. //Person &operator+=(Person &L, const Person &R)
    373. //{
    374. // L.a += R.a;
    375. // L.b += R.b;
    376. // return L;
    377. //}
    378. ///***************全局函数实现-=赋值运算符重载***************/
    379. //Person &operator-=(Person &L, const Person &R)
    380. //{
    381. // L.a -= R.a;
    382. // L.b -= R.b;
    383. // return L;
    384. //}
    385. ///***************全局函数实现*=赋值运算符重载***************/
    386. //Person &operator*=(Person &L, const Person &R)
    387. //{
    388. // L.a *= R.a;
    389. // L.b *= R.b;
    390. // return L;
    391. //}
    392. ///***************全局函数实现/=赋值运算符重载***************/
    393. //Person &operator/=(Person &L, const Person &R)
    394. //{
    395. // if(0==R.a && 0==R.b)
    396. // cout << "除数为0" << endl;
    397. // else if(0==R.a && 0!=R.b)
    398. // {
    399. // cout << "a除数为0" << " b = " << L.b << endl;
    400. // L.b /= R.b;
    401. // }
    402. // else if(0!=R.a && 0==R.b)
    403. // {
    404. // L.a /= R.a;
    405. // cout << "a = " << L.a << " b除数为0" << endl;
    406. // }
    407. // else if(0!=R.a && 0!=R.b)
    408. // {
    409. // L.a /= R.a;
    410. // L.b /= R.b;
    411. // cout << "a = " << L.a << " b = " << L.b << endl;
    412. // }
    413. // return L;
    414. //}
    415. ///***************全局函数实现%=赋值运算符重载***************/
    416. //Person &operator%=(Person &L, const Person &R)
    417. //{
    418. // L.a %= R.a;
    419. // L.b %= R.b;
    420. // return L;
    421. //}
    422. /****************************************************/
    423. int main()
    424. {
    425. Person p1(3,4);
    426. Person p2(1,2);
    427. Person p3 = p1 + p2; p3.show();
    428. Person p4 = p1 - p2; p4.show();
    429. Person p5 = p1 * p2; p5.show();
    430. Person p6 = p1 / p2;
    431. Person p7 = p1 % p2; p7.show();
    432. if(p3>p2)
    433. cout << "p3>p2" << endl;
    434. else if(p3>=p2)
    435. cout << "p3>=p2" << endl;
    436. else if(p3
    437. cout << "p3 << endl;
    438. else if(p3<=p2)
    439. cout << "p3<=p2" << endl;
    440. else if(p3==p2)
    441. cout << "p3==p2" << endl;
    442. else if(p3!=p2)
    443. cout << "p3!=p2" << endl;
    444. p3+=p2;p3.show();
    445. p3-=p2;p3.show();
    446. p3*=p2;p3.show();
    447. p3/=p2;
    448. p3%=p2;p3.show();
    449. return 0;
    450. }

  • 相关阅读:
    博客从 CloudBase 迁移至云主机
    287. 寻找重复数
    PAT 1040 Longest Symmetric String
    [CVPR2022] A Dual Weighting Label Assignment Scheme for Object Detection
    leetcode:6240. 树上最大得分和路径【两次dfs模拟 + 读题题 + 不要用list做py函数的参数!!】
    基于SNAT+DNAT发布内网K8S及Jenkins+gitlab+Harbor模拟CI/CD的综合项目
    Spring-RabbitMQ 工作队列实践
    2023网安面试题170道,轻松应对面试
    (18)不重启服务动态停止、启动RabbitMQ消费者
    注释之重——程序员与代码可维护性
  • 原文地址:https://blog.csdn.net/qq_53195772/article/details/133755676