Assignment
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
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.
Beyond that, you'll need to ask a more specific question. Especially since this is homework.
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.
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.
You should be able to turn up plenty of examples of operator overloading on google.
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.
You should be able to turn up plenty of examples of operator overloading on google.
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.
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:
But the definitions would look something like this?
my complex class.
void add(Complex c1);
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?
my complex class.
void add(Complex c1);
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?
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..
Quote:Original post by RogerThat123Whoever 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.
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..
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement