我刚刚学习了运算符重载,我想尝试制作一个表示复数的对象。我写了以下内容:
#include <iostream>
using namespace std;
class complexNumber
{
public:
double re, im;
complexNumber(){}
complexNumber(double a, double b)
{
re = a;
im = b;
}
complexNumber operator+ (complexNumber b)
{
complexNumber c;
c.re = re + b.re;
c.im = im + b.im;
}
complexNumber operator- (complexNumber b)
{
complexNumber c;
c.re = re - b.re;
c.im = im - b.im;
}
};
int main()
{
complexNumber a(1,2);
complexNumber b(4,6);
complexNumber c;
complexNumber d;
c = a + b;
d = a - b;
cout << c.re << " + " << c.im << "i" << endl;
cout << d.re << " + " << d.im << "i" << endl;
}
我得到了结果:
2.44156e+253 + 6.95225e-308i
2.44156e+253 + 6.95225e-308i
关于我在这里出错的任何想法。提前致谢。