对运算符重载和友元函数的例子

以下是博主在学习完运算符重载和友元函数后编写的一个例子,实现一个复数的基本运算。
定义的头文件:

/******************************************************************
        complex.h                     Defination of the complex
******************************************************************/

#ifndef _complex_
#define _complex_
#include<cmath>
#include<iostream>
using std::sqrt ;
using std::ostream;   // Error :    ‘ostream’ does not name a type
using std::istream; 

class Complex{
private:
double real ;
double imag ;
public:
Complex( double real=0 , double imag=0 ) { this->real = real ;   this->imag = imag ; }
void zero() {   real = imag = 0 ; }
double module() {   return sqrt(real*real+imag*imag); }
double angle()  {   return atan2(imag,real); }

//     operator overloading
/******************************************************/

Complex operator+(const Complex & com) const;
Complex operator-(const Complex & com) const;
Complex operator*(const Complex & com) const ;
Complex operator/(const Complex & com) const;
Complex operator~() const;
/*******************************************************/

/******************************************************************/
//    template overloading  for the general operation of built-in type 

 Complex operator+(double value);
 Complex operator-(double value);
 Complex operator*(double value);
 Complex operator/(double value);

//   friend functions  
friend ostream & operator<<( ostream & os ,  Complex & com );
friend Complex operator+( double value , Complex & com );
friend istream & operator>>( istream & is ,  Complex & com );

friend Complex operator+( double value , Complex & com );
friend Complex operator-( double value , Complex & com );
friend Complex operator*( double value , Complex & com );
friend Complex operator/( double value , Complex & com );

};
#endif

实现的文件:

/***********************************************
             Realization of the Complex
************************************************/

#include "complex.h"
#include <iostream>
using std::ostream;
using std::istream;
/*
    Complex( double real=0 , double imag=0 ) { this->real = real ;   this->imag = imag ; }
    void zero() {   real = imag = 0 ; }
    double module() {   return sqrt(real*real+imag*imag); }
    double angle()  {   return atan2(imag,real); }

    //     Operator overloading

Complex operator+(const Complex & com) const;
Complex operator-(const Complex & com) const;
Complex operator*(const Complex & com) const ;
Complex operator/(const Complex & com) const;
Complex operator~() const;

//      Template overloading  for the general operation of built-in type 
 Complex operator+(double value);
 Complex operator-(double value);
 Complex operator*(double value);
 Complex operator/(double value);

//      Friend functions  
friend ostream & operator<<( ostream & os ,  Complex & com );
friend Complex operator+( double value , Complex & com );
friend istream & operator>>( istream & is ,  Complex & com );
friend Complex operator+( double value , Complex & com );
friend Complex operator-( double value , Complex & com );
friend Complex operator*( double value , Complex & com );
friend Complex operator/( double value , Complex & com );

*/

/***********************************************************
        Operator Overloading
***********************************************************/

Complex Complex::operator+(const Complex & com) const{
    Complex result ;
    result.real = this->real + com.real ;
    result.imag = this->imag + com.imag ;
    return result ;
}

Complex Complex::operator-(const Complex & com) const{
    Complex result ;
    result.real = this->real - com.real ;
    result.imag = this->imag - com.imag ;
    return result ;
}   

Complex Complex::operator*(const Complex & com) const{
    Complex result ;
    result.real = (this->real) * com.real - (this->imag*com.imag);
    result.imag = this->real*com.imag + this->imag*com.real ;
    return result ;
}

Complex Complex::operator/(const Complex & com) const{
    Complex result ;
    result.real = (this->real*com.real + this->imag*com.imag)/(com.real*com.real+com.imag*com.imag);
    result.real = (this->imag*com.real - this->real*com.imag)/(com.real*com.real+com.imag*com.imag);
    return result ;
}

Complex Complex::operator~()const{
    Complex result ;
    result.real = (this->real) ;
    result.imag = -this->imag  ;
    return result ;
}

/*******************************************************************************
    Template overloading  for the general operation of built-in type 
********************************************************************************/

Complex Complex::operator+(double value){
    Complex result ;
    result.real = this->real+value;
    result.imag = this->imag;

    return result ;
}

Complex Complex::operator-(double value){
    Complex result ;
    result.real = this->real-value;
    result.imag = this->imag;

    return result ;
}

Complex Complex::operator*(double value){
    Complex result ;
    result.real = this->real*value;
    result.imag = this->imag*value;

    return result ;
}

Complex Complex::operator/(double value){
    Complex result ;
    result.real = this->real/value;
    result.imag = this->imag/value;

    return result ;
}

/**********************************************************
             friend functions
***********************************************************/
ostream & operator<<( ostream & os ,  Complex & com ){
    os<<com.real<<'+'<<com.imag<<'j';
    return os ;
}

istream & operator>>( istream & is ,  Complex & com ){
    std::cout<<"Enter the real part :";
    is>>com.real;
    is.clear();
    std::cout<<"Enter the image part:";
    is>>com.imag;

    return is ;
}

//    Behold this part , Never get the Istream wrong again !!!!!

Complex operator+( double value , Complex & com ){
    Complex result ;
    result.real = com.real+value;
    result.imag = com.imag;

    return result ;
}

Complex operator-( double value , Complex & com ){
    Complex result ;
    result.real = com.real-value;
    result.imag = com.imag;

    return result ;
}

Complex operator*( double value , Complex & com ){
    Complex result ;
    result.real = com.real*value;
    result.imag = com.imag*value;

    return result ;
}

Complex operator/( double value , Complex & com ){
    Complex result ;
    result.real = com.real/value;
    result.imag = com.imag/value;

    return result ;
}

测试文件:
#include "complex.h"
#include<iostream>

using namespace std ;

int main(){
    Complex a(3.0,4.0);
    Complex c ;
    Complex result ;

    while( std::cin>>c )
    {
        std::cout<<" c is "<<c<<'\n';
        std::cout<<" complex conjugate is "<<(result=~c)<<'\n';
        std::cout<<" a is "<<a<<'\n';
        std::cout<<" a + c is "<<(result=a+c)<<'\n';
        std::cout<<" a - c is "<<(result=a-c)<<'\n';
        std::cout<<" a * c is "<<(result=a*c)<<'\n';
        std::cout<<" a / c is "<<(result=a/c)<<'\n';
        std::cout<<" the mod   of c is "<<c.module()<<'\n';
        std::cout<<" the angle of c is "<<c.angle()<<'\n';
    }

    return 0 ;
}

在编写时,博主遇到了一个问题:
invalid initialization of non-const reference of type ‘Complex&’ from an rvalue of type ‘Complex’
这是由于函数的返回是一个暂时的量所导致的,所以需要将结果用一个变量保存起来,否则就会报错。
因此就有了以上的写法:(result=a/c)
关于这个问题可以参考:https://blog.csdn.net/u011068702/article/details/64443949

猜你喜欢

转载自blog.51cto.com/13824643/2133140