侯捷《C++面向对象开发》--复数类的实现

//防范式声明,可理解为:if not define __COMPLEX__,接下来 define __COMPLEX__
#ifndef __COMPLEX__                        
#define __COMPLEX__
#include<ostream>

class complex
{
public:
    complex(const double r=0, const double i=0)                //构造函数,r和i的默认参数为0
        :re(r),im(i)                                           //初始化:将re初始化为r,im初始化为i
    {}

    //操作符重载,参数中实际上还包含一个隐藏参数this,返回的是一个complex的引用
    complex& operator += (const complex&);                     
    complex& operator -= (const complex&);
    complex& operator *= (const complex&);
    complex& operator /= (const complex&);

    // 成员函数,用于查看一个complex对象的实数部分和虚数部分,我们不希望这部分被更改,所以添加const
    double real() const { return re; }
    double imag() const { return im; }

    //成员函数,用于计算共轭复数
    complex conjugate() const
    {
        return complex(this->real(), -1 * this->imag());
    }
private:
    double re, im;

    // 友元函数,友元函数不是类的成员函数,但友元函数可以通过.操作获取类的private变量。友元函数打破了类的封装
    friend complex& __doapl(complex*, const complex&);
    friend complex& __doami(complex*, const complex&);
    friend complex& __doamu(complex*, const complex&);
};

//重载+=
complex& __doapl(complex* ths, const complex& x)
{
    ths->re += x.re;
    ths->im += x.im;
    return *ths;
}

complex& complex::operator += (const complex& x)
{
    return __doapl(this, x);
}

//重载-=
complex& __doami(complex* ths, const complex& x)
{
    ths->re -= x.re;
    ths->im -= x.im;
    return *ths;
}

complex& complex::operator-= (const complex& x)
{
    return __doami(this, x);
}

//重载*=
complex& __doamu(complex* ths, const complex& x)
{
    double temp = ths->re * x.re - ths->im * x.im;
    ths->im = (ths->re * x.im + ths->im * x.re);
    ths->re = temp;
    return *ths;
}

complex& complex::operator*= (const complex& x)
{
    return __doamu(this, x);
}


//重载/=
complex& __doadiv(complex* ths, const complex& x)
{
    double r = (ths->real() * x.real() + ths->imag() * x.imag()) / (x.real() * x.real() + x.imag() * x.imag());
    double i = (ths->imag() * x.real() - ths->real() * x.imag()) / (x.real() * x.real() + x.imag() * x.imag());
    *ths = complex(r, i);
    return *ths;
}

complex& complex::operator/= (const complex& x)
{
    return __doadiv(this, x);
}

//重载+操作,具体分为三种情况:复数+复数、复数+实数、复数+实数。其它重载操作类似
// 这里返回的不是reference,而是一个value。
//不能返回reference的原因是:进行‘+’操作后得到的对象,存放在一个临时的complex中。
//这个临时对象的生命周期在函数结束时结束。
complex operator + (const complex& c1, const complex& c2)
{
    return complex(c1.real() + c2.real(), c1.imag() + c2.imag());
}

complex operator + (const complex& c1, const double& c2)
{
    return complex(c1.real() + c2, c1.imag());
}

complex operator + (const double& c1, const complex& c2)
{
    return complex(c1 + c2.real(), c2.imag());
}

complex operator - (const complex& c1, const complex& c2)
{
    return complex(c1.real() - c2.real(), c1.imag() - c2.imag());
}

complex operator - (const complex& c1, const double& c2)
{
    return complex(c1.real() - c2, c1.imag());
}

complex operator - (const double& c1, const complex& c2)
{
    return complex(c1 - c2.real(), c2.imag());
}

complex operator * (const complex& c1, const complex& c2)
{
    double r = c1.real() * c2.real() - c1.imag() * c2.imag();
    double i = c1.real() * c2.imag() + c1.imag() * c2.real();
    return complex(r, i);
}

complex operator / (const complex& c1, const complex& c2)
{
    double r = (c1.real() * c2.real() + c1.imag() * c2.imag()) / (c2.real() * c2.real() + c2.imag() * c2.imag());
    double i = (c1.imag() * c2.real() - c1.real() * c2.imag()) / (c2.real() * c2.real() + c2.imag() * c2.imag());
    return complex(r, i);
}

inline double real(const complex& x)
{
    return x.real();
}

inline double imag(const complex& x)
{
    return x.imag();
}

// 重载输出操作
std::ostream& operator << (std::ostream& os, const complex& x)
{
    return os << "(" << real(x) << "," << imag(x) << ")";
}
#endif // !__COMPLEX__



#include"complex.h"
#include<ostream>
#include<iostream>
using namespace std;

int main()
{
    complex c1(double(1), double(2));
    while (1)
    {
        double a, b,c,d;
        cin >> a >> b >> c >> d;
        complex c1(a, b);
        complex c2(c, d);
        cout << "c1" << c1 << endl;
        cout << "c2" <<c2 << endl;
        cout << "conjugate" << c1.conjugate() << endl;
        cout << "c1/c2=" << (c1 / c2) << endl;
        cout << "c1*c2=" << (c1 * c2) << endl;
        cout << "c1-c2=" << (c1 - c2) << endl;
        cout << "c1+c2=" << (c1 + c2) << endl;
        cout << "c1*=c2=" << (c1 *= c2) << endl;
        cout << real(c1) << " " <<imag(c1) << endl;
    }
    return 0;
}

 

上一篇:proxy_pass


下一篇:数据库:计算地球上两个坐标点之间里程