Computer Science Canada c++ please help |
Author: | tinhnho [ Sat Mar 13, 2004 5:47 pm ] | ||
Post subject: | c++ please help | ||
Hi everyone i have a proplem with C++.Here is the question: Use the class complex .Write two(2) versions ,a nonmember function friend version and a member function version.Bóth them have the binary operator functions add,multiply, and substract.Each should return complex. Declare three complex objects ,c1, c2 ,c3.Initialize the objects c1,c2 to values of your choice and then print thẹm Invoke each of the overloaded operators(eg., c3= c1+c2 ;)and print the resulting objects c3 Here is the complex function and what i thinking:
note: more information Example: Multiple c1 = 3+4i c2 = -2 -5i c3 = c1 * c2 ==>c3 = 14 -23i thanks for reading |
Author: | wtd [ Sat Mar 13, 2004 7:58 pm ] | ||
Post subject: | |||
You're headed in the right direction, but I don't think you've quite got your operator overloading syntax down. For instance, to overload the + operator for the friend version... This is shown to work
|
Author: | tinhnho [ Sun Mar 14, 2004 12:22 am ] |
Post subject: | |
thanks for reply. i got it figure out. and it works fine.But do you have any idea for member function version?thanks |
Author: | wtd [ Sun Mar 14, 2004 12:37 am ] | ||
Post subject: | |||
Well, just keep in mind that any function that's a member of a class has an implicit first argument called this. Thus we can take one of the arguments away from our operator and it'll still funcion quite nicely.
|
Author: | tinhnho [ Sun Mar 14, 2004 2:59 am ] | ||||
Post subject: | |||||
Here is for member function version:
And here is the nonmember function friend version:
Here is the output for 2 program(they have out same each other): c1= 4 + 2i c2= 5 + 7i c3= c1 + c2 = 9 + 9i c3= c1 * c2 =20 + 9i c3= c1 - c2 =-1 + -5i Press any key to continue . . . Please check and let me know if i got any error,thanks alot |
Author: | wtd [ Sun Mar 14, 2004 8:42 am ] | ||||
Post subject: | |||||
Just a few notes... for your copy constructor, you'll want to copy the data inside the object. As it is, I think you're just pointing something else to the same bit of memory. Something like:
Also, with the overloaded << operator, you no longer need the print function. You can simply write:
|
Author: | tinhnho [ Sun Mar 14, 2004 12:43 pm ] |
Post subject: | |
thank for reply.In this program, do i need to have copy constructor?which way is the best ?thanks |
Author: | tinhnho [ Sun Mar 14, 2004 1:31 pm ] |
Post subject: | |
I used other compiler which is MS visual C++ 6 to compiled it.There are 16 errors in friend version.I used to use Dev C++ 4.Anyone have same proplem? |
Author: | wtd [ Sun Mar 14, 2004 5:24 pm ] |
Post subject: | |
tinhnho wrote: I used other compiler which is MS visual C++ 6 to compiled it.There are 16 errors in friend version.I used to use Dev C++ 4.Anyone have same proplem?
If it works with Dev-C++ (which uses GCC as the compiler) then I wouldn't worry about it too much. MS Visual C++ 6 is notorious for not being compliant with standards. It's handling of input/output and templates are especially atrocious. I think Visual C++.NET improved this, but GCC's still a better bet. |
Author: | tinhnho [ Wed Mar 17, 2004 4:01 pm ] | ||
Post subject: | |||
hi wtd i have a proplem with this part.It doesn't work:
Example: c1= 4 + 2i c2= 5 + 7i c3=c1 * c2=(4+2i)*(5+7i)= 20 +28i +10i +14i^2=14i^2 + 38i +20 i^2=-1 ==>c3= 38i + 6 |
Author: | tinhnho [ Wed Mar 17, 2004 5:38 pm ] |
Post subject: | |
nvm,i got it figure out,thanks alot |