[c++]複數的運算子多載

來源:互聯網
上載者:User

標籤:c++   重載   

類的定義和聲明

<span style="font-size:32px;">#include<iostream>#include<string>using namespace std;class Complex{    friend istream& operator>>(istream &in, Complex &c);    friend ostream& operator<<(ostream &out, const Complex &c);    friend Complex operator+(int x, Complex &c);public:    Complex(const int real=0,const int imag=0):m_real(real),m_imag(imag)    {}    ~Complex()    {}    void Show()const    {        cout<<"("<<m_real<<","<<m_imag<<")"<<endl;    }public:    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);    }    Complex operator*(Complex &c);    Complex operator/(Complex &c);    Complex operator+=(Complex &c);    Complex operator-=(Complex &c);    Complex operator*=(Complex &c);    Complex operator/=(Complex &c);    Complex operator+(int x);    //friend Complex operator+(int x, Complex &c);private:    int m_real;    int m_imag;};Complex Complex::operator+(int x){    return Complex(m_real+x,m_imag);}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 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){    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 Complex:: operator/=(Complex &c){    int i = m_real;    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-i*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;}</span>
測試程式:

int main(){    Complex c1(1,1);    Complex c2(1,-1);        Complex c;    //c1.operator+(c2)    c = c1 + c2;    cout<<c<<endl;//    c = c1 * c2;//    cout<<c<<endl;//    c = c1 / c2;//    cout<<c<<endl;//    c = 2 + c1;//    cout<<c<<endl;//    c1+=c2;//    cout<<c1<<endl;//    c1-=c2;//    cout<<c1<<endl;//    c1*=c2;//    cout<<c1<<endl;//    c1/=c2;//    cout<<c1<<endl;//請不要把它們一起測試,分開    Complex c3;    cin>>c3;    cout<<c3;    return 0;}


[c++]複數的運算子多載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.