方案一:用类的成员函数完成运算符的重载
- #include<iostream>
- using namespace std;
- class Complex
- {
- public:
- Complex(){real=0;imag=0;}
- Complex(double r,double i){real=r;imag=i;}
- Complex operator+(Complex &c2);
- Complex operator-(Complex &c2);
- Complex operator*(Complex &c2);
- Complex operator/(Complex &c2);
- void display();
- private:
- double real;
- double imag;
- };
- //下面定义成员函数
- //复数的相加运算:
- Complex Complex::operator+(Complex &c2)
- {
- Complex c;
- c.real=real+c2.real;
- c.imag=imag+c2.imag;
- return c;
- }
- //复数的相减运算:
- Complex Complex::operator-(Complex &c2)
- {
- Complex c;
- c.real=real-c2.real;
- c.imag=imag-c2.imag;
- return c;
- }
- //复数的相乘运算:
- Complex Complex::operator*(Complex &c2)
- {
- Complex c;
- c.real=real*c2.real-imag*c2.imag;
- c.imag=imag*c2.real+real*c2.imag;
- return c;
- }
- //复数的相除运算:
- Complex Complex::operator/(Complex &c2)
- {
- Complex c;
- double d=c2.real*c2.real+c2.imag*c2.imag;
- c.real=(real*c2.real+imag*c2.imag)/d;
- c.imag=(imag*c2.real-real*c2.imag)/d;
- return c;
- }
- void Complex::display( )
- {
- cout<<"("<<real<<","<<imag<<"i)"<<endl;
- }
- int main()
- {
- Complex c1(3,4),c2(5,-10),c3;
- cout<<"c1=";
- c1.display();
- cout<<"c2=";
- c2.display();
- c3=c1+c2;
- cout<<"c1+c2=";
- c3.display();
- c3=c1-c2;
- cout<<"c1-c2=";
- c3.display();
- c3=c1*c2;
- cout<<"c1*c2=";
- c3.display();
- c3=c1/c2;
- cout<<"c1/c2=";
- c3.display();
- system("pause");
- return 0;
- }
方案二:用类的友元函数,而不是成员函数,完成运算符的重载
- #include<iostream>
- using namespace std;
- class Complex
- {
- public:
- Complex(){real=0;imag=0;}
- Complex(double r,double i){real=r;imag=i;}
- friend Complex operator+(Complex &c1,Complex &c2);
- friend Complex operator-(Complex &c1,Complex &c2);
- friend Complex operator*(Complex &c1,Complex &c2);
- friend Complex operator/(Complex &c1,Complex &c2);
- friend void display(Complex &c2);
- private:
- double real;
- double imag;
- };
- //下面定义友元函数
- //复数的相加运算:
- Complex operator+(Complex &c1,Complex &c2)
- {
- Complex c;
- c.real=c1.real+c2.real;
- c.imag=c1.imag+c2.imag;
- return c;
- }
- //复数的相减运算:
- Complex operator-(Complex &c1,Complex &c2)
- {
- Complex c;
- c.real=c1.real-c2.real;
- c.imag=c1.imag-c2.imag;
- return c;
- }
- //复数的相乘运算:
- Complex operator*(Complex &c1,Complex &c2)
- {
- Complex c;
- c.real=c1.real*c2.real-c1.imag*c2.imag;
- c.imag=c1.imag*c2.real+c1.real*c2.imag;
- return c;
- }
- //复数的相除运算:
- Complex operator/(Complex &c1,Complex &c2)
- {
- Complex c;
- double d=c2.real*c2.real+c2.imag*c2.imag;
- c.real=(c1.real*c2.real+c1.imag*c2.imag)/d;
- c.imag=(c1.imag*c2.real-c1.real*c2.imag)/d;
- return c;
- }
- void display(Complex &c2)
- {
- cout<<"("<<c2.real<<","<<c2.imag<<"i)"<<endl;
- }
- int main()
- {
- Complex c1(3,4),c2(5,-10),c3;
- cout<<"c1=";
- display(c1);
- cout<<"c2=";
- display(c2);
- c3=c1+c2;
- cout<<"c1+c2=";
- display(c3);
- c3=c1-c2;
- cout<<"c1-c2=";
- display(c3);
- c3=c1*c2;
- cout<<"c1*c2=";
- display(c3);
- c3=c1/c2;
- cout<<"c1/c2=";
- display(c3);
- system("pause");
- return 0;
- }
方案三:扩展+、-、*、/运算符的功能,使之能与double型数据进行运算
- #include<iostream>
- using namespace std;
- class Complex
- {
- public:
- Complex(){real=0;imag=0;}
- Complex(double r,double i){real=r;imag=i;}
- friend Complex operator+(Complex &c1,Complex &c2);
- friend Complex operator-(Complex &c1,Complex &c2);
- friend Complex operator-(Complex &c2);
- friend Complex operator*(Complex &c1,Complex &c2);
- friend Complex operator/(Complex &c1,Complex &c2);
- friend Complex operator+(Complex &c1,const double &d);
- friend Complex operator+(const double &d, Complex &c);
- friend void display(Complex &c2);
- private:
- double real;
- double imag;
- };
- //下面定义友元函数
- //复数相加: (a+bi)+(c+di)=(a+c)+(b+d)i.
- Complex operator+(Complex &c1,Complex &c2)
- {
- Complex c;
- c.real=c1.real+c2.real;
- c.imag=c1.imag+c2.imag;
- return c;
- }
- Complex operator+(Complex &c1,const double &d)
- {
- return Complex(c1.real+d, c1.imag);
- }
- Complex operator+(const double &d, Complex &c1)
- {
- return Complex(c1.real+d, c1.imag);
- }
- //复数相减:(a+bi)-(c+di)=(a-c)+(b-d)i.
- Complex operator-(Complex &c1,Complex &c2)
- {
- Complex c;
- c.real=c1.real-c2.real;
- c.imag=c1.imag-c2.imag;
- return c;
- }
- Complex operator-(Complex &c2)
- {
- return Complex(-c2.real, -c2.imag);
- }
- //复数相乘:(a+bi)(c+di)=(ac-bd)+(bc+ad)i.
- Complex operator*(Complex &c1,Complex &c2)
- {
- Complex c;
- c.real=c1.real*c2.real-c1.imag*c2.imag;
- c.imag=c1.imag*c2.real+c1.real*c2.imag;
- return c;
- }
- //复数相除:(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)i
- Complex operator/(Complex &c1,Complex &c2)
- {
- Complex c;
- double d=c2.real*c2.real+c2.imag*c2.imag;
- c.real=(c1.real*c2.real+c1.imag*c2.imag)/d; //此处有危险未排除:除法溢出
- c.imag=(c1.imag*c2.real-c1.real*c2.imag)/d;
- return c;
- }
- void display(Complex &c2)
- {
- cout<<"("<<c2.real<<","<<c2.imag<<"i)"<<endl;
- }
- int main()
- {
- Complex c1(3,4),c2(5,-10),c3,c4;
- cout<<"c1=";
- display(c1);
- cout<<"c2=";
- display(c2);
- c3=c1+c2;
- cout<<"c1+c2=";
- display(c3);
- c3=c1-c2;
- cout<<"c1-c2=";
- display(c3);
- c3=c1*c2;
- cout<<"c1*c2=";
- display(c3);
- c3=c1/c2;
- cout<<"c1/c2=";
- display(c3);
- c4=c1+3.14;
- cout<<"c1+3.14=";
- display(c4);
- c4=3.14+c1;
- cout<<"3.14+c1=";
- display(c4);
- c4=-c1;
- cout<<"-c1=";
- display(c4);
- system("pause");
- return 0;
- }
-