|
Reference · Data types Standard C · assert.h · complex.h · ctype.h · fenv.h · float.h · locale.h · math.h · setjmp.h · signal.h · stdarg.h · stddef.h · stdio.h · stdlib.h · string.h · time.h Standard C++ · IOstream · new Win32 · gc.h DOS, DOS32, Win16 · bios.h · cerror.h · disp.h · dos.h · dos.h part 2 · emm.h · handle.h · int.h · msmouse.h · sound.h · swap.h · tsr.h · winio.h Other C · bitops.h · conio.h · controlc.h · direct.h · fltpnt.h · io.h · page.h · process.h · search.h · sys\stat.h · tabsize.h · trace.h · utime.h · unmangle.h · util.h Other C++ · regexp.h · class complex |
The Complex ClassThis chapter describes:
This class implements complex numbers. Using overloaded functions and operators, you can treat complex numbers like you treat reals or integers. You can use the following:
Using the Complex ClassTo use complex numbers, add the library complex to your project and include file oldcomplex.h in your file, in a statement like this:#include <oldcomplex.h>You can declare a complex variable three ways: uninitialized, initialized with two numbers, or initialized with another complex number. For example: complex z1; // Uninitialized complex z2(1.0,2.0); // Initialized with two numbers complex z3 = z2; // Initialized with a complex numberTo access components of a complex number, use member functions complex::real() and complex::imag(). For example: complex z1, z2(5.0,6.0); z1.real() = z2.imag(); z1.imag() = z2.real();To print or read a complex number, use stream operators operator>> and operator<<. For example, this program:
#include <complex.h>
#include <iostream.h>
main()
{
complex z1(1.1, 2.0), z2;
cout << "Enter z2: ";
cin >> z2;
cout << "z1 = " << z1 << '\n';
cout << "z2 = " << z2 << '\n';
return 0;
}
prints this:
Enter z2: 10 9.8 z1 = (1.1, 2) z2 = (10,9.8)You can also use a complex number in the middle of a statement. For example, this line prints out the sine of (1, 1): cout << sin(complex(1,1) );Note: To invoke the complex version of a function, one of its arguments must be complex. For example, to figure the square root of -1, you must use sqrt(complex(-1,0)).
Member FunctionsThe five types of class member functions are:
Constructor Function The following function is a constructor function. complex Header oldcomplex.h Prototype complex::complex(); complex::complex(double re, double im ); complex::complex(const complex& z ); Description The first constructor creates an unitialized complex number. For example: complex z; The second constructor creates a complex number with the real part initialized to re and the imaginary part initialized to im. For example: complex z1(1.2,3.0), z2(3,4); The third constructor creates a copy of a complex number. It is also called the X(X&) constructor. For example:
#include <oldcomplex.h>
complex complexFunc()
{
// Invokes complex(double, double)
complex z1(2.0,4.0);
// Invokes complex(const complex&)
complex z2 = z1;
// Invokes complex(const complex&) to
// create a temporary variable and adds
// it to z1.
return complex(1.1, 2.2) + z1;
}
Operator Functions The operators defined for complex are overloaded to have the same meaning as the built-in operators. For example, + is the addition of two complex variables just as it is used for the addition of two variables of type double. The operators in the complex class have the usual operator precedence. The five types of operators defined for the complex class are:
complex::operator= Header oldcomplex.h Prototype complex& complex::operator=(const complex& z); complex& complex::operator=(double u); Description Assigns a value to complex variable. complex::operator+= Header oldcomplex.h Prototype complex& complex::operator+=(const complex& z); complex& complex::operator+=(double u); Description Adds a value to a complex variable and assigns the result to the complex variable. complex::operator-= Header oldcomplex.h Prototype complex& complex::operator-=(const complex& z); complex& complex::operator-=(double u); Description Subtracts a value from a complex variable and assigns the result to the complex variable. complex::operator*= Header oldcomplex.h Prototype complex& complex::operator*=(const complex& z); complex& complex::operator*=(double u); Description Multiplies a value by a complex variable and assigns the result to the complex variable. complex::operator/= Header oldcomplex.h Prototype complex& complex::operator/=(const complex& z); complex& complex::operator/=(double u); Description Divides a complex variable by a value and assigns the result to the complex variable. complex::operator- Header oldcomplex.h Prototype complex complex::operator-() const; Description Returns the negative of a complex number. complex::operator! Header oldcomplex.h Prototype int complex::operator!() const; Description Performs logical negation on a complex number. operator+ Header oldcomplex.h Prototype complex operator+(const complex& z1, complex& z2); complex operator+(double u, const complex& z); complex operator+(const complex& z, double u); Description Returns the sum of a complex number and any other number. operator- Header oldcomplex.h Prototype complex operator-(const complex& z1, const complex& z2); complex operator-(double u, const complex& z); complex operator-(const complex& z, double u); Description Returns the difference of a complex number and any other number. operator* Header oldcomplex.h Prototype complex operator*(const complex& z1, const complex& z2); complex operator*(double u, const complex& z); complex operator*(const complex& z, double u); Description Returns the product of a complex number and any other number. operator/ Header oldcomplex.h Prototype complex operator/(const complex& z1, const complex& z2); complex operator/(double u, const complex& z); complex operator/(const complex& z, double u); Description Returns the quotient of a complex number and any other number. operator&& Header oldcomplex.h Prototype complex operator&&(const complex& z1, const complex& z2); complex operator&&(double u, const complex& z); complex operator&&(const complex& z, double u); Description Performs logical AND. Returns 0 if both arguments are 0 or (0, 0). operator|| Header oldcomplex.h Prototype complex operator||(const complex& z1, const complex& z2); complex operator||(double u, const complex& z); complex operator||(const complex& z, double u); Description Performs logical OR. Returns 0 if both arguments are 0 or (0, 0). operator! Header oldcomplex.h Prototype complex operator!=(const complex& z1, const complex& z2); complex operator!=(double u, const complex& z); complex operator!=(const complex& z, double u); Description Tests for inequality. operator== Header oldcomplex.h Prototype complex ::operator==(const complex& z1, const complex& z2); complex ::operator==(double u, const complex& z); complex ::operator==(const complex& z, double u); Description Tests for equality. operator<< Header oldcomplex.h Prototype ostream& operator<<(ostream& s, const complex& z ); Description Reads z from the stream s, in the form(real, imaginary). operator>> Header oldcomplex.h Prototype istream& operator>>(istream& s, const complex& z); Description Reads z from the stream s, in the form(real, imaginary). Complex FunctionsThe complex functions are unique to the complex type and do not overload names of the ANSI C math library.arg Header oldcomplex.h Prototype double arg(const complex& z ); DescriptionThe arg function determines the angle, in radians, of the number inthe complex plane specified in the x argument. The positive real axis has angle 0; the positive imaginary axis has angle pi/2. The arg function is equivalent to: atan2( imag( x), real( x)) Return ValueReturns the argument of the complex number z in radians.
complex::imag, complex::real
|