What is the difference between an inspector and a mutator?
- The get() functions are usually refered to as inspectors as that just retrieve the data values from the source. However, mutators that can be refered to as set() finctions change the values of the data source.
What is the difference between an inspector and a mutator?
- An object’s state is returned without modifying the object’s abstract state by using a function called inspector. Invoking an inspector does not cause any noticeable change in the object’s behavior of any of the functions of that object.
- A mutator, on the other hand, changes the state of an object which is noticeable by outsiders. It means, it changes the abstract state of the object.
- The following code snippet depicts the usage of these two functions :
class ShoppingCart
{
public:
int addItem(); //Mutator
int numItems() const; //Inspector
};
- The function addItems() is a mutator. The reason is that it changes the ‘ShoppingCart’ by adding an item.
- The function numItems() is an inspector. The reason is that it just updates the count of number of items in the ‘ShoppingCart’.
- The const declaration followed by int numItems() specifies that numItems() never change the ‘ShoppingCart’ object..