手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

C++复数类的运算符重载

[日期:2016-01-29] 来源:Linux社区  作者:pawnsir [字体: ]

在这里实现复数类运算符的重载,网上比较少的是对cout cin的重载,以及数学运算*,/,一下是我实现的声明:

Complex.h:
#ifndef __COMPLEX__
#define __COMPLEX__
#include <iostream>
using namespace std;
class Complex
{
public:
friend ostream & operator << (ostream& out, const Complex& num)//重载cout,cin,课一直接调
{                                                              //用>>、<<运算符进行输入输
if (num._imgic<0.0)                                            //出,我把定义友元的重载写
{                                                              //到了头文件里
out << num._real << " - " << -(num._imgic) << "i";
}
else
{
out << num._real << " + " << num._imgic << "i";
}
return out;
}
friend istream & operator>> (istream& in, Complex& num)
{
in >> num._real;
in >> num._imgic;
return in;
}
Complex(double real = 0, double imgic = 0)
:_real(real)
, _imgic(imgic)
{
}
~Complex()
{
}
bool operator >(const Complex & num);
bool operator <(const Complex & num);
bool operator ==(const Complex & num);
bool operator >=(const Complex & num);
bool operator <=(const Complex & num);
bool operator !=(const Complex & num);
Complex operator + (const Complex & num);//算数运算符的重载
Complex operator - (const Complex & num);
Complex operator ++ ();//前置++、--,后置++、--在这里我全部实现为+、-实部
Complex operator ++ (int);//对虚部不做运算
Complex operator -- ();
Complex operator -- (int);
Complex operator += (const Complex & num);
Complex operator -= (const Complex & num);
Complex operator * (const Complex & num);//乘、除运算的实现
Complex operator *= (const Complex & num);//若实现 / 运算功能,我们必须先实现~(共轭)
Complex operator /  (Complex & num);
Complex operator /= (Complex & num);
Complex operator ~ ();//共轭运算符的实现
private:
double _real;
double _imgic;
};
#endif

实现代码如下,Complex.cpp:

#include "Complex.h"
bool Complex:: operator >(const Complex & num)
{
if ((_real*_real + _imgic*_imgic) >
(num._real*num._real + num._imgic*num._imgic))
return true;
return false;
}
bool Complex:: operator <(const Complex & num)
{
return !(*this >= num);
}
bool Complex:: operator ==(const Complex & num)
{
if ((_real == num._real) &&
(_imgic == num._imgic))
return true;
return false;
}
bool Complex:: operator != (const Complex & num)
{
return !(*this == num);
}
bool Complex:: operator >=(const Complex & num)
{
return (*this > num) || (*this == num);
}
bool Complex:: operator <=(const Complex & num)
{
return !(*this > num);
}
Complex Complex:: operator + (const Complex & num)
{
return Complex(_real + num._real, _imgic + num._imgic);
}
Complex Complex:: operator - (const Complex & num)
{
return Complex(_real - num._real, _imgic - num._imgic);
}
Complex Complex:: operator ++ ()
{
_real++;
return *this;
}
Complex Complex:: operator ++ (int)
{
Complex ret = *this;
_real++;
return *this;
}
Complex Complex:: operator -- ()
{
_real--;
return *this;
}
Complex Complex:: operator -- (int)
{
Complex ret = *this;
_real--;
return *this;
}
Complex Complex:: operator += (const Complex & num)
{
_real += num._real;
_imgic += num._imgic;
return *this;
}
Complex Complex:: operator -= (const Complex & num)
{
_real -= num._real;
_imgic -= num._imgic;
return *this;
}
Complex Complex:: operator * (const Complex & num)
{
return Complex((_real*num._real - _imgic*num._imgic),
(_real*num._imgic + (num._imgic*_real)));
}
Complex Complex:: operator *= (const Complex & num)
{
return*this = *this*num;
}
Complex Complex:: operator / (Complex & num)
{
return Complex(((*this*(~num))._real) / ((num*(~num))._real),
((*this*(~num))._imgic) / ((num*(~num))._real));
}
Complex Complex:: operator /= (Complex & num)
{
Complex ret = *this / num;
return ret;
}
Complex Complex:: operator ~ ()
{
return Complex(_real, -_imgic);
}

如有不足希望指正,有疑惑希望留言一起探讨

本文永久更新链接地址http://www.linuxidc.com/Linux/2016-01/127980.htm

linux
相关资讯       C++复数类  C++运算符重载 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款