COMPLEX
A C++ Class for Complex Arithmetic


COMPLEX is a C++ class that defines complex numbers and the operations necessary to do arithmetic on them.

Since the ANSI C++ standard library now includes a complex class, the information described here is probably not of practical use. On the other hand, it may be helpful as a practical demonstration of how to define a useful arithmetic class.

The COMPLEX class defines a complex number as a pair of double precision values. A variable c in this class may be declared by


        complex c;
      
The declaration can include an initialization:

        complex c = complex ( 1.0, 2.0 );
      

A variable c declared as complex may be assigned a value using the Complex() function:


        c = complex ( a, b )
      
where a and b are double values; the assignment

        c = complex ( a )
      
sets the real part of c to a, and the imaginary part to 0.

A variable c declared as complex may be used in addition, subtraction, multiplication, or division with another complex value, or a double value:


        c3 = c1 + c2;
        c3 = c1 - c2;
        c3 = c1 * c2;
        c3 = c1 / c2;
      

A variable c declared as complex may be conjugated, or its real or imaginary part may be produced as a double value:


        c2 = ~c;
        d1 = c1.real ( );
        d2 = c1.imaginary ( );
      

Related Data and Programs:

CPP is a directory of C++ examples which includes an example of the declaration and use of complex variables.

Reference:

  1. Steve Oualline,
    Practical C++ Programming,
    O'Reilly & Associates, 1997,
    ISBN: 1-56592-139-9

Source Code:

Examples and Tests:

You can go up one level to the C++ source codes.


Last revised on 08 January 2006.