The conjugate function for a complex number

I am trying to create a function that matches a complex number, for example, A (2, 3) turns into (2, -3) by typing ~ A, I did a little code below, but I think it’s wrong, I hope you can me to help. I quoted the part that I did wrong in the code below.

#include <iostream>
    using namespace std;
    class Complex
    {
    private:
        double real;
        double imaginenary;
    public:
        Complex();
        Complex(double r, double i = 0);

        // Declaration 
        Complex operator~(const Complex & c) const;


        Complex operator+(const Complex & c) const;
        Complex operator-(const Complex & c) const;
        Complex operator*(const Complex & c) const;
        Complex operator*(double n) const; 
       friend Complex operator*(double m, const Complex & c)
               { return c * m; }    
        friend ostream & operator<<(ostream & os, const Complex & c);
    };
    Complex::Complex()
    {
        real = imaginenary = 0;
    }

    Complex::Complex(double r, double i )
    {
        real = r;
        imaginenary = i;
    }



    // Definition
    Complex Complex::operator~(const Complex & c) const   
    {
        Complex conj;
        conj.imaginenary = -1 * imaginenary;
        conj.real = real;
    }


    Complex Complex::operator+(const Complex & c) const
    {
        Complex sum;
        sum.imaginenary = imaginenary + c.imaginenary;
        sum.real = real + c.real;
        return sum;
    }

    Complex Complex::operator-(const Complex & c) const
    {
        Complex diff;
        diff.imaginenary = imaginenary - c.imaginenary;
        diff.real = real - c.real;
        return diff;
    }
    Complex Complex::operator*(const Complex & c) const
    {
        Complex mult;
        mult.imaginenary = imaginenary * c.imaginenary;
        mult.real = real * c.real;
        return mult;
    }

    Complex Complex::operator*(double mult) const
    {
        Complex result;
        result.real = real * mult;
        result.imaginenary = imaginenary * mult;
        return result;
    }
    ostream & operator<<(ostream & os, const Complex & c)
    {
        os << "(" << c.real <<"," << c.imaginenary<<"i)";
        return os;
    }
    int main()
    {
        Complex A;
        Complex B(5, 40);
        Complex C(2, 55);
        cout << "A, B, and C:\n";
        cout << A <<"; " << B << ": " << C << endl;
        cout << " complex conjugate is" << ~C << endl;  
        cout << "B + C: " << B+C << endl;
        cout << "B * C: " << B*C << endl;
        cout << "10 * B: " << 10*B << endl;
        cout << "B - C: " << B - C << endl;
        return 0;
    }
+3
source share
5 answers

to try

Complex Complex::operator~() const    
{ 
    Complex conj; 
    conj.imaginenary = -1 * imaginenary; 
    conj.real = real; 
    return conj;
} 

But it would be wiser to remove the operators from the class definition and create (friend) functions. It works better with implicit type conversion.

+3
source

The tilde operator (~) is a unary operator, so it should not accept a parameter (it works on *this). You also forgot to return the value from operator~.

Complex operator~() const 
{
    return Complex( real, -1 * imaginenary);
}

BTW: .

+9

, operator~, . operator+, - .

+3
Complex Complex::operator~(const Complex & c) const
{
    Complex conj;
    conj.imaginenary = -1 * c.imaginenary;
    conj.real = c.real;
    return conj;
}

.

, -, nonglobal, . , :)

+1
friend Complex operator*(double m, const Complex & c) { return c * m; } 
friend ostream & operator<<(ostream & os, const Complex & c); 

, ? , .

0

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


All Articles