我在做一个练习,关于运算符重载的,写了一个重载+ 运算符的小程序,目的是输出K的实部和虚部,但无法运行,求前辈指导!谢谢大家。
代码如下:
#include "iostream"
using namespace std;
class Complex
{
private:
double real;
double image;
public:
Complex(double r = 0.0, double i = 0.0);
Complex operator + (const Complex &);
double getComplexReal();
double getComplexImage();
};
double Complex::getComplexReal()
{
return real;
}
double Complex::getComplexImage()
{
return image;
}
Complex Complex::operator+(const Complex & operand2)
{
return Complex(real + operand2.real, image + operand2.image);
}
int main()
{
Complex m(1.0,2.0),n(2.0,3.0),k;
k = m + n;
cout<<"K's real is:"<<k.getComplexReal()<<". and image is:"<<k.getComplexImage()<<endl;
}