Wednesday, September 21, 2011

C++ ACCESSORS AND MUTATORS









REVISED: Sunday, March 3, 2013




CONTENTS:
I.      INTRODUCTION TO C++ ACCESSORS AND MUTATORS
II.    C++ PRIVATE DATA MEMBERS
III.   C++ CONSTRUCTORS
IV.  GOOD C++ ACCESSOR CHARACTERISTICS
V.   GOOD C++ MUTATOR CHARACTERISTICS
VI.  ADVANTAGES OF USING C++ GET AND SET FUNCTIONS
VII. C++ ACCESSORS AND MUTATORS SUMMARY 

YOU WILL LEARN:
1. C++ Accessors.
2. C++ Mutators.
3. The characteristics of a good C++ accessor.
4. The characteristics of a good C++ mutator.
5. The advantages of using C++ accessors and mutators.

I. INTRODUCTION TO C++ ACCESSORS AND MUTATORS


Hello; nice to meet you! Welcome to the “C++ Accessors and Mutators Tutorial.”

A.  Vocabulary


The tutorial assumes you are familiar with the following C++ 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. C++ 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. C++ 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 C++ ACCESSOR CHARACTERISTICS

A.  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 C++ MUTATOR CHARACTERISTICS 

A.  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 C++ GET AND SET FUNCTIONS

A.  Main Advantages


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. C++ ACCESSORS AND MUTATORS 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. 

YOU HAVE LEARNED:
1. Accessors.
2. Mutators.
3. The characteristics of a good accessor.
4. The characteristics of a good mutator.
5. The advantages of using accessors and mutators.




-->

-->

-->








How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"




No comments:

Post a Comment