2017-02-13 9 views
0

(3 + 6i)...のような複素数をとるオーバーロードされたプリインクリメント演算子があります。この場合、a、それを(-27 + 36i)に変える正方形の関数を通します。私は同じことをするポストインクリメント演算子も持っています。ポストインクリメント演算子はうまく動作します。メインの中で使用すると、(-27 + 36i)何の問題もありません。オーバーロードされたポストインクリメント演算子内の変数の結果を使用してオーバーロードされたプリインクリメント演算子

しかし、私はpreincrement演算子をメインの内部で使用しようとすると(私は同じ答えを期待しています:(-27 + 36i))代わりに(-27 + 36i) (-567-1944i)です。そして、私はなぜこれが正しいのか、それをどう修正するのか分かりません。私はシーケンスの問題をチェックしましたが、私は何も見分けることができませんでした。私はポストとプリインクリメント演算子を使用して、コード本体の内部で二乗関数を実行することになっています。ここでは、コードは次のとおりです。

#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"); 
} 

答えて

1

あなたのポストインクリメント演算子はaの値を変更しているので、あなたの前置インクリメント演算子が呼び出されたときに、aはすでに乗される(それが再び乗ます)。あなたのポストインクリメントにも同じ問題があります。表示される2つの出力行を入れ替えます。

a++を呼び出した結果はa.square(a);です。これにより、aのコピーが作成され、aに再び格納されます。次に、変更されたaへの参照がインクリメントの値として返されます。

一般的に、プリインクリメント演算子は、新しい値が既存のオブジェクトにあるため参照を返すことができますが、ポストインクリメントは新しいオブジェクトを返し、既存のオブジェクトを変更しません。

関連する問題