这是输出:
First Complex Number:
Enter real part of complex number: 3
Enter imaginary part of complex number: 6
Second Complex Number:
Enter real part of complex number: 5
Enter imaginary part of complex number: -5
a == (-27.00+36.00i)
b == (5.00-5.00i)
a+b == (-22.00+31.00i)
a-b == (-32.00+41.00i)
a*b == (45.00+315.00i)
a*a == (-567.00-1944.00i)
b*b == (0.00-50.00i)
a*a (using postincrement) ==(-27.00+36.00i)
如您所见,并非所有涉及a
的内容都是错误的,因为它将a
(复数)的平方作为a
.因此,虽然“a*a (using postincrement) == (-27.00+36.00i)
”的答案是正确的答案......它说“a == ( -27.00+36.00i)
"是不正确的,因为它应该是 a==(3+6i)
。我相信错误在于我的代码的重载和友元方面,但是我不确定如何修复它,因为我没有收到任何错误...这是我代码中的逻辑问题。
这是我的代码:
#include<iostream>
#include<iomanip>
using namespace std;
class ComplexNum
{
public:
ComplexNum(float = 0.0, float = 0.0); //default constructor that uses default arg. in case no init. are in main
ComplexNum& getComplexNum(); //get real and imaginary numbers from keyboard
ComplexNum& sum(ComplexNum a, ComplexNum b); //method to add two ComplexNum numbers together
ComplexNum& diff(ComplexNum a, ComplexNum b); //method to find the difference of two complex numbers
ComplexNum& prod(ComplexNum a, ComplexNum b); //method to find the product of two complex numbers
ComplexNum& square(ComplexNum a); //method to find square using pre/post increment operators
//overloaded operators
ComplexNum& operator = (const ComplexNum& that) = default;
ComplexNum& operator += (const ComplexNum& that) { return sum(*this, that); }
ComplexNum& operator -= (const ComplexNum& that) { return diff(*this, that); }
ComplexNum& operator *= (const ComplexNum& that) { return prod(*this, that); }
ComplexNum& operator ++() { return square(*this); } //called for ++num
ComplexNum& operator ++(int) { return square(*this); } //called for num++
ostream& print(ostream& stm = cout) const;
private:
float real; //float data member for real number (to be entered in by user)
float imaginary; //float data member for imaginary number (to be entered in by user)
//non-member overloaded operators
//a is passed by value
friend ComplexNum operator+ (ComplexNum a, const ComplexNum& b) { return a += b; }
friend ComplexNum operator- (ComplexNum a, const ComplexNum& b) { return a -= b; }
friend ComplexNum operator* (ComplexNum a, const ComplexNum& b) { return a *= b; }
friend ComplexNum operator++(ComplexNum a) { return a++; }
friend ostream& operator<< (ostream& stm, const ComplexNum& c) { return c.print(stm); }
};
ComplexNum::ComplexNum(float a, float b)
{
real = a;
imaginary = b;
}
ComplexNum& ComplexNum::getComplexNum()
{
ComplexNum keyboard;
cout << "Enter real part of complex number: ";
cin >> real;
cout << "Enter imaginary part of complex number: ";
cin >> imaginary;
return keyboard;
}
ComplexNum& ComplexNum::square(ComplexNum a)
{
this->real = (a.real * a.real) - (a.imaginary * a.imaginary);
this->imaginary = (2 * (a.real * a.imaginary));
return *this;
}
ComplexNum& ComplexNum::sum(ComplexNum a, ComplexNum b)
{
this->real = a.real + b.real;
this->imaginary = a.imaginary + b.imaginary;
return *this;
}
ComplexNum& ComplexNum::diff(ComplexNum a, ComplexNum b)
{
this->real = a.real - b.real;
this->imaginary = a.imaginary - b.imaginary;
return *this;
}
ComplexNum& ComplexNum::prod(ComplexNum a, ComplexNum b)
{
this->real = (a.real * b.real) - (a.imaginary * b.imaginary);
this->imaginary = (a.real * b.imaginary) + (b.real * a.imaginary);
return *this;
}
ostream& ComplexNum::print(ostream& stm) const
{
return stm << "(" << noshowpos << real << showpos << imaginary << "i)";
}
int main()
{
ComplexNum a, b;
cout << "First Complex Number:" << endl;
a.getComplexNum();
cout << endl;
cout << "Second Complex Number:" << endl;
b.getComplexNum();
cout << endl;
cout << fixed << setprecision(2)
<< "a == " << a << '\n'
<< "b == " << b << '\n'
<< "a+b == " << a + b << '\n'
<< "a-b == " << a - b << '\n'
<< "a*b == " << a*b << '\n'
<< "a*a == " << a*a << '\n'
<< "b*b == " << b*b << '\n'
<< "a*a (using postincrement) ==" << a++ << '\n';
cout << endl;
system("PAUSE");
}
最佳答案
这不是关于运算符重载,而是关于 order of evaluation .
在声明中:
cout << fixed << setprecision(2)
<< "a == " << a << '\n'
<< "b == " << b << '\n'
<< "a+b == " << a + b << '\n'
<< "a-b == " << a - b << '\n'
<< "a*b == " << a*b << '\n'
<< "a*a == " << a*a << '\n'
<< "b*b == " << b*b << '\n'
<< "a*a (using postincrement) ==" << a++ << '\n';
参数将按照您的预期从左到右打印,但计算每个参数的各个步骤不必按该顺序完成。
编译器可以按任何顺序计算每个子表达式的值,只要该值在需要打印时“准备好”即可。所以它可以做这样的事情:
auto&& temp1 = a + b;
auto&& temp2 = a - b;
auto&& temp3 = a*b;
auto&& temp4 = a*a;
auto&& temp5 = b*b;
auto&& temp6 = a++;
cout << fixed << setprecision(2)
<< "a == " << a << '\n'
<< "b == " << b << '\n'
<< "a+b == " << temp1 << '\n'
<< "a-b == " << temp2 << '\n'
<< "a*b == " << temp3 << '\n'
<< "a*a == " << temp4 << '\n'
<< "b*b == " << temp5 << '\n'
<< "a*a (using postincrement) ==" << temp6 << '\n';
如果发生这种情况,您将获得预期的行为,因为子表达式将按从左到右的顺序求值。但是当前的 C++ 标准 (C++14) 允许编译器重新排序评估,如果它需要更少的堆栈空间和更少的寄存器,这可能允许它更好地优化代码。该语句的另一个有效执行是:
auto&& temp1 = a++;
auto&& temp2 = b*b;
auto&& temp3 = a*a;
auto&& temp4 = a*b;
auto&& temp5 = a - b;
auto&& temp6 = a + b;
cout << fixed << setprecision(2)
<< "a == " << a << '\n'
<< "b == " << b << '\n'
<< "a+b == " << temp6 << '\n'
<< "a-b == " << temp5 << '\n'
<< "a*b == " << temp4 << '\n'
<< "a*a == " << temp3 << '\n'
<< "b*b == " << temp2 << '\n'
<< "a*a (using postincrement) ==" << temp1 << '\n';
这次你可以看到a++
操作先发生,所以后面的计算将在发生后进行。
编译器还可以选择子表达式求值的任何其他顺序,例如它可以评估 a*a
然后 ++a
然后 a-b
等(但在实践中最常见的顺序是从左到右和从右到左)
顺便说一句,您的代码还有另外两个严重的问题:
此函数返回对局部变量的引用:
ComplexNum& ComplexNum::getComplexNum()
{
ComplexNum keyboard;
// ...
return keyboard;
}
该局部变量在函数返回后不再存在,因此任何使用该引用的尝试都会访问一个不存在的对象。 永远不要这样做!您的编译器应该警告您此处存在问题,启用编译器的警告并且不要忽略它们!
其次,真的重载像++
这样的运算符来表示完全不同的东西,比如将一个对象乘以自身,这不是一个好主意。只有当运算符对您要执行的操作有意义时,才应使用运算符重载。递增不是平方,所以这是一个糟糕的选择。我会为此定义一个普通的(非运算符)函数。
关于c++ - 重载后递增运算符的复数平方值,无需运算符实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42193794/