Toila
Toila

Reputation: 29

Conjugate function for complex number

I am trying to create a function that conjugate a complex number for example A(2, 3) will turn into A(2,-3) by typing ~A i have done a little code below but i guess it's wrong, i hope you can help me slove this. i have 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;
    }

Upvotes: 3

Views: 8550

Answers (5)

Toila
Toila

Reputation: 29

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

Is there anyway to avoid using friend in this problem? because i have read that from the book but not quite understand it.

Upvotes: 0

Tobias Langner
Tobias Langner

Reputation: 10808

try

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

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

Upvotes: 3

MSalters
MSalters

Reputation: 179799

Lots of code, but if you would have compared your operator~ to e.g. operator+ , you would have spotted one detail - there's no return statement.

Upvotes: 3

Krzysztof Bujniewicz
Krzysztof Bujniewicz

Reputation: 2417

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

This should do.

Although it's not the best idea to return anything you've allocated inside nonglobal scope, since that region in the memory can be overwritten anytime. And btw it's imaginary, not imaginenary :)

Upvotes: 1

Motti
Motti

Reputation: 114695

The tilde (~) operator is a unary operator so it shouldn't accept a parameter (it works on *this). You also forgot to return a value from operator~.

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

See your fixed code here

BTW: It's spelt imaginary.

Upvotes: 9

Related Questions