您的位置:首页 > 编程语言 > C语言/C++

C++实现复数类(重载操作符实现复数对应的操作)

2015-10-03 17:12 477 查看
下面的代码是复数类的实现:

#ifndef COMPLEX_H
#define COMPLEX_H
//-------------------------------------------------------------------------------------------------
//这是一个复数类,支持复数加、减、乘操作,并支持对应的复合赋值操作。还支持复数数学意义上的相等操作
//有对应的函数获取复数对象的实部或者虚部。重载了<<操作符,直接以数学形式输出复数,如a+bi;

#include<iostream>
using namespace std;
template<class T> class Complex;//:因为重载+算术运算符时要用到Complex类模板
//因此要先对这个模板类进行声明

template <class T>
Complex<T> operator* (const Complex<T> &a, const Complex<T> &b);
template <class T>
Complex<T> operator+(const Complex<T> &a, const Complex<T>& b);//类模板中要把重载+运算符声明为友元,
//且要把友元限制为特定实例化时,必须在进行友元声明之前声明对应的类或者函数为为模板类或者函数。
//如果没有事先告诉编译器该友元是一个模板,则编译器将认为该友元是一个普通的非模板类或非模板函数
//强行定义为类的话会出现错误,因为没有把对应的类或者函数声明为模板类或函数。

//这并没有一定要如上所述要先进行声明,这是为什么。上面一段话出自:《C++ primer》 4th page 553.
//在实现的过程中,+、-运算符重载的时候不先声明为模板函数不会有问题,但是*运算符的时候不先声明就出现问题了。

template <class T>
Complex<T> operator-(const Complex<T> &a, const Complex<T>& b);
template <class T>
ostream& operator<<(ostream &os, const Complex<T>& q);
template<class T>
bool operator== (const Complex<T> &a, const Complex<T> &b);

template <class T>
class Complex{
private:
T real{}, virt{};
public:
T GetReal(){//获得实部
return real;
}
T GetVirtual(){//获得虚部
return virt;
}

Complex(T a = 0, T b = 0) :real(a), virt(b){}
Complex(Complex &a){
real = a.real;
virt = a.virt;
}
Complex& operator=(const Complex& a){
real = a.real;
virt = a.virt;
return *this;
}
Complex& operator+=(const Complex& a);
Complex& operator*=(const Complex& a);
Complex& operator-=(const Complex& a);
friend  Complex<T> operator+<T>(const Complex<T> &a, const Complex<T> &b);
friend ostream& operator<< <T>(ostream &os, const Complex<T> &q);
friend Complex<T> operator- <T>(const Complex<T> &a, const Complex<T> &b);
friend Complex<T> operator* <T>(const Complex<T> &a, const Complex<T> &b);
//Complex operator +( const Complex &b);
friend bool operator== <T>(const Complex<T> &a, const Complex<T> &b);

};
template<class T>
Complex<T>& Complex<T>::operator +=(const Complex<T>& a){
real +=a.real;
virt += a.virt;
return *this;
}

template<class T>
Complex<T>& Complex<T>::operator -=(const Complex<T>& a){
real -= a.real;
virt -= a.virt;
return *this;
}
template<class T>
Complex<T>& Complex<T>::operator *=(const Complex<T>& a){
T re = real, vi = virt;
real = a.real *re-a.virt*vi;
virt = a.virt*re + vi*a.real;
return *this;
}

template <class T>
ostream& operator<<(ostream &os, const Complex<T> &q){
if (q.virt > 0){
if (q.real != 0)
os << q.real << "+" << q.virt << "i";
else
os << q.virt << "i";
}
else if (q.virt < 0){
if (abs(q.real) >= 0.000001)
os << q.real << q.virt << "i";
else
os << q.virt << "i";
}
else{
os << q.real;
}

return os;
}

template <class T>
Complex<T> operator+(const Complex<T> &a, const Complex<T> &b){
Complex<T> s;
s.real = a.real + b.real;
s.virt = a.virt + b.virt;
return s;
}
template <class T>
Complex<T> operator -(const Complex<T> &a, const Complex<T> &b){
Complex<T> s;
s.real = a.real - b.real;
s.virt = a.virt - b.virt;
return s;
}
template <class T>
Complex<T> operator* (const Complex<T> &a, const Complex<T> &b){
Complex<T> s;
s.real = a.real *b.real-a.virt*b.virt;
s.virt = a.virt*b.real + b.virt*a.real;
return s;
}
template<class T>
bool operator== <T>(const Complex<T> &a, const Complex<T> &b){
return a.real == b.real && a.virt == b.virt;

}

#endif


验证上述复数类的功能的主函数:

#include"Complex.h"
#include<iostream>
using namespace std;
int main(){
Complex<int> c(11, 2);
cout << "c's real number:" << c.GetReal() << endl;
cout << "c's virtual number:" << c.GetVirtual() << endl;
Complex<int> a(c);
cout << "a's real number:" << a.GetReal() << endl;
cout << "a's virtual number:" << a.GetVirtual() << endl;
Complex<int> b (2,4);
a = a + c;
cout << a;
a += c;
cout << endl;
cout << a << endl;
b = b- c;
cout << b << endl;
b -= a;
cout << b << endl;
b = a*c;
cout << b<<endl;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: