Can you please explain the difference between an inspector and a mutator?
Submitted by: AdministratorAn 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
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
Top C++ Pointers & Functions Questions
☺ | Can you please explain the difference between Pointer to constant and pointer constant? |
☺ | How many minimum number of functions are need to be presented in c++? |
☺ | Explain function pointers? |
☺ | Explain pointer with examples? |
Top C Plus Plus Language Categories
☺ | C++ Pointers & Functions Interview Questions. |
☺ | C++ Operator Overloading Interview Questions. |
☺ | C++ Exception Handling Interview Questions. |
☺ | C++ Virtual Functions Interview Questions. |
☺ | C++ Template Interview Questions. |