• I. INTRODUCTION
Hello; nice to meet you! Welcome to the “C++ Accessors and Mutators Tutorial.”
The tutorial assumes you are familiar with the following vocabulary:
1. Instantiation is declaring an object of a class type.
2. Encapsulation is the idea of an object containing data and functions that operate on that data.
3. A class is a user defined type.
4. Inheritance allows the creation of hierarchical classifications.
5. Polymorphism is Greek for “many shapes;” which becomes manipulating “many types” through a common interface. Polymorphism gives a programmer “programming in the general” instead of “programming in the specific.”
6. Object-oriented programming (OOP) is the use of inheritance, run-time polymorphism, encapsulation, and the programming style of defining your own data types as classes.
• II. PRIVATE DATA MEMBERS
The variables declared as part of the class are data members. Data hiding occurs when access control is established by data members being declared in the private area of the body of a class definition. The private access specifier prevents direct access to the class data members. However, private data members can be accessed indirectly by public accessor and mutator member functions and friends of that class.
• III. CONSTRUCTORS
When data members are declared they can not be initialized in the class body. Therefore, constructors are used to initialize the class data members when the class objects are instantiated.
• IV. GOOD ACCESSOR CHARACTERISTICS
Accessors or get functions:
1. Read or obtain the value of private member variables.
This must be done in a manner that maintains the integrity of the private member data.
2. Display the value of private member variables.
The displayed information should be user friendly, i.e., formatted in a fashion easily readable and understandably by the user.
3. Print the value of private member variables.
When an inappropriate attempt is made to change the value of a private data member, a properly written get function with good characteristics will be programmed to notify the user. User management should receive written notification of all inappropriate activity as soon as it occurs.
• V. GOOD MUTATOR CHARACTERISTICS
Mutators or set functions:
1. Modify the value of private data members.
Public set functions set the value of private data members. However, set functions should not just change data. Set functions must be programmed to make sure what they are being called to do is correct before they do it. Properly written set functions are the first line of defense against, “garbage in, garbage out.”
2. Validate the value of private data members.
When an inappropriate attempt is made to change the value of a private data member, a properly written set function will be programmed to prevent the modification.
• VI. ADVANTAGES OF USING GET AND SET FUNCTIONS
The main advantages of always using get and set public class member functions is faster, more efficient, and less expensive program maintenance.
1. All data usage updates only have to be made to the appropriate public get member functions.
2. All data value storage updates only have to be made to the appropriate public set member functions.
• VII. SUMMARY
The names of the accessor and mutator member functions do not have to begin with get and set; however, the naming convention is a generally accepted programming practice.
In general, get and set functions are a public interface for read/write access to private data members.
Even though all class member functions can indirectly access private data members, the programmer should ensure the program is written so that all member functions call the appropriate get and set functions when interacting with private data members.