?? complex.h
字號:
//自定義復數類的頭文件(Complex.h)
#ifndef __COMPLEX_H
#define __COMPLEX_H
#include <iostream>
#include <cmath>
using namespace std;
class Divbyzero{};
class Complex; //前向引用聲明
//函數原型聲明
istream &operator>>(istream &is,Complex &c);
ostream &operator<<(ostream &out,const Complex &c);
Complex operator+(const Complex &c1,const Complex &c2);
Complex operator-(const Complex &c1,const Complex &c2);
Complex operator*(const Complex &c1,const Complex &c2);
Complex operator/(const Complex &c1,const Complex &c2);
bool operator==(const Complex &c1,const Complex &c2);
bool operator!=(const Complex &c1,const Complex &c2);
bool operator>(const Complex &c1,const Complex &c2);
bool operator>=(const Complex &c1,const Complex &c2);
bool operator<(const Complex &c1,const Complex &c2);
bool operator<=(const Complex &c1,const Complex &c2);
//以上幾行只有在VC++6.0 SP5以前的版本才需要
class Complex
{
private:
double rpart; //實部
double ipart; //虛部
double abs() const; //計算負數的模
double norm() const; //計算負數模的平方
public:
//構造函數
Complex(); //缺省構造函數
Complex(const Complex &c); //拷貝構造函數
Complex(const double &r,const double &i); //初始化構造函數
Complex(const double &r); //(類型)轉換構造函數
void setValue(const double &r,const double &i); //設置復數值
//重載運算符
Complex &operator=(const Complex &c); //賦值=
Complex operator-(); //負號-
//重載算術運算符
friend Complex operator+(const Complex &c1,const Complex &c2);
friend Complex operator-(const Complex &c1,const Complex &c2);
friend Complex operator*(const Complex &c1,const Complex &c2);
friend Complex operator/(const Complex &c1,const Complex &c2);
Complex operator+=(const Complex &c);
Complex operator-=(const Complex &c);
Complex operator*=(const Complex &c);
Complex operator/=(const Complex &c);
//重載關系(比較)運算符
friend bool operator==(const Complex &c1,const Complex &c2);
friend bool operator!=(const Complex &c1,const Complex &c2);
friend bool operator>(const Complex &c1,const Complex &c2);
friend bool operator>=(const Complex &c1,const Complex &c2);
friend bool operator<(const Complex &c1,const Complex &c2);
friend bool operator<=(const Complex &c1,const Complex &c2);
//重載抽取運算符>>和插入運算符<<,不能重載為類的成員函數
friend istream &operator>>(istream &is,Complex &c);
friend ostream &operator<<(ostream &out,const Complex &c);
};
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -