• C++Day4


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

     

  • 相关阅读:
    c++11 thread 新线程的启动(一)
    代码随想录 | Day46
    大数据面试题:MapReduce压缩方式
    js原型对象的使用
    redis集群的三种方式
    mac 安装docker
    ssm毕设项目学生社团管理系统k8wrg(java+VUE+Mybatis+Maven+Mysql+sprnig)
    Spring Boot企业级开发教程课后习题——第5章Spring Boot实现Web的常用功能
    注塑行业MES系统解决方案,打造数字化智能工厂 先达智控
    uniapp AES加密解密
  • 原文地址:https://blog.csdn.net/cncs1314/article/details/133754843