The value of the overloaded postincreasing operator of the square number of a complex number without an instance of the operator

Here is the result:

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)

As you can see, not everything connected with ais wrong, because it takes a square a(complex number) as a. So, although the answer to " a*a (using postincrement) == (-27.00+36.00i)is the correct answer ... the part where" a == (-27.00+36.00i)"is indicated is incorrect, as it should be a==(3+6i). I believe that the error lies in overloads and relationships with each other my code, but I'm not sure how it is fix since they don't give me any errors ... this is a logic issue in my code.

Here is my 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++(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");
}
+4
source share
1

, .

:

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';

, , , . ++- (++ 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; 
}

, , . !. , , !

-, ++, , , . , , . , . () .

+1

Source: https://habr.com/ru/post/1669530/


All Articles