C++运算符重载(以复数为例)

来源:互联网 发布:stc12c5a16s2 编程 编辑:程序博客网 时间:2024/06/06 03:48
#include <iostream>// using namespace std;using std::cout;using std::endl;using std::cin;using std::istream;using std::ostream;class Complex {public:Complex();// default constructorComplex( double );// constructorComplex( double, double );// constructorComplex& operator=( const Complex& );// overload the assignment operatorComplex& operator+=( const Complex& );// overload operator +=Complex& operator-=( const Complex& );// overload operator -=Complex& operator*=( const Complex& );// overload operator *=Complex& operator/=( const Complex& );// overload operator /=Complex& operator++();// overload the prefix self-additionComplex& operator--();// overload the prefix self-subtrationComplex operator++( int );// overload the suffix self-additionComplex operator--( int );// overload the suffix self-subtrationvoid print() const;// the function to output the complexfriend Complex operator+( const Complex&, const Complex& );// overload the addition operatorfriend Complex operator-( const Complex&, const Complex& );// overload the subtration operatorfriend Complex operator*( const Complex&, const Complex& );// overload the multiplication operatorfriend Complex operator/( const Complex&, const Complex& );// overload the division operatorfriend bool operator==( const Complex&, const Complex& );// overload the equivalent operatorfriend bool operator!=( const Complex&, const Complex& );// overload the unequal operatorfriend ostream& operator<<( ostream&, const Complex& );friend istream& operator>>( istream&, Complex& );private:double real;// the real part of the complexdouble imag;// the imaginary part of the complex};/** * The default constructor to create a complex without parameter. * Both the real part and imaginary part are made zero. * */Complex::Complex() {real = 0;imag = 0;}/** * The constructor to create a complex with one paramater to set the real part, * the imaginary part is made zero. */Complex::Complex( double r ) {real = r;imag = 0;}/** * The constructor to create a complex with two paramaters. */Complex::Complex( double r, double i ) {real = r;imag = i;}/** * The program is to overload the assignment operator with a paramater. *  * @param t       the complex to assign. * @return        the self object */ Complex& Complex::operator=( const Complex& t ) {real = t.real;// assign the real part of the paramater to the objectimag = t.imag;// assign the imaginary part of the paramater to the objectreturn *this;}/** * This program is to overload the operator +=,with a complex paramater. * * @param t   the complex to be added to the objext * @return    self object */Complex& Complex::operator+=( const Complex& t ) {real = real + t.real;// add the real part of the paramater to the real part of the objectimag = imag + t.imag;// add the imaginary part of the paramater to the imaginary part of the objectreturn *this;}/** * This program is to overload the operator -=,with a complex paramater. * * @param t   the complex to be subtracted to the objext * @return    self object */Complex& Complex::operator-=( const Complex& t ) {real = real - t.real;// subtract the real partimag = imag - t.imag;// subtract the imaginary partreturn *this;}/** * This program is to overload the operator *=,with a complex paramater. * * @param t   the complex to be multiplied to the objext * @return    self object */Complex& Complex::operator*=( const Complex& t ) {double temp1;// the first intermediate variabledouble temp2;// the second intermediate variabletemp1 = real;temp2 = imag;// calculate the result and set it to the object real = temp1 * t.real - temp2 * t.imag;imag = temp1 * t.imag + temp2 * t.real;return *this;}/** * This program is to overload the operator /=,with a complex paramater. * * @param t   the complex to be divided to the objext * @return    self object */Complex& Complex::operator/=( const Complex& t ) {double temp1;// the first intermediate variabledouble temp2;// the second intermediate variabletemp1 = real;temp2 = imag;// calculate the result and set it to the objectreal = ( temp1 * t.real + temp2 * t.imag ) / ( t.real * t.real + t.imag * t.imag );imag = ( temp2 * t.real - temp1 * t.imag ) / ( t.real * t.real + t.imag * t.imag );return *this;}/** * This program is to overload the operator prefix ++,without paramater. * * @return    self object */Complex& Complex::operator++() {real++;// the real part add oneimag++;// the imaginary part add one return *this;}/** * This program is to overload the operator prefix --,without paramater. * * @return    self object */Complex& Complex::operator--() {real--;// the real part minus oneimag--;// the imaginary part minus onereturn *this;}/** * This program is to overload the operator suffix ++,without paramater. * * @return    the complex before add */Complex Complex::operator++( int ) {Complex temp( real, imag );// set the real part and imaginary part to another complex objectreal++;// the real part add oneimag++;// the imaginary add onereturn temp;}/** * This program is to overload the operator suffix --,without paramater. * * @return   the object before minus */Complex Complex::operator--( int ) {Complex temp( real, imag );// set the real part and imaginary part to another complex objectreal--;// the real part minus oneimag--;// the imaginary part minus onereturn temp;}/** * This program is to overload the operator +,with tow complex paramaters. * * @param t   the first addend * @param u   the second addend * @return    a complex object */Complex operator+( const Complex& t, const Complex& u ) {return Complex( t.real + u.real, t.imag + u.imag );}/** * This program is to overload the operator -,with two complex paramaters. * * @param t   the first subtrahend * @param u   the second subtrahend * @return    a complex object */Complex operator-( const Complex& t, const Complex& u ) {return Complex( t.real - u.real, t.imag - u.imag );}/** * This program is to overload the operator *,with two complex paramaters. * * @param t   the first multiplier * @param u   the second multiplier * @return    a complex object */Complex operator*( const Complex& t, const Complex& u ) {return Complex( t.real * u.real - t.imag * u.imag, t.real * u.imag + t.imag * u.real );}/** * This program is to overload the operator /,with two complex paramaters. * * @param t   the dividend * @param u   the divisor * @return    a complex object */Complex operator/( const Complex& t, const Complex& u ) {return Complex( ( t.real * u.real + t.imag * u.imag ) / ( u.real * u.real + u.imag * u.imag ), ( t.imag * u.real - t.real * u.imag ) / ( u.real * u.real + u.imag * u.imag ) );}/** * This program is to overload the operator ==,with two complex paramaters. * If both the real part and imaginary part are equal,return true;otherwise, * return false. * * @param t   the first complex to be compared * @param u   the second complex to be compared * @return    true or false */bool operator==( const Complex& t, const Complex& u ) {if ( t.real == u.real && t.imag == u.imag ) {// both the real part and imaginary part are equalreturn true;}// end ifelse {// not both the real part and imaginary part are equalreturn false;}// end else}/** * This program is to overload the operator !=,with two complex paramaters. * If not both the real part and imaginary part are equal,return true;otherwise, * return false. * * @param t   the first complex to be compared * @param u   the second complex to be compared * @return    true or false */bool operator!=( const Complex& t, const Complex& u ) {return !( t == u );}ostream& operator<<( ostream& os, const Complex& c) {os << c.real << "+" << c.imag << "i";return os;}istream& operator>>( istream& is, Complex& c) {is >> c.real >> c.imag;return is;}/** * This program is to output complex object in a format. */void Complex::print() const {cout << "(" <<real << "," << imag << ")" << endl;}int main(){Complex x( 1, 1 );Complex x1( 1, 1 );Complex y( 2, 2 );Complex z( 4, 4 );// invoke operator==if( x == x1 ) {cout << "The same!\n";}// invoke operator!=if ( x != y ) {cout << "Not same!\n";}// invoke operator=cout << "\nBefore assign,x1=";x1.print();x1 = y;cout << "After assign,x1=";x1.print();// invoke operator+=cout << "\nBefore the operator+=\n";cout << "x=";x.print();cout << "y=";y.print();x += y;cout << "After x+=y\n";cout << "x=";x.print();// invoke operator-=cout << "\nBefore the operator-=\nx=";x.print();cout << "y=";y.print();x -= y;cout << "After x-=y\nx=";x.print();// invoke operator*=cout << "\nBefore the operator*=\nx=";x.print();cout << "y=";y.print();x *= y;cout << "After x*=y\nx=";x.print();// invoke operator/=cout << "\nBefore the operator/=\nx=";x.print();cout << "y="; y.print();x /= y;cout << "After x/=y\nx=";x.print();// invoke operator+cout << "\nInvoke the operator+\nx=";x.print();cout << "y=";y.print();z = x + y;cout << "z=x+y=";z.print();// invoke operator-cout << "\nInvoke the operator-\nx=";x.print();cout << "y=";y.print();z = x - y;cout << "z=x-y=";z.print();// invoke operator*cout << "\nInvoke the operator*\nx=";x.print();cout << "y=";y.print();z = x * y;cout << "z=x*y=";z.print();// invoke operator/cout << "\nInvoke the operator/\nx="; x.print();cout << "y=";y.print();z = x / y;cout << "z=x/y=";z.print();// invoke operator prefix ++cout << "\nInvoke the operator prefix++\nx=";x.print();z = ++x;cout << "After z=++x,\nz="; z.print();cout << "x=";x.print();// invoke operator prefix --cout << "\nInvoke the operator prefix--\nx=";x.print();z = --x;cout << "After z=--x,\nz=";z.print();cout << "x=";x.print();// invoke operator suffix ++cout << "\nInvoke the operator suffix++\nx=";x.print();z = x++;cout << "After z=x++,\nz=";z.print();cout << "x=";x.print();// invoke operator suffix --cout << "\nInvoke the operator suffix--\nx=";x.print();z = x--;cout << "After z=x--,\nz=";z.print();cout << "x=";x.print();cin >> x;cout << x << endl;return 0;}

原创粉丝点击