C++ 操作符重载(2) 复数加减乘除

复数由实部和虚部组成 复数间的运算通过实部和虚部,我们构建一个复数类来模拟复数间的操作

类定义如下:

  1. //复数的运算
  2. class Complex {
  3. private:
  4. int real; //复数的实部
  5. int image; //复数的虚部
  6. public:
  7. void myPrint() {
  8. cout << this->real << " " << this->image << "i" << endl; //显示复数
  9. }
  10. Complex( int real= 0, int image= 0) { //构造函数
  11. this->real = real;
  12. this->image = image;
  13. }

现在我们通过操作符的重载来实现复数间的直接运算

加号的重载:

  1. //重载加号
  2. Complex operator+( const Complex &obj) {
  3. Complex res;
  4. res.real = this->real + obj.real;
  5. res.image = this->image + obj.image;
  6. return res;
  7. }

重载的重要信息:

1)为了能实现操作符重载,至少得有一个对象是用户自定义的对象,如上述加号重载是基于Complex类对象

2)赋值操作符: 编译器会自动的给每个类创建一个默认的赋值操作符。它会将所有成员从右边赋给左边,大多数情况下可以正常工作(不正常的地方与拷贝构造一样)。



全局运算符重载函数:

运算符函数与普通函数一样。唯一的不同在于,运算符函数的名字通常由operator关键字加上运算操作符组成。

我们可以把操作符函数定义为全局的


  1. Complex operator * ( const Complex &obj1, const Complex &obj2) {
  2. Complex res;
  3. res.real = obj1.real * obj2.real;
  4. res.image = obj1.image * obj2.image;
  5. return res;
  6. }
然后在类中声明友元

friend Complex operator * (const Complex &obj1, const Complex &obj2);

这样不但实现了全局函数,还可以传入两个形参


类型转换的重载:

我们可以自定义类型转换操作符,把一个类型转换成另一个类型

  1. operator float() const {
  2. return float( this->real) / float( this->image);
  3. }

使用:

  1. float val = C;
  2. cout << val << endl;

整个程序如下:

  1. #include<iostream>
  2. using namespace std;
  3. //复数的运算
  4. class Complex {
  5. private:
  6. int real; //复数的实部
  7. int image; //复数的虚部
  8. public:
  9. void myPrint() {
  10. cout << this->real << " " << this->image << "i" << endl; //显示复数
  11. }
  12. Complex( int real= 0, int image= 0) { //构造函数
  13. this->real = real;
  14. this->image = image;
  15. }
  16. //重载加号
  17. Complex operator+( const Complex &obj) {
  18. Complex res;
  19. res.real = this->real + obj.real;
  20. res.image = this->image + obj.image;
  21. return res;
  22. }
  23. //重载减号
  24. Complex operator-( const Complex &obj) {
  25. Complex res;
  26. res.real = this->real - obj.real > 0 ? this->real - obj.real : 0;
  27. res.image = this->image - obj.image > 0 ? this->image - obj.image : 0;
  28. return res;
  29. }
  30. //重载乘号
  31. //使全局函数可以使用私有的类
  32. friend Complex operator * ( const Complex &obj1, const Complex &obj2);
  33. operator float() const {
  34. return float( this->real) / float( this->image);
  35. }
  36. };
  37. //全局的乘号重载
  38. Complex operator * ( const Complex &obj1, const Complex &obj2) {
  39. Complex res;
  40. res.real = obj1.real * obj2.real;
  41. res.image = obj1.image * obj2.image;
  42. return res;
  43. }
  44. int main() {
  45. Complex A(10, 20);
  46. Complex B(20, 30);
  47. A.myPrint();
  48. cout << endl << endl;
  49. B.myPrint();
  50. cout << endl << endl;
  51. Complex C = A + B; //重载的加号
  52. C.myPrint();
  53. C = B - A; //重载的减号
  54. C.myPrint();
  55. C = B * A; //重载的乘号
  56. C.myPrint();
  57. float val = C;
  58. cout << val << endl;
  59. system( "PAUSE");
  60. return 0;
  61. }

猜你喜欢

转载自blog.csdn.net/qq_40955914/article/details/80836119