作业帮 > 综合 > 作业

编写一个Complex类,需要完成的运算符重载有:+ :重载+,用来完成两个复数的加法

来源:学生作业帮 编辑:灵鹊做题网作业帮 分类:综合作业 时间:2024/05/01 08:59:56
编写一个Complex类,需要完成的运算符重载有:+ :重载+,用来完成两个复数的加法
编写一个Complex类,需要完成的运算符重载有:
(1) + :重载+,用来完成两个复数的加法;
(2) - :重载 - ,用来完成两个复数的减法;
(3) *:重载*,用来完成两个复数的乘法;
(4)
编写一个Complex类,需要完成的运算符重载有:+ :重载+,用来完成两个复数的加法
#include
#include
class Complex
{
public:
Complex(float a,float b)
:m_real(a)
,m_imaginary(b)
{
}
Complex()
:m_real(0)
,m_imaginary(0)
{
}
const Complex operator+(const Complex& other)
{
Complex c(m_real+other.m_real,m_imaginary+other.m_imaginary);
return c;
}
const Complex operator-(const Complex& other)
{
Complex c(m_real-other.m_real,m_imaginary-other.m_imaginary);
return c;
}
const Complex operator*(const Complex& other)
{
Complex c(m_real*other.m_real-m_imaginary*other.m_imaginary,m_imaginary*other.m_real+m_real*other.m_imaginary);
return c;
}
float m_real,m_imaginary;
};
std::ostream& operato