【例】定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编程序,求两个复数之和。
#include <iostream> using namespace std; class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} double get_real(); double get_imag(); void display(); private: double real; double imag; }; double Complex::get_real() {return real;} double Complex::get_imag() {return imag;} void Complex::display() {cout<<"("<<real<<","<<imag<<"i)"<<endl;} Complex operator + (Complex &c1,Complex &c2) { return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag()); } int main() {Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; cout<<"c3="; c3.display(); return 0; }
回到这题,我们可以仿照上面编写程序。
定义一个复数类Complex,重载运算符“+”、“-”,“*”,“/”,使之能用于复数的加、减、乘、除。运算符重载函数作为Complex类的成员函数。编程序,分别计算两个复数之和、差、积和商。
#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 cn; cn.real = real + c2.real; cn.imag = imag + c2.imag; return cn; }
减法定义
Complex Complex::operator-(Complex &c2)
{ Complex cn; cn.real = real - c2.real; cn.imag = imag - c2.imag; return cn; }
乘法定义
Complex Complex::operator*(Complex &c2) { Complex cn; cn.real = real * c2.real-imag*c2.imag; cn.imag = imag *c2.real+real* c2.imag; return cn; }
除法定义
Complex Complex::operator/(Complex &c2) { Complex cn; cn.real = (real * c2.real + imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); cn.imag = (imag *c2.real - real* c2.imag)/(c2.real*c2.real + c2.imag*c2.imag); return cn; }
display定义
void Complex::display()
{ cout << "(" << real << "," << imag << "i)"<<endl; }
主函数
int main() { double real, imag; cin >> real >> imag; Complex c1(real, imag); cin >> real >> imag; Complex c2(real, imag); cout << setiosflags(ios::fixed); cout << setprecision(2); Complex 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(); return 0; }
其实,运算符重载很简单,搞懂加法,其他就套就可以了。
代码如下
#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(); void set(); 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=real*c2.imag+imag*c2.real; return c; } Complex Complex::operator /(Complex &c2) { Complex c; c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); return c; } void Complex::set() { cout<<"input real=";cin>>real; cout<<" imag=";cin>>imag; } void Complex::display() { cout<<"("<<real<<","<<imag<<"i)"<<endl; } int main() { Complex c1,c2,c3,c4,c5,c6; c1.set(); c2.set(); c3=c1+c2; c4=c1-c2; c5=c1*c2; c6=c1/c2; cout<<"c1=";c1.display(); cout<<"c2=";c2.display(); cout<<"c1+c2=";c3.display(); cout<<"c1-c2=";c4.display(); cout<<"c1*c2=";c5.display(); cout<<"c1/c2=";c6.display(); return 0; }