天天看點

複數類

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>

using namespace std;

class Complex

{

public:

void display( )

/*cout << "real:" << this->_real << endl;

cout << "image:" << this->_image << endl;*/

cout << this->_real << "+" << this->_image << "i" << endl;

}

Complex operator+(const Complex& a)

Complex tem;

tem._real = this->_real + a._real;

tem._image = this->_image + a._image;

return tem;

Complex operator-(const Complex& a)

tem._real = this->_real - a._real;

tem._image = this->_image - a._image;

Complex operator*(const Complex& a)

tem._real = this->_real * a._real;

tem._image = this->_image * a._image;

Complex operator/(const Complex& a)

tem._real = this->_real / a._real;

tem._image = this->_image / a._image;

void operator+=(const Complex& a)

this->_real = this->_real + a._real;

this->_image = this->_image + a._image;

void operator-=(const Complex& a)

this->_real = this->_real - a._real;

this->_image = this->_image - a._image;

void operator*=(const Complex& a)

this->_real = this->_real * a._real;

this->_image = this->_image * a._image;

void operator/=(const Complex& a)

this->_real = this->_real / a._real;

this->_image = this->_image / a._image;

Complex operator++(int)//huozhi

Complex tem(*this);

this->_real += 1;

this->_image += 1;

Complex& operator++()

return *this;

Complex operator--(int)//huozhi

this->_real -= 1;

this->_image -= 1;

Complex& operator--()

bool operator>(const Complex& a)

double m = pow(this->_real, 2) + pow(this->_image, 2);

double n = pow(a._real, 2) + pow(a._image, 2);

return (m > n);

bool operator<(const Complex& a)

return (m < n);

bool operator>=(const Complex& a)

return (m > n || m == n);

bool operator<=(const Complex& a)

return (m < n || m == n);

bool operator==(const Complex& a)

return (m == n);

Complex(double real = 0.0, double image = 0.0)

this->_real = real;

this->_image = image;

Complex(Complex& a)

this->_real = a._real;

this->_image = a._image;

~Complex()

;

Complex* operator=(const Complex& a)

return this;

private:

double _real;

double _image;

};

int main()

Complex first(1.0, 2.0);

first.display();

Complex second(first);

second.display();

Complex three;

//three = second = first;

//second.display();

//three = first + second;

first.operator++(1);

system("pause");

return 0;

本文轉自 ye小灰灰  51CTO部落格,原文連結:http://blog.51cto.com/10704527/1719280,如需轉載請自行聯系原作者

繼續閱讀