#include<iostream>
using namespace std;
#include<math.h>
class complex {
float real, image;
public:
complex(float r = 0, float i = 0)
{
real = r; image = i;
}
complex & operator+=(complex b)
{
real += b.real;
image += b.image;
return *this;
}
complex operator*=(complex b)
{
real += b.real;
image += b.image;
return *this;
}
void display()
{
cout << real << (image >= 0 ? '+' : '-') << "j*" << fabs(image) << endl;
}
};
int main() { return 0; }你能告诉我复形operator*=(配合物b)和复形&operator+=(配合物b)的区别吗?
非常感谢你!
发布于 2015-11-09 15:43:43
operator*=的实现是不正确的。它所做的事情与operator+=相同。此外,它返回一个副本而不是一个引用。
更好的执行办法是:
complex& operator*=(complex b)
{
double tempReal = real*b.real - image*b.image;
double tempImage = real*b.image + image*b.real;
real = tempReal;
image = tempImage;
return *this;
}https://stackoverflow.com/questions/33612541
复制相似问题