【C++】複數類的實現

來源:互聯網
上載者:User

標籤:

#include<iostream>using namespace std;class Complex{private: double _real; double _image;public: Complex(double real = 2.2,double image=1.1)//建構函式 {  cout<<"建構函式被調用"<<endl;  _real = real;  _image = image; } Complex(const Complex& d)//拷貝建構函式 {  cout<<"拷貝建構函式被調用"<<endl;  this->_real = d._real ;  this->_image = d._image ; } ~Complex() {  cout<<"解構函式被調用"<<endl; } void Display() {  cout<<"Real:"<<_real;  cout<<"   Image:"<<_image<<endl; }public: Complex& operator=(const Complex& d) {  if(this != &d)  {   cout<<"賦值運算子被重載"<<endl;   this->_real = d._real ;   this->_image = d._image ;  }  return *this; } Complex& operator++() {  cout<<"前置++被重載"<<endl;  this->_real++;  return *this; }    Complex operator++(int) {  cout<<"後置++被重載"<<endl;  Complex *tmp = this;  this->_real++;  return *tmp; } Complex& operator--() {  cout<<"前置--被重載"<<endl;  this->_real --;  return *this; } Complex operator--(int) {  cout<<"後置--被重載"<<endl;  Complex *tmp = this;  this->_real --;  return *tmp; } Complex operator+(const Complex& d) {  cout<<"+被重載"<<endl;  Complex tmp ;  tmp._real = this->_real + d._real ;  tmp._image = this->_image + d._image ;  return tmp; } Complex operator-(const Complex& d) {  cout<<"-被重載"<<endl;  Complex tmp;  tmp._real = this->_real - d._real ;  tmp._image = this->_image - d._image;  return tmp; } Complex& operator-=(const Complex& d) {  cout<<"-=被重載"<<endl;  this->_real -= d._real ;  this->_image -= d._image ;  return *this; } Complex& operator+=(const Complex& d) {  cout<<"+=被重載"<<endl;  this->_real +=  d._real ;  this->_image +=  d._image ;  return *this; } Complex operator*(const Complex& d) {  Complex tmp;  tmp._real = this->_real * d._real - this->_image * d._image;  tmp._image = this->_image * d._real + this->_real * d._image;  return tmp; } Complex operator/(const Complex& d) {  Complex tmp;  tmp._real = (this->_real * d._real + this->_image * d._image)/   (d._real * d._real +d._image * d._image);  tmp._image = (this->_image * d._real - this->_real * d._image)/   (d._real * d._real +d._image * d._image);  return tmp; }}; int main(){ Complex d1; d1.Display(); //Complex d2(d1); Complex d2 = d1; d2.Display(); Complex d3(4.4,5.5); d3.Display() ; d3 = d2; d3.Display();    ++d3; d3.Display (); d3++; d3.Display (); --d3; d3.Display (); d3--; d3.Display (); //system(pause); d3 = d3+d1; d3.Display (); d3 = d3-d1; d3.Display (); d3-=d1;    d3.Display (); getchar(); 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.