How are accessors and mutators different? An example and explanation would be great.
How are accessors and mutators different? An example and explanation would be great.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
An accessor is a class method used to read data members, while a mutator is a class method used to change data members.
Here’s an example:
It’s best practice to make data members
private(as in the example above) and only access them via accessors and mutators. This is for the following reasons: