• c++day4


    1. class Stu
    2. {
    3. friend const Stu operator+(const Stu &L,const Stu &R);
    4. friend const Stu operator-(const Stu &L,const Stu &R);
    5. friend const Stu operator*(const Stu &L,const Stu &R);
    6. friend const Stu operator/(const Stu &L,const Stu &R);
    7. friend const Stu operator%(const Stu &L,const Stu &R);
    8. friend bool operator>(const Stu &L,const Stu &R);
    9. friend bool operator>=(const Stu &L,const Stu &R);
    10. friend bool operator<(const Stu &L,const Stu &R);
    11. friend bool operator<=(const Stu &L,const Stu &R);
    12. friend bool operator==(const Stu &L,const Stu &R);
    13. friend bool operator!=(const Stu &L,const Stu &R);
    14. friend Stu &operator+=(Stu &L,const Stu &R);
    15. friend Stu &operator-=(Stu &L,const Stu &R);
    16. friend Stu &operator*=(Stu &L,const Stu &R);
    17. friend Stu &operator/=(Stu &L,const Stu &R);
    18. friend Stu &operator%=(Stu &L,const Stu &R);
    19. private:
    20. string name;
    21. int a;
    22. int b;
    23. public:
    24. Stu()
    25. {
    26. }
    27. void happen() const
    28. {
    29. cout << "有const";
    30. cout << "name = " << name << endl;
    31. }
    32. void happen()
    33. {
    34. cout << "无const";
    35. cout << "name = " << name << endl;
    36. }
    37. //成员内运算符重载
    38. // Stu operator-(const Stu &p)
    39. // {
    40. // Stu temp;
    41. // temp.a= a - p.a;
    42. // temp.b= b - p.b;
    43. // return temp;
    44. // }
    45. };
    46. //全局运算符重载
    47. //算数运算符
    48. const Stu operator+(const Stu &L,const Stu &R)
    49. {
    50. Stu temp;
    51. temp.a= L.a + R.a;
    52. temp.b= L.b + R.b;
    53. return temp;
    54. }
    55. const Stu operator-(const Stu &L,const Stu &R)
    56. {
    57. Stu temp;
    58. temp.a= L.a - R.a;
    59. temp.b= L.b - R.b;
    60. return temp;
    61. }
    62. const Stu operator*(const Stu &L,const Stu &R)
    63. {
    64. Stu temp;
    65. temp.a= L.a * R.a;
    66. temp.b= L.b * R.b;
    67. return temp;
    68. }
    69. const Stu operator/(const Stu &L,const Stu &R)
    70. {
    71. Stu temp;
    72. temp.a= L.a / R.a;
    73. temp.b= L.b / R.b;
    74. return temp;
    75. }
    76. const Stu operator%(const Stu &L,const Stu &R)
    77. {
    78. Stu temp;
    79. temp.a= L.a % R.a;
    80. temp.b= L.b % R.b;
    81. return temp;
    82. }
    83. //关系运算符
    84. bool operator>(const Stu &L,const Stu &R)
    85. {
    86. if(L.a > R.a && L.b > R.b)
    87. {
    88. return true;
    89. }
    90. else
    91. {
    92. return false;
    93. }
    94. }
    95. bool operator>=(const Stu &L,const Stu &R)
    96. {
    97. if(L.a >= R.a && L.b >= R.b)
    98. {
    99. return true;
    100. }
    101. else
    102. {
    103. return false;
    104. }
    105. }
    106. bool operator<(const Stu &L,const Stu &R)
    107. {
    108. if(L.a < R.a && L.b < R.b)
    109. {
    110. return true;
    111. }
    112. else
    113. {
    114. return false;
    115. }
    116. }
    117. bool operator<=(const Stu &L,const Stu &R)
    118. {
    119. if(L.a <= R.a && L.b <= R.b)
    120. {
    121. return true;
    122. }
    123. else
    124. {
    125. return false;
    126. }
    127. }
    128. bool operator==(const Stu &L,const Stu &R)
    129. {
    130. if(L.a == R.a && L.b == R.b)
    131. {
    132. return true;
    133. }
    134. else
    135. {
    136. return false;
    137. }
    138. }
    139. bool operator!=(const Stu &L,const Stu &R)
    140. {
    141. if(L.a != R.a && L.b != R.b)
    142. {
    143. return true;
    144. }
    145. else
    146. {
    147. return false;
    148. }
    149. }
    150. //赋值运算符
    151. Stu &operator+=(Stu &L,const Stu &R)
    152. {
    153. L.a += R.a;
    154. L.b += R.b;
    155. return L;
    156. }
    157. Stu &operator-=(Stu &L,const Stu &R)
    158. {
    159. L.a -= R.a;
    160. L.b -= R.b;
    161. return L;
    162. }
    163. Stu &operator*=(Stu &L,const Stu &R)
    164. {
    165. L.a *= R.a;
    166. L.b *= R.b;
    167. return L;
    168. }
    169. Stu &operator/=(Stu &L,const Stu &R)
    170. {
    171. L.a /= R.a;
    172. L.b /= R.b;
    173. return L;
    174. }
    175. Stu &operator%=(Stu &L,const Stu &R)
    176. {
    177. L.a %= R.a;
    178. L.b %= R.b;
    179. return L;
    180. }

  • 相关阅读:
    RDBMS 的历史回顾
    ESP32是真火
    3389,为了保障3389端口的安全,我们可以采取的措施
    MySQL命令行插入数据乱码分析
    Java Class07
    JavaWeb三大组件之Listener------Listener详细讲解
    三模块七电平级联H桥整流器电压平衡控制策略Simulink仿真
    基于JavaGUI的图书管理系统
    基于Dockerfile创建镜像
    图扑数字孪生智慧加油站,构建安全防护网
  • 原文地址:https://blog.csdn.net/m0_73912044/article/details/133755279