1,复数类应该具有的操作:
1,运算:+,-,*,/;
2,比较:==,!=;
3,赋值:=;
4,求模:modulus;
(5),完善的复数类操作符重载必不可少;
2,利用操作符重载:
1,统一复数与实数的运算方式;
1,Complex operator + (const Complex& c);
2,Complex operator - (const Complex& c);
3,Complex operator * (const Complex& c);
4,Complex operator / (const Complex& c);
2,统一复数域实数的比较方式;
1,bool operator == (const Complex& c);
2,bool operator != (const Complex& c);
3,赋值操作符:
1,Complex& operator = (const Complex& c);
2,特殊在只能将赋值操作符作为成员函数来实现;
4,这些成员函数是普通的函数,只不过使用了 operator 作为函数名的一部分, 因此就成为了操作符重载函数;
3,复数类的实现编程实验:
1,Complex.h 文件:
1 #ifndef _COMPLEX_H_ 2 #define _COMPLEX_H_ 3 4 class Complex 5 { 6 double a; 7 double b; 8 public: 9 Complex(double a = 0, double b = 0); 10 double getA(); 11 double getB(); 12 double getModulus(); 13 14 Complex operator + (const Complex& c); 15 Complex operator - (const Complex& c); 16 Complex operator * (const Complex& c); 17 Complex operator / (const Complex& c); 18 19 bool operator == (const Complex& c); 20 bool operator != (const Complex& c); 21 22 Complex& operator = (const Complex& c); 23 }; 24 25 #endif
2,Complex.cpp 文件:
1 #include "Complex.h" 2 #include "math.h" 3 4 Complex::Complex(double a, double b)// 默认值只能在函数声明的地方指定; 5 { 6 this->a = a; 7 this->b = b; 8 } 9 10 double Complex::getA() 11 { 12 return a; 13 } 14 15 double Complex::getB() 16 { 17 return b; 18 } 19 20 double Complex::getModulus() 21 { 22 return sqrt(a * a + b * b); 23 } 24 25 Complex Complex::operator + (const Complex& c) 26 { 27 double na = a + c.a; 28 double nb = b + c.b; 29 Complex ret(na, nb); // 将实部和虚部指定进去; 30 31 return ret; 32 } 33 34 Complex Complex::operator - (const Complex& c) 35 { 36 double na = a - c.a; 37 double nb = b - c.b; 38 Complex ret(na, nb); 39 40 return ret; 41 } 42 43 Complex Complex::operator * (const Complex& c) 44 { 45 double na = a * c.a - b * c.b; 46 double nb = a * c.b + b * c.a; 47 Complex ret(na, nb); 48 49 return ret; 50 } 51 52 Complex Complex::operator / (const Complex& c) 53 { 54 double cm = c.a * c.a + c.b * c.b; 55 double na = (a * c.a + b * c.b) / cm; 56 double nb = (b * c.a - a * c.b) / cm; 57 Complex ret(na, nb); 58 59 return ret; 60 } 61 62 bool Complex::operator == (const Complex& c) 63 { 64 return (a == c.a) && (b == c.b); 65 } 66 67 bool Complex::operator != (const Complex& c) 68 { 69 return !(*this == c); 70 } 71 72 Complex& Complex::operator = (const Complex& c) 73 { 74 if( this != &c ) 75 { 76 a = c.a; 77 b = c.b; 78 } 79 80 return *this; // 赋值操作可以连续,所以要返回左操作数; 81 }
3,main.cpp 文件:
1 #include <stdio.h> 2 #include "Complex.h" 3 4 int main() 5 { 6 Complex c1(1, 2); 7 Complex c2(3, 6); 8 Complex c3 = c2 - c1; 9 Complex c4 = c1 * c3; 10 Complex c5 = c2 / c1; 11 12 printf("c3.a = %f, c3.b = %f ", c3.getA(), c3.getB()); 13 printf("c4.a = %f, c4.b = %f ", c4.getA(), c4.getB()); 14 printf("c5.a = %f, c5.b = %f ", c5.getA(), c5.getB()); 15 16 Complex c6(2, 4); 17 18 printf("c3 == c6 : %d ", c3 == c6); 19 printf("c3 != c4 : %d ", c3 != c4); 20 21 (c3 = c2) = c1; 22 23 printf("c1.a = %f, c1.b = %f ", c1.getA(), c1.getB()); 24 printf("c2.a = %f, c2.b = %f ", c2.getA(), c2.getB()); 25 printf("c3.a = %f, c3.b = %f ", c3.getA(), c3.getB()); 26 27 return 0; 28 }
4,输出结果:
1 c3.a = 2.000000, c3.b = 4.000000 2 c4.a = -6.000000, c4.b = 8.000000 3 c5.a = 3.000000, c5.b = 0.000000 4 c3 == c6 : 1 5 c3 != c4 : 1 6 c1.a = 1.000000, c1.b = 2.000000 7 c2.a = 3.000000, c2.b = 6.000000 8 c3.a = 1.000000, c3.b = 2.000000
4,注意事项:
1,C++ 规定赋值操作符(=)只能重载为成员函数;
2,操作符重载不能改变原操作符的优先级;
3,操作符重载不能改变操作数的个数;
4,操作符重载不应改变操作符的原有语义;
5,小结:
1,复数的概念可以通过自定义类实现;
2,复数中的运算操作可以通过操作符重载实现;
3,赋值操作符只能通过成员函数实现;
4,操作符重载的本质为函数定义;
1,定义新的函数扩展已有功能;