Can you please explain the difference between an inspector and a mutator?

Submitted by: Administrator
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..
Submitted by: Administrator

Read Online C++ Pointers & Functions Job Interview Questions And Answers