0

I have an overloaded preincrement operator that is supposed to take a complex number like (3+6i)....which is, in this case, a, and run it through a square function that turns it into (-27+36i). I also have a postincrement operator that does the same thing. The postincrement operator works fine, when I use it inside the main, I get what you would expect (-27+36i) no problem.

However, when I try to use the preincrement operator inside the main, (I'm expecting the same answer: (-27+36i)) instead I'm given the square of (-27+36i) which is (-567-1944i). And I don't know why this is the case or how I would correct for it. I've checked for sequence issues, and I've been able to discern nothing. I'm supposed to use post and pre increment operators to both execute the square function inside the body of the code. Here is the code:

#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");
}
garyoak
  • 41
  • 3

1 Answers1

1

Your postincrement operator is changing the value of a, so when your preincrement operator is called, a is already squared (so it gets squared again). Your postincrement has the same problem; swap those two output lines you'll see.

The result of calling a++ is a.square(a);. This will create a copy of a, and store the square back into a. Then a reference to the modified a is returned as the value of the increment.

Generally, a preincrement operator can return a reference as the new value is in an existing object, but the postincrement should return a new object and not modify the existing one.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56