Public Group

# Assignment

This topic is 3126 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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.

1. 1
2. 2
3. 3
Rutin
14
4. 4
5. 5

• 10
• 11
• 14
• 10
• 25
• ### Forum Statistics

• Total Topics
632651
• Total Posts
3007657
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!