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.
Questions and codes
/* Copyright (c) 2015, Yantai University School of Computer * All rights reserved. * File name: Test.cpp * Author: Simbin * Completion Date: April 26, 2015 * Version number: v1.0 */#include <iostream>using namespace Std;class comple X{public:complex () {real=0; imag=0; } Complex (double r,double i) {real=r; Imag=i; } Friend Complex operator+ (const Complex &c1,const Complex &C2); Friend Complex operator+ (const double a,const Complex &C2); Friend Complex operator+ (const Complex &c1,const double A); Friend Complex operator-(const Complex &c1,const Complex &C2); Friend Complex operator-(const double a,const Complex &C2); Friend Complex operator-(const Complex &c1,const double A); Friend Complex operator* (const Complex &c1,const Complex &C2); Friend Complex operator* (const double a,const Complex &C2); Friend Complex operator* (const Complex &c1,const double A); Friend Complex operator/(const Complex &c1,const Complex &C2); Friend Complex operator/(const double a,const Complex &C2); Friend Complex operator/(const Complex &c1,const double A); void display ();p rivate:double Real; Double imag;};/ /The following defines member functions Complex operator+ (const Complex &c1,const Complex &c2) {return Complex (c1.real+c2.real,c1.imag+ C2.IMAG);} Complex operator+ (const double A,const Complex &c2) {return Complex (A+C2.REAL,C2.IMAG);} Complex operator+ (const Complex &c1,const double A) {return Complex (A+C1.REAL,C1.IMAG);} Complex operator-(const Complex &c1,const Complex &c2) {return Complex (C1.REAL-C2.REAL,C1.IMAG+C2.IMAG);} Complex operator-(const double A,const Complex &c2) {return Complex (A-C2.REAL,C2.IMAG);} Complex operator-(const Complex &c1,const double A) {return Complex (C1.REAL-A,C1.IMAG);} Complex operator* (const Complex &c1,const Complex &c2) {return Complex (C1.real*c2.real-c1.imag*c2.imag, C1.IMAG*C2.REAL+C1.REAL*C2.IMAG);} Complex operator* (const Double a,const Complex &c2) {return Complex (A*C2.REAL,A*C2.IMAG);} Complex operator* (const Complex &c1,const double A) {return Complex (c1.real*a,c1.imag*a);} Complex operator/(const Complex &c1,const Complex &c2) {return Complex ((C1.REAL*C2.REAL+C1.IMAG*C2.IMAG)/( C2.real*c2.real+c2.imag*c2.imag), (C1.IMAG*C2.REAL-C1.REAL*C2.IMAG)/(C2.real*c2.real+c2.imag*c2.imag));} Complex operator/(const double A,const Complex &c2) {return Complex ((a*c2.real)/(C2.REAL*C2.REAL+C2.IMAG*C2.IMAG) , (-a*c2.imag)/(C2.real*c2.real+c2.imag*c2.imag));} Complex operator/(const Complex &c1,const double A) {return Complex ((c1.real*a)/(A*a), (c1.imag*a)/(A*a));} void Complex::d isplay () {if (imag>0) cout<<real<< "+" <<imag<< "I" <<endl; else cout<<real<<imag<< "I" <<ENDL;} The following defines the main () function for testing int main () {Complex C1 (3,4), C2 (5,-10), C3; Double a=1; cout<< "c1="; C1.display (); cout<< "c2="; C2.dIsplay (); cout<< "B=1" <<endl; C3=C1+C2; cout<< "c1+c2="; C3.display (); C3=c1+a; cout<< "C1+a="; C3.display (); C3=A+C2; cout<< "a+c2="; C3.display (); C3=C1-C2; cout<< "c1-c2="; C3.display (); C3=A-C1; cout<< "a-c1="; C3.display (); C3=C1*C2; cout<< "c1*c2="; C3.display (); C3=A*C1; cout<< "a*c1="; C3.display (); C3=C1/C2; cout<< "c1/c2="; C3.display (); c3=c1/a; cout<< "C1/a="; C3.display (); return 0;}
Operation Result:
Eighth week on-machine practice project The implementation of operator overloading in plural classes (3)