• 国庆作业4


    C++运算符重载的实现过程和代码

    所有运算符重载,都拥有一个统一的名称:operator# (#表示运算符号)

    参数:根据运算符本身特点决定,如果是单目运算符,最多拥有一个参数,如果是双目运算符,最多拥有两个参数,返回值由用户自己决定

    调用原则:运算符的左侧是函数调用者,右侧是该函数的参数

    每个运算符重载,都可以实现两个版本的重载函数,分别是成员函数版和全局函数版

    成员函数版比全局函数版本少一个参数,因为类对象本身就是一个参数

    全局函数版和成员函数版只能实现一个,否则会造成调用时的混乱情况

    全局函数版,需要使用友元函数来完成

    1. #include
    2. using namespace std;
    3. //定义一个复数类 5 + 3i
    4. class Complex
    5. {
    6. private:
    7. int real; //实部
    8. int vir; //虚部
    9. public:
    10. Complex() {}
    11. Complex(int r, int v):real(r), vir(v) {} //有参构造
    12. //定义展示函数
    13. void show()
    14. {
    15. if(vir>=0)
    16. {
    17. cout<" + "<"i"<
    18. }else
    19. {
    20. cout<"i"<
    21. }
    22. }
    23. //全局函数版实现加运算符重载
    24. friend const Complex operator+ (const Complex &L, const Complex &R);
    25. //成员函数版实现运算符重载
    26. const Complex operator- ( const Complex &R)const
    27. {
    28. Complex c;
    29. c.real = this->real - R.real;
    30. c.vir = this->vir - R.vir;
    31. return c;
    32. }
    33. //成员函数版实现关系运算符的重载:实部>实部 && 虚部>虚部
    34. bool operator>(const Complex &R)const
    35. {
    36. return this->real>R.real&&this->vir>R.vir;
    37. }
    38. //重载中括号运算符
    39. int & operator[](int index)
    40. {
    41. if(index == 0)
    42. {
    43. return real; //返回实部
    44. }else if(index == 1)
    45. {
    46. return vir; //返回虚部
    47. }
    48. }
    49. //重载+=运算符:实部+=实部 虚部+=虚部
    50. Complex & operator+=(const Complex &R)
    51. {
    52. this->real += R.real;
    53. this->vir += R.vir;
    54. return *this; //返回自身的引用
    55. }
    56. //重载负号运算符: 实部= -实部, 虚部 = -虚部
    57. Complex operator-()
    58. {
    59. Complex c;
    60. c.real = -this->real;
    61. c.vir = -this->vir;
    62. return c;
    63. }
    64. //重载前置自增运算符重载函数:实部 = 实部+1 虚部=虚部+1
    65. Complex &operator++()
    66. {
    67. ++this->real;
    68. ++this->vir;
    69. return *this;
    70. }
    71. //重载后置自增运算符重载函数:实部 = 实部+1 虚部=虚部+1
    72. Complex operator++(int)
    73. {
    74. Complex c;
    75. c.real = this->real++;
    76. c.vir = this->vir++;
    77. return c;
    78. }
    79. //将全局函数版实现的输出运算符重载函数设置成友元
    80. friend ostream &operator<<(ostream &L, Complex &c);
    81. //重载小括号运算符,做一个仿函数
    82. void operator()(string s)
    83. {
    84. cout<
    85. }
    86. //重载小括号运算符,做一个仿函数
    87. void operator()()
    88. {
    89. cout<<520<<" "<<1314<
    90. }
    91. //重写类型转换运算符
    92. operator int()
    93. {
    94. return real; //将该数据类型向int类型转换后,使用的是real的值
    95. }
    96. };
    97. //全局函数版实现加号运算符重载:实部+实部 虚部+虚部
    98. const Complex operator+ (const Complex &L, const Complex &R)
    99. {
    100. //定义一个临时空间
    101. Complex c;
    102. c.real = L.real + R.real;
    103. c.vir = L.vir + R.vir;
    104. return c;
    105. }
    106. //重载输出运算符函数
    107. ostream &operator<<(ostream &L, Complex &c)
    108. {
    109. if(c.vir>=0)
    110. {
    111. L<" + "<"i"<
    112. }else
    113. {
    114. L<"i"<
    115. }
    116. //返回左操作数自身的引用
    117. return L;
    118. }
    119. int main()
    120. {
    121. Complex c1(5,3);
    122. c1.show(); //5+3i
    123. Complex c2(2,-1);
    124. c2.show(); //2-1i
    125. Complex c3 = c1-c2; //调用加法运算符重载函数 c1.operator-(c2)
    126. c3.show(); //3+4i
    127. if(c3 > c2) //调用关系运算符重载函数
    128. {
    129. cout<<"yes"<
    130. }else
    131. {
    132. cout<<"no"<
    133. }
    134. c3[0] = 5; //将实部进行修改成5,调用中括号运算符重载
    135. c3.show(); //5+4i
    136. c3 += c2; //调用+=运算符重载函数
    137. c3.show(); //7+3i
    138. Complex c4 = -c3; //调用-号运算符重载
    139. c4.show(); //-7 - 3i
    140. c3.show(); //7+3i
    141. Complex c5 = ++c3; //调用前置自增运算符重载函数
    142. c5.show(); //8+4i
    143. c3.show(); //8+4i
    144. Complex c6 = c3++; //调用后置自增运算符重载函数
    145. c6.show(); //8+4i
    146. c3.show(); //9+5i
    147. cout<//cout.operator<<(c3)
    148. c3("hello world"); //c3.operator()("hello world");
    149. c3();
    150. int num = (int)c3; //调用类型转换运算符重载函数
    151. cout<<"num = "<
    152. return 0;
    153. }

  • 相关阅读:
    PAT乙级 1101 B是A的多少倍 C语言实现
    4_其他基础题
    hadoop宕机的处理方法
    Unity3D DOTS JobSystem物理引擎的使用详解
    猿创征文|Python迭代器、生成器、装饰器、函数闭包
    844. Backspace String Compare
    Java本地高性能缓存实践
    Python的基础算法题
    Git通过rebase合并多个commit
    写一个函数,将一个字符串中的元音字母复制到另一字符串,然后输出。
  • 原文地址:https://blog.csdn.net/cwj442257772/article/details/133514717