The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details. 
Home Classes and invariants Complex Numbers  
Complex Numbers
As a running example for the rest of this chapter we will consider a class definition for complex numbers. Complex numbers are useful for many branches of mathematics and engineering, and many computations are performed using complex arithmetic. A complex number is the sum of a real part and an imaginary part, and is usually written in the form x + yi, where x is the real part, y is the imaginary part, and i represents the square root of 1. The following is a class definition for a userdefined type called Complex: class Complex{ double real, imag; public: Complex () { } Complex (double r, double i) { real = r; imag = i; } };
As usual, there are two constructors: one takes no parameters and does nothing; the other takes two parameters and uses them to initialize the instance variables. So far there is no real advantage to making the instance variables private. Let's make things a little more complicated; then the point might be clearer. There is another common representation for complex numbers that is sometimes called "polar form" because it is based on polar coordinates. Instead of specifying the real part and the imaginary part of a point in the complex plane, polar coordinates specify the direction (or angle) of the point relative to the origin, and the distance (or magnitude) of the point. The following figure shows the two coordinate systems graphically. Complex numbers in polar coordinates are written r e^{i theta}, where r is the magnitude (radius), and theta is the angle in radians. Fortunately, it is easy to convert from one form to another. To go from Cartesian to polar,
To go from polar to Cartesian,
So which representation should we use? Well, the whole reason there are multiple representations is that some operations are easier to perform in Cartesian coordinates (like addition), and others are easier in polar coordinates (like multiplication). One option is that we can write a class definition that uses both representations, and that converts between them automatically, as needed. class Complex{ double real, imag; double mag, theta; bool cartesian, polar; public: Complex () { cartesian = false; polar = false; } Complex (double r, double i) { real = r; imag = i; cartesian = true; polar = false; } }; There are now six instance variables, which means that this representation will take up more space than either of the others, but we will see that it is very versatile. Four of the instance variables are selfexplanatory. They contain the real part, the imaginary part, the angle and the magnitude of the complex number. The other two variables, cartesian and polar are flags that indicate whether the corresponding values are currently valid. For example, the donothing constructor sets both flags to false to indicate that this object does not contain a valid complex number (yet), in either representation. The second constructor uses the parameters to initialize the real and imaginary parts, but it does not calculate the magnitude or angle. Setting the polar flag to false warns other functions not to access mag or theta until they have been set. Now it should be clearer why we need to keep the instance variables private. If client programs were allowed unrestricted access, it would be easy for them to make errors by reading uninitialized values. In the next few sections, we will develop accessor functions that will make those kinds of mistakes impossible.


Home Classes and invariants Complex Numbers 