+ 6

What is mutator function and accessor function?

cpp classes and objects

11th Mar 2018, 2:49 PM
Thomas Kurian
Thomas Kurian - avatar
2 Réponses
+ 3
To add on, mutators, as the name suggests, are functions or methods that change the value of a member variable. Usually these have 'set' in the method name (but not always, depending on the conventions of the language), they take a parameter to adjust the member variable with, and they don't have a return type (are void methods). These are also known as setters. Accessors are functions or methods that return the value of the member variable. These don't usually have parameters and often their return type matches the member variable returned. Some people define them with 'get' in the name, others do not & use the member variable name instead. These are also known as getters. Both methods have to be public to serve the purpose they were defined for, whereas the member variable is usually private. Examples of mutators: //C++ void set_name(string new_name) { name = new_name; } //Java public void setName(String newName) { name = newName; } Examples of accessors: //C++ void get_name( ) { return name; } //Java public String getName( ) { return name; }
12th Mar 2018, 9:51 PM
Jiren The Grey
Jiren The Grey - avatar
+ 2
They are functions used to get or set the value of private fields of a class.
11th Mar 2018, 5:11 PM
noerror
noerror - avatar