Definition of operator overloading:
A user's operations on a custom type, such as the operation of a complex number. You need to redefine the operation symbol (create function).
All operators in C + + can be overloaded except for the generic relational operator ".", the member pointer operator ". *", The scope operator "::", the sizeof operator, and the three-mesh operator "?:".
The operator overloads of the complex number are as follows:
<span Style= "FONT-SIZE:18PX;"
#include <iostream> #include <string>using namespace Std;class Complex; Complex operator+ (int x, Complex &c);ostream& operator<< (ostream &out, const Complex &c); istream& Amp Operator>> (IStream &in, Complex &c), class Complex{friend istream& operator>> (IStream &in, Complex &c); friend ostream& operator<< (ostream &out, const Complex &c); Friend Complex operator+ ( int x, Complex &c);p ublic:complex (const int real = 0, const int imag = 0): m_real (Real), M_imag (imag) {}~complex () {}voi D Show () const{cout << "(" << m_real << "," << m_imag << ")" << Endl; Public:complex operator+ (int x) {return Complex (m_real + x, M_imag);} Complex operator+ (Complex &c) {return Complex (m_real + c.m_real, M_imag + c.m_imag);} Complex operator-(Complex &c) {return Complex (m_real-c.m_real, m_imag-c.m_imag);} Friend Complex operator+ (int x, Complex &c) {return Complex (x + c.m_real, c.m_imag);} FRiend Complex operator-(int x, Complex &c) {return Complex (x-c.m_real,-c.m_imag);} Complex operator* (Complex &c); friend Complex operator* (int x, Complex &c); Complex operator/(Complex &c); friend Complex operator/(int x, Complex &c); Complex operator+= (Complex &c); Complex operator-= (Complex &c); Complex operator*= (Complex &c); Complex operator/= (Complex &c);p rivate:int m_real;int M_imag;}; Complex complex::operator/(Complex &c) {Complex Temp;temp.m_real = (m_real*c.m_real + m_imag*c.m_imag)/(c.m_real* C.m_real + c.m_imag*c.m_imag); Temp.m_imag = (M_imag*c.m_real-m_real*c.m_imag)/(C.m_real*c.m_real + C.m_imag*c.m_imag ); return temp;} Complex operator/(int x, Complex &c) {Complex temp;temp.m_real = (x*c.m_real)/(C.m_real*c.m_real + C.m_imag*c.m_imag ); Temp.m_imag = (-x*c.m_imag)/(C.m_real*c.m_real + c.m_imag*c.m_imag); return temp;} Complex Complex:: operator+= (Complex &c) {m_real = m_real + C.m_real;m_imag = m_imag + C.m_imag;return *this;} CompLex complex::operator-= (Complex &c) {m_real = M_real-c.m_real;m_imag = M_imag-c.m_imag;return *this;} Complex Complex:: operator* (Complex &c) {return Complex (M_real*c.m_real-m_imag*c.m_imag, M_imag*c.m_real + m_real* C.M_IMAG);} Complex operator* (int x, Complex &c) {return Complex (c.m_real*x, c.m_imag*x);} Complex complex::operator*= (Complex &c) {int i = M_real;m_real = M_real*c.m_real-m_imag*c.m_imag;m_imag = m_imag*c.m _real + I*c.m_imag;return *this;} Complex Complex:: operator/= (Complex &c) {m_real = (m_real*c.m_real + m_imag*c.m_imag)/(C.m_real*c.m_real + C.m_imag *C.M_IMAG); M_imag = (M_imag*c.m_real-m_real*c.m_imag)/(C.m_real*c.m_real + c.m_imag*c.m_imag); return *this;} ostream& operator<< (ostream &out, const Complex &c) {out << "(" << c.m_real << "," < < C.m_imag << ")"; return out;} istream& operator>> (IStream &in, Complex &c) {in >> c.m_real >> c.m_imag;return in;} int main () {Complex C1 (2, 1); Complex C2 (3, 2); Complex c;c = c1 + c2;cout<<c1<< "+" <<c2<< "=" <<c<<endl;c = C1 * C2;cout << C1 < ;< "*" << c2 << "=" << c << endl;c = c1/c2;cout << C1 << "*" << C2 << "=" << c << endl;c = 2 + c1;cout << "2" << "+" << c1 << "=" << C << endl;// c1+=c2;//cout<<c1<<endl;//c1-=c2;//cout<<c1<<endl;c1 *= c2;cout << C1 <&L T ENDL;C1/= c2;cout << c1 << endl;c = 4/c1;cout << c << Endl; Complex C3; cin>>c3; Cout<<c3;return 0;}
</span>
Operation Result:
Operator overloading for "C + +" complex numbers