Section 3 - Understanding Accessors

6B.3.1 Accessor/Mutator Methods Explained

Because the fields of the Galaxy class are declared private, the main() method cannot modify them directly.  Try this from main():

gal1.magnitude = 13.2;

What happened?  You can't get away with it, can you?  That's what private does.  We like that.

Instead, the client can use the method setMagnitude() which it can access, because it is declared public.  The client (main()) dereferences the setMagnitude() method using an object of the Galaxy class.  Here are two examples:

gal1.setMagnitude(100);
gal2.setMagnitude(13.2);

If we want to change the private data of the object gal1 from main() or some other client outside the class Galaxy, then we use gal1 to dereference the setMagnitude() method and pass in an int argument - the value we want to assign.  Similarly, with gal2.

Each of these is an attempt to set the magnitude of the respective Galaxy object to some value.  Notice I say "attempt."  Since the setMagnitude() mutator method has a built in filter to prevent out-of-range values from being assigned to the private data, there is no guarantee that either attempt will succeed.  For instance,  the first of these two methods calls will not cause the magnitude of gal1 to change.

To see why, just look at the definition of setMagnitude():

// mutators "set" methods
bool Galaxy::setMagnitude(double mag)
{
if (mag < -3 || mag > 30)
return false;
// else
magnitude = mag;
return true;
}

The value of 100 that is passed in (when we call gal1.setMagnitude(100)) is not in the legal range.  The if statement will evaluate to true (which is bad in this case) and the return false statement will be executed.  This will cause the method to return, skipping the remaining statements. 

Therefore:

  1. No value is assigned to magnitude, and
  2. The method returns a bool value of false to the client (which did not, by the way, actually make use of that information -- but could have if we had designed the client differently).

If you run the program you will see that, in fact, gal1 never has any of its private data modified because both accessor set() methods were called with bad (out-of-range) data.  This is evident when we display the data for both Galaxy objects at the end of the program. 

console shot

You also notice that gal2 does receive the assigned data because, for that object, we passed in legal values.

This, in a nutshell, is how a mutator (or "setter") method is defined and used.  The principle is very simple as long as you make sure to disallow those values that you deem to be nonsensical for the data you are representing.

Important

This is about as important a concept as there is in the course.  Your mutator methods are there to protect the private data.  If you somehow don't get this, you will lose lots of points on your assignments, so please do get it.  And ask if you have any doubt about it.

What about the accessor methods, getMagnitude() and getName()?

These are needed because, without them, our main() could never print out, display or even find out what is stored in the private data magnitude and name.  Again, the private nature of these two fields requires that we have such a method.  It is standard practice to include a public accessor get method  for any class field from which  we want our client to be able to read.

I often use the term accessor to mean both mutator methods (set methods) and accessor methods (get methods).