tags:

views:

130

answers:

5

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;
    }
+8  A: 

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.

Motti
+1 [:-)] I was going to paste the same!!!
TheMachineCharmer
+1  A: 
raceCh-
+3  A: 

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.

MSalters
+1  A: 

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.

Tobias Langner
friend Complex operator*(double m, const Complex } friend ostream Is there anyway to avoid using friend in this problem? because i have read that from the book but not quite understand it.
Toila
I don't see your problem here, but having a constructor, that takes a double allows implicit conversion from double to Complex. With operator* not being a member, the compiler can generate code for multiplying a double with a complex and the other way round. If the operator* is member, you can only multiply complex with double, but not the other way round since there's no implicite converstion from Complex to double and there's no operator* that takes a Complex for double.
Tobias Langner
A: 
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.

Toila
you should not need friend in this cases, since you have full (reading) access to the members of complex. Friend is only needed if you need access to the hidden internals.
Tobias Langner