# Assignment

## Recommended Posts

Hey I have to an assignment for my basic oop programming class. A complex number has the form a + bi, where a and b are real numbers and i is . You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas: a + bi + c + di = (a + c) + (b + d)i a + bi – (c + di) = (a – c) + (b – d)i (a + bi) * (c + di) = (ac – bd) + (bc + ad)i (a + bi) / (c + di) = (ac + bd) / (c^2 + d^2) + (bc – ad)i / (c^2 + d^2) Design a class named Complex for representing complex numbers and the functions add, subtract, multiply, divide for performing complex number operations, Im sort of confused by what it means, it also says to overload a bunch of operators Overload the operators +, –,*, /, +=, –=, [], unary + and –, prefix ++ and ––, postfix ++ and ––, <<, >>. Overload the operators +, –, *, /, <<, >> as nonmember functions. Overload [] so that [0] returns a and [1] returns b. Could someone explain what the function parameters are supposed to be or something, Im pretty confused

##### Share on other sites
He wants you to practice operator overloading by implementing a complex number class. You should probably know what that is -- if not, you can try using Google to find out. If you don't know what a complex number is, Google can help you there, too.

Beyond that, you'll need to ask a more specific question. Especially since this is homework.

##### Share on other sites
well what would the add function look like? how can it perform any kind of operation with i and imaginary number that doesnt really exist.

##### Share on other sites
Assuming we're talking about C++. you overload operators much like you would write any other member function of a class, except that you use a special name and adhere to a specific set of parameters.

by non-member functions, he means functions which are not defined inside the class. Since they will be defined outside, all the necessary data to perform the operation will have to be accessable to the non-member function, either because the public interface of the class exposes them (preferred) or because the class has 'friend'ed the non-member function (this should be avoided because most would argue that it breaks encapsulation, but it is a viable approach).

as far as the difference between declaring member and non-member overloads, the non-member version will take one more parameter than the member version (because the member version implicitly takes 'this' -- that is, some instance of the class -- already).

As an example, to add two things together, you need two things. The non-member addition operator overload takes two parameters, while the member version takes 1 (plus the one it takes implicitly).

I'm afraid we can't be more specific than that, its policy here to direct homework questions in the right direction, rather than providing answers or direct examples.

However, do be careful of the prefix/postfix operators, in C++ they use specific signatures to diferentiate between them -- which is a little tricky to get your head around at first. Again, google will help you out.

You can come back to this thread later for feedback once you've got some work to show, and if you do it to completion I'd be happy to throw you a few other pointers here that its not really appropriate to share with you if you've never done it the 'naive' way before.

EDIT: Appologies if this wasn't the advice you were looking for, you must've edited the original post to include the bit about complex numbers [grin], before I assumed that you were looking for info on the overloading aspect, rather than the mathematical aspect.

##### Share on other sites
Quote:
 well what would the add function look like? how can it perform any kind of operation with i and imaginary number that doesnt really exist.

Google for "complex numbers." Your professor also gave you the relevant equations:
Quote:
 A complex number has the form a + bi, where a and b are real numbers and i is . You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas:

##### Share on other sites
But the definitions would look something like this?

my complex class.

void subtract(Complex c1);
void multiply(Complex c1);
void divide(Complex c1);

and inside the functions I would just execute those formulas he gave me?

##### Share on other sites
no, which is why i dont really get it. why do i need to make these functions and also overload the operators. arent they just doing the same thing..

##### Share on other sites
Quote:
 Original post by RogerThat123no, which is why i dont really get it. why do i need to make these functions and also overload the operators. arent they just doing the same thing..
Whoever assigned this to you wanted you to write regular functions named add, subtract, multiply, and divide and to write overloads of the +, –,*, /, +=, –=, [], unary +, unary –, prefix ++, prefix --, postfix ++, postfix --, <<, and >> operators. If you want to know why you need to do both operator overloading and regular function then you should ask that person. I'm afraid that we can only guess.

##### Share on other sites
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.

##### Share on other sites

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

how do i overload the unary + ?

##### Share on other sites
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?

##### Share on other sites
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...

##### Share on other sites
Quote:
 Original post by RogerThat123in the formula given its adding 2 complex numbers together isnt it?1st complex #: a,b2nd 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 complexnumber 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]

##### Share on other sites
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..

##### Share on other sites
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?

##### Share on other sites
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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628381
• Total Posts
2982360

• 10
• 9
• 15
• 24
• 11