14.4. Accessor functions¶
By convention, accessor functions have names that begin with get and
end with the name of the instance variable they fetch. The return type,
naturally, is the type of the corresponding instance variable.
In this case, the accessor functions give us an opportunity to make sure
that the value of the variable is valid before we return it. Here’s what
get_real looks like:
double complex_number::get_real ()
{
if (cartesian == false) calculate_cartesian ();
return real;
}
If the cartesian flag is true then real contains valid data, and
we can just return it. Otherwise, we have to call calculate_cartesian
to convert from polar coordinates to Cartesian coordinates:
void complex_number::calculate_cartesian ()
{
real = mag * std::cos (theta);
imag = mag * std::sin (theta);
cartesian = true;
}
Assuming that the polar coordinates are valid, we can calculate the
Cartesian coordinates using the formulas from the previous section. Then
we set the cartesian flag, indicating that real and imag now
contain valid data.
As an exercise, write a corresponding function called calculate_polar
and then write get_mag and get_theta. One unusual thing about
these accessor functions is that they are not const, because
invoking them might modify the instance variables.
Take a look at the active code below, which uses the get_real
accessor function.
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 () { cartesian = true; polar = false; }
12 complex_number (double r, double i)
13 {
14 real = r; imag = i;
15 cartesian = true; polar = false;
16 }
17 void calculate_cartesian ()
18 {
19 real = mag * std::cos (theta);
20 imag = mag * std::sin (theta);
21 cartesian = true;
22 }
23 double get_real ()
24 {
25 if (cartesian == false) calculate_cartesian ();
26 return real;
27 }
28 double get_imag ()
29 {
30 if (cartesian == false) calculate_cartesian ();
31 return imag;
32 }
33};
34
35int main() {
36 complex_number c1 (5.0, 3.5);
37 std::cout << c1.get_real() << ", " << c1.get_imag() << std::endl;
38}
Write your implementation of calculate_polar in the commented area of the active
code below. Once you're done with that, write the get_mag and get_theta
accessor functions. Read the comments in main to see how we'll test if your
functions works. If you get stuck, you can reveal the extra problem at the end for help.
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};
20
21void complex_number::calculate_polar () {
22 // ``calculate_polar`` should convert the real and imaginary parts
23 // into magnitude and theta. Use the formula in the previous section.
24 // Write your implementation here.
25}
26
27double complex_number::get_mag () {
28 // ``get_mag`` should return the magnitude.
29 // Delete the return 0 and write your implementation here.
30 return 0;
31}
32
33double complex_number::get_theta () {
34 // ``get_mag`` should return the theta.
35 // Delete the return 0 and write your implementation here.
36 return 0;
37}
38
39int main() {
40 complex_number c1 (0.0, 1.0);
41 // Magnitude should be 1, theta should be pi/2, or about 1.5708
42 std::cout << c1.get_mag() << ", " << c1.get_theta() << std::endl;
43}
Reveal Problem
Let's write the code for the calculate_polar function.
Follow the format of the function calculate_cartesian.
-
cartesian = true; } -
polar = true; } -
mag = std::pow(real, 2) + std::pow(imag, 2); -
mag = std::sqrt(std::pow(real, 2) + std::pow(imag, 2)); -
theta = std::atan2(imag, real); -
void complex_number::calculate_cartesian () { -
void complex_number::calculate_polar () {
Reveal Problem
Let's write the code for the get_mag function,
which should return the magnitude of a complex_number object.
-
calculate_polar (); } -
return mag; } -
double complex_number::get_mag () { -
if (polar == false) { -
void complex_number::get_mag () {
Reveal Problem
Let's write the code for the get_theta function,
which should return the magnitude of a complex_number object.
-
calculate_cartesian (); } -
calculate_polar (); } -
return theta; } -
double complex_number::get_mag () { -
double complex_number::get_theta () { -
if (polar == false) {