(3) Define a fully defined class (which can be released as a standalone product and become a "foundation project" in many projects). This class is based on (2), extending the function of the + 、-、 * and/operator so that it can be operated with a double type of data. Set complex C; Double D; The result of C+d and D+c is "add the complex with C as the real Part D", and the other-, *,//operators are similar.
Class Complex {public: Complex () {real=0;imag=0;} Complex (double r,double i) {real=r; imag=i;} Complex operator+ (const Complex &C2); Complex operator-(const Complex &C2); Complex operator* (const Complex &C2); Complex operator/(const Complex &C2); void display ();p rivate: double real; Double imag;};/ /below defines the member function//The following defines the main () function used for testing 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 (); 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+ (double d1, Complex &c2); nbsp; friend Complex operator+ (Complex &c1, double D2); friend Complex operator-( Complex &c1, Complex &c2); friend Complex operator-(double d1, Complex &c2); & nbsp Friend Complex operator-(Complex &c1, double D2); friend Complex operator* (Complex &c1, Complex &c2); friend Complex operator* (double d1, CompLex &c2); friend Complex operator* (Complex &c1, double D2); friend Complex operator/(Complex &c1, Complex &c2); friend Complex operator/(double d1, Complex &c2); nbsp; friend Complex operator/(Complex &c1, double D2); void display ();p rivate: 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+ (double d1, Complex &c2) { Complex C (d1,0); return C+C2;} Complex operator+ (Complex &c1, double D2) { Complex C (d2,0); return c1+c;} Complex operator-(Complex &c1, Complex &c2) { Complex c; c.real= c1.real-c2.real; C.IMAG=C1.IMAG-C2.IMAG;&Nbsp; return c;} Complex operator-(double d1, Complex &c2) { Complex C (d1,0); return C-C2;} Complex operator-(Complex &c1, double D2) { Complex C (d2,0); return c1-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* (double d1, Complex &c2) { Complex C (d1,0); return C*C2;} Complex operator* (Complex &c1, double D2) { Complex C (d2,0); return c1*c;} Complex operator/(Complex &c1, Complex &c2) { Complex c; c.real= (c1.real* C2.REAL+C1.IMAG*C2.IMAG)/(C2.real*c2.real+c2.imag*c2.imag); c.imag= (c1.imag*c2.real-c1.real* C2.IMAG)/(C2.REAL*C2.REAL+C2.IMAG*C2.IMAG); returnC;} Complex operator/(double d1, Complex &c2) { Complex C (d1,0); return C/C2;} Complex operator/(Complex &c1, double D2) { Complex C (d2,0); return c1/c;}
void Complex::d isplay () { cout<< "(" <<real<< "," <<imag<< "i)" <<endl;}
int main () { Complex C1 (3,4), C2 (5,-10),c3; double d=11; cout << "c1="; c1.display (); cout<< "c2="; c2.display ( ); cout<< "d=" <<d<<endl<<endl; cout<< " The following is the calculation result of the overloaded operator: "<<endl; c3=c1+c2; cout<<" c1+c2= "; C3.display (); cout<< "c1+d="; (c1+d). Display (); cout<< "d+c1="; (D+C1). Display (); c3=c1-c2; cout< < "c1-c2="; c3.display (); cout<< "c1-d="; (c1-d). Display (); cout<< "d-c1="; (D-C1). Display (); c3=c1*c2; cout<< "c1*c2="; c3.display (); cout<< "c1*d="; nbsp; (c1*d). Display (); cout<< "d*c1="; (D*C1). display (); c3=c1/c2; cout<< "c1/c2="; c3.display (); cout<< "c1/d="; (c1/d). Display (); cout<< "d/c1="; (D/C1). Display (); return 0;}
Item 1-Implementing operator overloading in a complex number Class 3