Program about Complex numbers.

Apr 22, 2010 at 5:45pm
...
Last edited on May 6, 2010 at 4:33pm
Apr 22, 2010 at 6:48pm
I would design it like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
template <typename T>
class Complex {
public:
    Complex(T const &real) : real(real), imaginary(0) { }
    Complex(T const &real, T const &imaginary) :
    real(real), imaginary(imaginary) { }
    
    friend T const & Re<> (Complex<T> const &z);
    friend T const & Im<> (Complex<T> const &z);
    
    Complex<T> operator+ (Complex<T> const &z)
    {
        return Complex<T>(real + z.real, imaginary + z.imaginary);
    }    
    Complex<T> operator* (Complex<T> const &z)
    {
        return Complex<T>(real * z.real - imaginary * z.imaginary,
                          real * z.imaginary + imaginary * z.real);
    }
    
private:
    T real;
    T imaginary;
};
Last edited on Apr 22, 2010 at 6:49pm
Apr 22, 2010 at 7:11pm
I would stay away from templates just yet. Keep it as simple as possible. Your class needs to have only two double variables: the real and the imaginary parts.

Good luck!
May 6, 2010 at 12:59am
...
Last edited on May 6, 2010 at 4:34pm
May 6, 2010 at 1:02am
So Far I have this much in the program. Am I in the right track? And I do have a lot of errors. So can you check please?
May 6, 2010 at 1:15am
It is really hard to evaluate code that is not in [code] tags and properly indented.
May 6, 2010 at 2:50am
...
Last edited on May 6, 2010 at 4:34pm
May 6, 2010 at 2:52am
How can I indent my code because whenever I copy paste it. It happens the same shit.
May 6, 2010 at 5:42am
rootsline1 wrote:
How can I indent my code because whenever I copy paste it. It happens the same shit.


put it between a pair of [code] [/code] tags.
May 6, 2010 at 9:19am
rootsline1, yes, basically, you are on a right track. However, your present code contains a dozen of syntax errors like c6(5,6,). Try to get rid of them first.

It is much better to use a template to avoid hardcored data type. You are using float one. Right? However, it is not the only choice. A complex number might compose integer or double precision real.
Last edited on May 6, 2010 at 9:19am
May 6, 2010 at 6:45pm
If you're still needing help on this topic I suggest you stop erasing the history of your posts. No one else can jump in to help you now since we can't see the original question.
Topic archived. No new replies allowed.