14.10. Private functions

In some cases, there are member functions that are used internally by a class, but that should not be invoked by client programs. For example, calculatePolar and calculateCartesian are used by the accessor functions, but there is probably no reason clients should call them directly (although it would not do any harm). If we wanted to protect these functions, we could declare them private the same way we do with instance variables. In that case the complete class definition for Complex would look like:

class Complex
{
private:
  double real, imag;
  double mag, theta;
  bool cartesian, polar;

  void calculateCartesian ();
  void calculatePolar ();

public:
  Complex () { cartesian = false;  polar = false; }

  Complex (double r, double i)
  {
    real = r;  imag = i;
    cartesian = true;  polar = false;
  }

  void printCartesian ();
  void printPolar ();

  double getReal ();
  double getImag ();
  double getMag ();
  double getTheta ();

  void setCartesian (double r, double i);
  void setPolar (double m, double t);
};

The private label at the beginning is not necessary, but it is a useful reminder.

The active code below updates calculatePolar and calculateCartesian to be private functions. Notice how we are no longer able to call calculateCartesian in main. Feel free to modify the code and experiment around!

Before you keep reading...

Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.

You have attempted 1 of 2 activities on this page