简体   繁体   中英

Overloading * operator for complex numbers (C++)

So I'm being asked to overload operators to implement basic arithmetic for complex numbers. I've gotten +,-, and = working but I can't seem to get * working even though I think I have the right logic.

What is wrong with my code?

#ifndef COMPLEX_HPP
#define COMPLEX_HPP
#include <string>

class Complex
{
    public:
        Complex(double = 0.0, double = 0.0); // default constructor
        Complex add(const Complex&) const;            // function add
        Complex subtract(const Complex&) const; // function subtract
        Complex multiply(const Complex&) const; // function multiply
        std::string toString() const;  // return string representation
        void setComplexNumber(double, double); // set complex number

        void operator=(const Complex& obj)
        {
            (*this).realPart = obj.realPart;
            (*this).imaginaryPart = obj.imaginaryPart;
        }

        Complex operator+(const Complex& obj)
        {
            Complex tmp_obj = *this;
            tmp_obj.realPart = tmp_obj.realPart + obj.realPart;
            tmp_obj.imaginaryPart = tmp_obj.imaginaryPart + obj.imaginaryPart;
            return tmp_obj;
        }

        Complex operator-(const Complex& obj)
        {
            Complex tmp_obj = *this;
            tmp_obj.realPart = tmp_obj.realPart - obj.realPart;
            tmp_obj.imaginaryPart = tmp_obj.imaginaryPart - obj.imaginaryPart;
            return tmp_obj;
        }

        Complex operator*(const Complex&obj)
        {
            Complex tmpObj = *this;
            tmpObj.realPart = (tmpObj.realPart * obj.realPart) - (tmpObj.imaginaryPart * obj.imaginaryPart);
            tmpObj.imaginaryPart = (tmpObj.realPart * obj.imaginaryPart) + (tmpObj.imaginaryPart * obj.realPart);
            return tmpObj;

        }


    private:
        double realPart;
        double imaginaryPart;
};

Once you start overwriting tmpObj (specifically, tmpObj.realPart ), you've lost the original values. So don't read from tmpObj , but from *this :

tmpObj.realPart = realPart * obj.realPart - imaginaryPart * obj.imaginaryPart;
//                ^^^^^^^^                  ^^^^^^^^^^^^^
tmpObj.imaginaryPart = realPart * obj.imaginaryPart + imaginaryPart * obj.realPart;
//                     ^^^^^^^^                       ^^^^^^^^^^^^^

Your implementation of the operator*() is essentially.

Complex operator*(const Complex&obj)
{
    Complex tmpObj = *this;
    tmpObj.realPart = (tmpObj.realPart * obj.realPart) - tmpObj.imaginaryPart * obj.imaginaryPart);
    tmpObj.imaginaryPart = (tmpObj.realPart * obj.imaginaryPart) + (tmpObj.imaginaryPart * obj.realPart);
    return tmpObj;
}

The problem is that the first statement modifies tmpObj.realPart , and the second statement proceeds as if tmpObj.realPart has not been modified.

The fix is simple : don't use tmpObj on the right hand side of the assignments.

Complex operator*(const Complex&obj)
{
    Complex tmpObj;   //   your default constructor initialises to (0.0,0.0)
    tmpObj.realPart = (realPart * obj.realPart) - (imaginaryPart * obj.imaginaryPart);
    tmpObj.imaginaryPart = (realPart * obj.imaginaryPart) + (imaginaryPart * obj.realPart);
    return tmpObj;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM