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

问题描述:

编写一个Complex类,需要完成的运算符重载有:+ :重载+,用来完成两个复数的加法
编写一个Complex类,需要完成的运算符重载有:
(1) + :重载+,用来完成两个复数的加法;
(2) - :重载 - ,用来完成两个复数的减法;
(3) *:重载*,用来完成两个复数的乘法;
(4)
1个回答 分类:综合 2014-10-11

问题解答:

我来补答
#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
 
 
展开全文阅读
剩余:2000