C++ 運算子多載

來源:互聯網
上載者:User

標籤:運算子多載   c++   

<span style="font-family: Arial, Helvetica, sans-serif;">#include <iostream></span>
using namespace std;class complex{public:// 帶預設值的建構函式complex (double real = 0, double image = 0):_real(real),_image(image){cout<<"complex (double real = 0, double image = 0)"<<endl;}// 解構函式~complex(){cout<<"~complex()"<<endl;}// 拷貝建構函式complex (const complex& d)        :_image(d._image),_real(d._real){cout<<"complex (const complex& d)"<<endl;}// 賦值運算子多載complex& operator= (const complex& d){cout<<"operator= (const complex& d)"<<endl;if (this != &d){this->_real = d._real;this->_image = d._image;}return *this;}// 取地址運算子多載complex* operator& (){cout<<"operator&()"<<endl;return this;}// const修飾的取地址運算子多載const complex* operator& () const{cout<<"operator&() const"<<endl;return this;}void display(){cout<<"real:"<<_real<<"--image:"<<_image<<endl<<endl;}// 請實現下面的運算子多載!complex& operator++()  //前置++    //返回當前對象本身{    cout<<"operator++()"<<endl;        _real++;        _image++;return *this;                  //可以立即體現出前置++的值}complex operator++(int)//後置++{    cout<<"operator++(int)"<<endl;        complex c = *this;        this->_real++;        this->_image++;        return c;}complex& operator--(){        cout<<"operator--()"<<endl;        _real--;        _image--;        return *this;}complex operator--(int) //後置--{        cout<<"operator--()"<<endl;        complex c = *this;        this->_real--;        this->_image--;        return c;}complex operator+(const complex& c){        cout<<"operator+(const complex& c)"<<endl;        return complex(_real+c._real,_image+c._image);}complex operator-(const complex& c){        cout<<"operator-(const complex& c)"<<endl;        return complex(_real-c._real,_image-c._image);}complex& operator-=(const complex& c){        cout<<"operator-=(const complex& c)"<<endl;        _real -= c._real;        _image -= c._image;        return *this;}complex& operator+=(const complex& c){        cout<<"operator+=(const complex& c)"<<endl;        _real += c._real;        _image += c._image;        return *this;}complex operator*(const complex& c){        cout<<"operator*(const complex& c)"<<endl;        return complex(_real*c._real-_image*c._image,c._image*_real+_image*c._real);}complex operator/(const complex& c){        cout<<"operator/(const complex& c)"<<endl;        return complex((_real/c._real+_image*c._image)/(c,_real*c._real+c._image*c._image),(_image*c._real-_real*c._image)/(c,_real*c._real+c._image*c._image));}private:double _real;double _image;};

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.