Assignment

Started by
16 comments, last by Zahlman 14 years, 1 month ago
ok well heres my attempt on the + operator...

double Complex::getA(){	return a;}double Complex::getB(){	return b;}double Complex::getI(){	return sqrt(-1.0);}//non member function overloaddouble operator+( Complex c1, Complex c2 ){	return (c1.getA() + c2.getA() + (c1.getB() + c2.getB())*c1.getI());}


i dont know if thats right because I dont really get how to compute an imaginary number but yeah.
Advertisement
i overloaded this binary operator

double operator+( Complex c1, Complex c2 )
{
return (c1.getA() + c2.getA() + (c1.getB() + c2.getB())*c1.getI());
}

how do i overload the unary + ?
You're not going to actually be expected to compute i. The sqrt(-1) is shown as i because you can't actually get a numerical value for it. That's why the professor gave you all those numbers in the form a+b*i. Think about what that operator+ overload is doing. You take 2 complex numbers, and return a double. Is that what the assignment is asking? Is that how complex numbers add together?
in the formula given its adding 2 complex numbers together isnt it?

1st complex #: a,b
2nd complex #: c->(a),d->(b) (in the formulas)

so shouldnt the math functions add, subtract etc computer those formulas?

Design a class named Complex for representing complex numbers and the functions add, subtract, multiply, divide for performing complex
number operations, and the tostring function for returning a string representation for a complex number. The tostring function returns a + bi as a string. If b is 0, it simply returns a.

So should it be printing them as they were adding them together in a string instead? or computer it into a value double.

and as for i. exactly, i dont get how to compute i in any of the functions to return a double. it doesnt say what the add functions etc are supposed to do but it gives you a formula so i assumed it should return some sort of value...
Quote:Original post by RogerThat123
in the formula given its adding 2 complex numbers together isnt it?

1st complex #: a,b
2nd complex #: c->(a),d->(b) (in the formulas)

so shouldnt the math functions add, subtract etc computer those formulas?

Design a class named Complex for representing complex numbers and the functions add, subtract, multiply, divide for performing complex
number operations, and the tostring function for returning a string representation for a complex number. The tostring function returns a + bi as a string. If b is 0, it simply returns a.

So should it be printing them as they were adding them together in a string instead? or computer it into a value double.

and as for i. exactly, i dont get how to compute i in any of the functions to return a double. it doesnt say what the add functions etc are supposed to do but it gives you a formula so i assumed it should return some sort of value...
It seems you're getting hung up a bit on the math part of the assignment.

You might take a look at the Wikipedia article on complex numbers, which includes a summary of the operations you've been asked to implement (I didn't look at the summary that carefully, but I assume it's correct). [Edit: Never mind - it looks like you already have that info.]

Note that in general, the result of these operations is another complex number, not a real number. That should give you a hint as to what's wrong with your current implementation, and how to fix it.

[Edited by - jyk on March 5, 2010 11:33:42 PM]
ok i got all the math done, thanks very much. it outputs another complex number instead for all the functions.

but how can I overload the + and - unary versions, i did the + and - binary already.

also, when i overload ++ and -- what should it be doing? increasing the complex number by 1? how would i do that..
Quote:but how can I overload the + and - unary versions, i did the + and - binary already.
Implement the unary + and - operators as member functions with return type Complex. If you're not sure how to do this, just Google 'c++ operator overloading' and you should be able to find some examples.
Quote:also, when i overload ++ and -- what should it be doing? increasing the complex number by 1? how would i do that..
It's not immediately clear to me what meaning these operators would have for a complex number type. Are you sure that you're expected to implement these particular operators?
A unary operator takes one argument. A binary operator takes two.

When you write the operator overload as a member function, the this-object provides one argument, and the parameters provide the others.

Therefore, unary +, implemented as a member function, has no parameters.

This operator is used for something like 'Complex b = +a'. You read the plus as "positive". Similarly to with real numbers, where "+3" means the same thing as 3, you would implement this operator to have no effect. Thus, unary + returns a copy of the current object. If you're returning by value (as you should), then the copy is done automatically by the process of returning, so you can just "return *this;".

For unary -, also known as negation, there are again no parameters. You need to return the negative of the current value; something that, added to the current object, yields zero.

This topic is closed to new replies.

Advertisement