主要是練習用運算子過載實現複數的一些基本運算,包括:
複數加法(過載的運算子:+、+=、前置++、後置++);
複數減法(過載的運算子:-、-=、前置--、後置--);
複數乘法(過載的運算子:*、*=、);
複數除法(過載的運算子:/、/=、);
**如下:
#includeusing namespace std;class complex
//建構函式
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)//拷貝建構函式complex& complex::operator=(const complex& complex)//賦值操作符過載
return *this;}
ostream& operator<<(ostream& output, const complex& complex)//輸出操作符過載
output << complex._imag << "i)";
return output;
}bool complex::operator==(const complex& complex)
bool complex::operator!=(const complex& complex)
complex complex::operator+(const complex& complex)
complex& complex::operator++()
complex complex::operator++(int)
complex& complex::operator+=(const complex& complex)
complex complex::operator-(const complex& complex)
complex& complex::operator-=(const complex& complex)
complex& complex::operator--()
complex complex::operator--(int)
complex complex::operator*(const complex& complex)
complex& complex::operator*=(const complex& complex)
complex complex::operator/(const complex& complex)
complex& complex::operator/=(const complex& complex)
C 實現複數類
complex類的成員變數有實部與虛部 protected double real double image c 中的complex類的基本函式,包括四個預設成員函式,比較運算子過載函式,賦值運算子過載函式,以及前置 與後置 的過載。四個預設成員函式是 1 建構函式 在定義物件時初始化成員變數,函式...
C 實現複數類
用c 語言實現乙個複數類,包括複數加 減 乘 除 加等 減等 乘等及除等8個運算子的過載。include using namespace std class complex 複數類 類外實現成員函式 complex complex const double real,const double ima...
複數類 一 C 實現
using system using system.collections.generic using system.text namespace csharpalgorithm.algorithm set private double imaginary 0.0 虛部 public double ...