这是输出:
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(使用后增量)==( – 27.00 36.00i)是正确的答案…它所说的”a ==( – 27.00 36.00i)“的部分是不正确的,因为它应该是==(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;
}
函数返回后,该局部变量不再存在,因此任何使用该引用的尝试都将访问不存在的对象.永远不要这样做!你的编译器应该警告你这里有问题,启用编译器的警告,不要忽略它们!
其次,重载运算符是一个非常糟糕的主意,就像意味着完全不同的东西一样,就像将对象本身相乘一样.当运算符对您要执行的操作有意义时,您应该只使用操作符重载.增量不是平方,所以这是一个糟糕的选择.我只想为此定义一个普通(非运算符)函数.