14.6. A function on complex_number numbers

A natural operation we might want to perform on complex numbers is addition. If the numbers are in Cartesian coordinates, addition is easy: you just add the real parts together and the imaginary parts together. If the numbers are in polar coordinates, it is easiest to convert them to Cartesian coordinates and then add them.

Again, it is easy to deal with these cases if we use the accessor functions:

complex_number add (complex_number& a, complex_number& b)
{
  double real = a.get_real() + b.get_real();
  double imag = a.get_imag() + b.get_imag();
  complex_number sum (real, imag);
  return sum;
}

Notice that the arguments to add are not const because they might be modified when we invoke the accessors. To invoke this function, we would pass both operands as arguments:

complex_number c1 (2.0, 3.0);
complex_number c2 (3.0, 4.0);

complex_number sum = add (c1, c2);
sum.print_cartesian();

The output of this program is

5 + 7i

The active code below uses the add function for complex_number objects. As an exercise, write the subtract function for complex_number objects in the commented area of the active code. If you get stuck, you can reveal the extra problem at the end for help. Once you are finished, feel free to modify the code and experiment around!

Example c192_fourteenseven
 1#include <iostream>
 2#include <cmath>
 3
 4class complex_number
 5{
 6  double real = 0.0, imag = 0.0;
 7  double mag = 0.0, theta = 0.0;
 8  bool cartesian, polar;
 9
10public:
11  complex_number ();
12  complex_number (double r, double i);
13  void calculate_cartesian ();
14  double get_real ();
15  double get_imag ();
16  void calculate_polar ();
17  double get_mag ();
18  double get_theta ();
19  void print_cartesian ();
20  void print_polar ();
21};
22
23complex_number add (complex_number& a, complex_number& b);
24complex_number subtract (complex_number& a, complex_number& b) {
25  // ``subtract`` should subtract b from a and return
26  // the difference of the two ``complex_number`` objects.
27  // Delete the existing code and write your implementation here.
28  complex_number c (0,0); return c;
29}
30
31int main() {
32  complex_number c1 (2.0, 3.0);
33  complex_number c2 (3.0, 4.0);
34  complex_number sum = add (c1, c2);
35  sum.print_cartesian();
36
37  // Difference should be 1 + 1i
38  complex_number diff = subtract(c2, c1);
39  diff.print_cartesian();
40}
Reveal Problem

Let's write the code for the subtract function, which should return the difference of two complex_number objects.

  1.    return diff;
    }
  2.    return sum;
    }
  3. complex_number diff (imag, real);
  4. complex_number diff (real, imag);
  5. complex_number subtract (complex_number& a) {
  6. complex_number subtract (complex_number& a, complex_number& b) {
  7. double imag = a.get_imag() - b.get_imag();
  8. double real = a.get_real() + b.get_real();
  9. double real = a.get_real() - b.get_real();

What is the correct output of the code below?

int main() {
  Complex c1 (2.5, 1.3);
  Complex c2 (3.9, 4.4);
  Complex c3 (9.5, 7.6);
  Complex sum = add (c1, c2);
  Complex diff = subtract(c3, sum);
  diff.printCartesian();
}