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; }
相关文章推荐
- C++基础编程----2.2标准库类型string
- c++对象适配器
- 项目32.4 输出小星星
- c++面试题
- C++ "#"的作用和用法
- c++类适配器
- 项目32.3 输出小星星
- C++运算符重载
- 在VS2015中配置和测试DCMTK遇到的问题
- C语言 switch和if..else的区别 以及枚举的作用
- C++ 中STL map 详解
- C++中“浅拷贝”与“深拷贝”
- C++primer第五版笔记-第六章函数
- C/C++中四种不同的类型转换方式
- 洛谷1071 潜伏者
- 项目32.2 输出小星星
- 项目32.1输出小星星
- C++学习笔记之多态(虚函数)
- c++遍历文件夹中的图片(或文件)
- C语言 scanf()和gets()函数的区别