主要是练习用运算符重载实现复数的一些基本运算,包括:
复数加法(重载的运算符:+、+=、前置++、后置++);
复数减法(重载的运算符:-、-=、前置--、后置--);
复数乘法(重载的运算符:*、*=、);
复数除法(重载的运算符:/、/=、);
代码如下:
#include<iostream> using namespace std; class Complex { public: Complex(float real = 1, float imag = 1):_real(real), _imag(imag){}//构造函数 Complex(const Complex& complex);//拷贝构造函数 ~Complex(){};//析构函数 Complex& operator=(const Complex& complex);//赋值操作符重载 bool operator==(const Complex& complex); bool operator!=(const Complex& complex); Complex operator+(const Complex& complex); Complex& operator++();//前置++ Complex operator++(int);//后置++ Complex& operator+=(const Complex& complex); Complex operator-(const Complex& complex); Complex& operator-=(const Complex& complex); Complex& operator--();//前置-- Complex operator--(int);//后置-- Complex operator*(const Complex& complex); Complex& operator*=(const Complex& complex); Complex operator/(const Complex& complex); Complex& operator/=(const Complex& complex); friend ostream& operator<<(ostream& output, const Complex& complex);//输出操作符重载 private: float _real; float _imag; };
各函数的实现:
Complex::Complex(const Complex& complex)//拷贝构造函数 { _real = complex._real; _imag = complex._imag; } Complex& Complex::operator=(const Complex& complex)//赋值操作符重载 { cout << "赋值操作符重载" << endl; if (this != &complex) { _real = complex._real; _imag = complex._imag; } return *this; } ostream& operator<<(ostream& output, const Complex& complex)//输出操作符重载 { output << "(" << complex._real; if (complex._imag >= 0) { output << "+"; } output << complex._imag << "i)"; return output; } bool Complex::operator==(const Complex& complex) { return (_real == complex._real) && (_imag == complex._imag); } bool Complex::operator!=(const Complex& complex) { return !(*this==complex); } Complex Complex::operator+(const Complex& complex) { Complex c(*this); c._real +=complex._real; c._imag +=complex._imag; return c; } Complex& Complex::operator++() { ++_real; return *this; } Complex Complex::operator++(int) { Complex c; c._real=_real++; return c; } Complex& Complex::operator+=(const Complex& complex) { _real += complex._real; _imag += complex._imag; return *this; } Complex Complex::operator-(const Complex& complex) { return Complex(_real - complex._real, _imag - complex._imag); } Complex& Complex::operator-=(const Complex& complex) { _real -= complex._real; _imag -= complex._imag; return *this; } Complex& Complex::operator--() { --_real; return *this; } Complex Complex::operator--(int) { Complex c; c._real=_real--; return c; } Complex Complex::operator*(const Complex& complex) { Complex c; c._real = _real*complex._real - _imag*complex._imag; c._imag = _real*complex._imag + _imag*complex._real; return c; } Complex& Complex::operator*=(const Complex& complex) { float real = _real; _real = _real*complex._real - _imag*complex._imag; _imag = _imag*complex._real + real*complex._imag; return *this; } Complex Complex::operator/(const Complex& complex) { return Complex((_real*complex._real + _imag*complex._imag) / (complex._real*complex._real + complex._imag*complex._imag), (_imag*complex._real - _real*complex._imag)/ (complex._real*complex._real + complex._imag*complex._imag)); /*Complex c; c._real = (_real*complex._real + _imag*complex._imag)*1.0 / (complex._real*complex._real + complex._imag*complex._imag); c._imag = (_imag*complex._real - _real*complex._imag)*1.0 / (complex._real*complex._real + complex._imag*complex._imag); return c;*/ } Complex& Complex::operator/=(const Complex& complex) { float real = _real; _real = (_real*complex._real + _imag*complex._imag)/ (complex._real*complex._real + complex._imag*complex._imag); _imag = (_imag*complex._real - real*complex._imag) / (complex._real*complex._real + complex._imag*complex._imag); return *this; }