【发布时间】:2019-02-23 03:49:43
【问题描述】:
我得到了一个驱动程序函数,它应该演示涉及复数的运算符重载的结果。在阅读了一段时间的重载后,我设法以一种成功编译的方式编写了代码,但是在此过程中,程序没有输出正确的值。
据我了解,重载本质上就像一个函数。传递对象,然后“函数”可以对其进行算术/任何操作并返回一个新对象。不过,我有点迷失的是,重载如何知道正在传递什么值。例如,在我的例子中,我重载了“+”和“=”运算符,以便以“x = y + z”的形式添加两个复数。当编译器遇到“=”符号时,我假设它只是传递左侧和右侧的任何内容并传递那些?与“+”相同。在这种情况下,它会传递“y”,因为它是左侧的对象,而“z”是因为它是右侧的对象?
这是我当前的“复杂”类,其中包括重载定义。
class Complex {
private:
double realPart;
double imaginaryPart;
public:
// friends
friend ostream & operator<<(ostream &out, const Complex &c);
friend istream & operator>>(istream &in, Complex &c);
// constructors
Complex()
{
realPart = 0;
imaginaryPart = 0;
}
Complex(double real)
{
realPart = real;
imaginaryPart = 0;
}
Complex(double real, double imaginary)
{
realPart = real;
imaginaryPart = imaginary;
}
// end of constructors
// + overloading
Complex operator+(Complex const &c)
{
Complex Add;
Add.realPart = realPart + c.realPart;
Add.imaginaryPart = imaginaryPart + c.imaginaryPart;
return Add;
}
// - overloading
Complex operator-(Complex const &c)
{
Complex Subtract;
Subtract.realPart = realPart - c.realPart;
Subtract.imaginaryPart = imaginaryPart - c.imaginaryPart;
return Subtract;
}
// * overloading
Complex operator*(Complex const &c)
{
Complex Multiply;
Multiply.realPart = (realPart * c.realPart) - (imaginaryPart * c.imaginaryPart);
Multiply.imaginaryPart = (realPart * c.imaginaryPart) - (imaginaryPart * c.realPart);
return Multiply;
}
// = overloading
Complex operator=(Complex const &c)
{
Complex Assignment;
Assignment.realPart = realPart;
Assignment.imaginaryPart = imaginaryPart;
return Assignment;
}
// == overloading
bool operator==(Complex const &c)
{
Complex Compare;
if (Compare.realPart == realPart && Compare.imaginaryPart == imaginaryPart)
{
return true;
}
else
{
return false;
}
}
// != overloading
bool operator!=(Complex const &c)
{
Complex NotEqual;
if (NotEqual.realPart == realPart && NotEqual.imaginaryPart == imaginaryPart)
{
return false;
}
else
{
return true;
}
}
};
// << overloading
ostream& operator<<(ostream& out, const Complex &c)
{
out << c.realPart;
if (c.imaginaryPart >= 0)
{
out << " + " << c.imaginaryPart << "i" << endl;
}
else
{
out << " - " << fabs (c.imaginaryPart) << "i" << endl;
}
return out;
}
// >> overloading
istream& operator>>(istream &in, Complex &c)
{
in >> c.realPart;
in >> c.imaginaryPart;
return in;
}
这是驱动程序:
int main()
{
for (double i = 1; i < 10; ++ i)
{
Complex y{i * 2.7, i + 3.2};
Complex z{i * 6, i + 8.3};
Complex x;
Complex k;
std::cout << "Enter a complex number in the form: (a, b)\n? ";
std::cin >> k; // demonstrating overloaded >>
std::cout << "x: " << x << "\ny: " << y << "\nz: " << z << "\nk: " << k << '\n'; // demonstrating overloaded <<
x = y + z; // demonstrating overloaded + and =
std::cout << "\nx = y + z:\n" << x << " = " << y << " + " << z << '\n';
x = y - z; // demonstrating overloaded - and =
std::cout << "\nx = y - z:\n" << x << " = " << y << " - " << z << '\n';
x = y * z; // demonstrating overloaded * and =
std::cout << "\nx = y * z:\n" << x << " = " << y << " * " << z << "\n\n";
if (x != k)
{ // demonstrating overloaded !=
std::cout << x << " != " << k << '\n';
}
std::cout << '\n';
x = k;
if (x == k)
{
// demonstrating overloaded ==
std::cout << x << " == " << k << '\n';
}
std::cout << std::endl;
}
}
运行时,问题似乎出在对象“x”上。输入“5 2”仍将输出“x: 0 + 0i” 这让我相信问题出在“=”或流运算符的重载上。也就是说,我不太明白为什么什么也没发生。
我认为“=”重载定义的构建方式是否有错误,或者可能是我遗漏了更大的东西?
【问题讨论】:
-
对于
operator=- 分配 - 您实际上忽略了为实例分配 值。赋值以这种方式不与+、-等平行。 -
您的
+、-和*运营商看起来不错。您的operator=应该分配给this以便按预期工作,并且应该按照约定返回对*this的引用。你的operator==和operator!=忽略他们的输入,而是与一个无用的临时对象进行比较。 -
要回答“运算符重载是如何工作的”,您似乎已经大致弄清楚了,但这里有一个很好的彻底阅读:What are the basic rules and idioms for operator overloading?
-
建议 -- 您的
!=应该是一个简单的单行:{ return !(this == c);}。使用已经写好的内容。
标签: c++ overloading operator-keyword