Engineering Science 1036A/B Lecture 26: Class and Objects: Accessor/Mutator
Access Privileges in a Class
Public data or function members can be accessed from the main() function
(driver function) directly using member access operator
•
Private data or function members can not be accessed directly from the main
function but can be accessed indirectly by any public member functions of the
same class
Compiler error will be generated if we attempt to access private members
using dot operator
○
•
Can be listed in any order in a class
•
Can appear multiple times in a class declaration
•
If not specified, the default is private
•
Example with private data members
Accessor/Mutator Functions
To access private data members from the main function indirectly, the class
declaration contains two types of public functions known as accessor and
mutator functions
Get-functions is referred to as a getter (accessor)
return Type getPropertyName();
§
○
Set-function is referred to as a setter (mutator)
void setPropertyName(dataType propertyValue);
§
○
•
Accessor/Mutator