У меня есть перегруженный оператор preincrement, который должен принимать сложное число вроде (3 + 6i) .... которое в данном случае a
и запускать его через квадратную функцию, которая превращает ее в (-27 + 36i). У меня также есть оператор postincrement, который делает то же самое. Оператор postincrement отлично работает, когда я использую его внутри основного, я получаю то, чего вы ожидаете (-27 + 36i), без проблем.Перегруженный оператор preincrement, использующий результат переменной внутри перегруженного оператора postincrement
Однако, когда я пытаюсь использовать оператор preincrement внутри основного, (я ожидаю тот же ответ: (-27 + 36i)), вместо этого мне дают квадрат (-27 + 36i), который (-567-1944i). И я не знаю, почему это так или как я буду исправлять это. Я проверил проблемы с последовательностью, и я ничего не смог различить. Я должен использовать операторы post и pre increment для выполнения квадратной функции внутри тела кода. Вот код:
#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++ (const ComplexNum& a) { return ++a; } //friend for ++num
//friend ComplexNum& operator++ (const ComplexNum& a, int) { return a++; } //friend for num++
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';
cout << "a*a (using postincrement) == " << a++ << '\n'; //works fine
cout << "a*a (using preincrement) == " << ++a << '\n'; //squares the square, instead of giving me the same answer as a++
cout << endl;
system("PAUSE");
}