In OOP, classes need to define not only data structures (objects, in particular), but
also the operations that can be carried out on such objects. For example, if a text
string is an ADT, then user-defined operations must be encoded for functions such
as concatenation, redaction, and testing for equality. These are referred to as class
member functions, in that they are specific to objects, or instances of a given class.
Programming should be provided during class definition for any operations that
objects could be expected to experience. This approach is demonstrated by the code
in the Appendix. The class member functions include a concatenation function (to
join two data members) and a destructor function (to deallocate memory associated
with an object), even though these class member functions were not utilized in the
main body of the program. At the time that the class was formulated, it was deemed
possible that these functions would be needed, so the class includes them.
Another feature of OOP is the concept of encapsulation. Some operations
are unavailable to objects or other data structures outside a particular class.
Encapsulation includes the internal implementation details of a particular type, as
well as the externally available operations and functions that can act on objects of
that type. Details regarding the execution of certain operations can be hidden from
user-provided code that implements the data type. For example, if a program to
calculate masses or lengths uses a class member function to convert units from
English to metric units, changing the internal workings of the unit conversion code
should have no effect on the accuracy of the calculation. In short, the
implementation of the unit conversion is hidden from its clients.
OOP can require a more sophisticated understanding of programming
procedures than top-down programming or procedural methodologies. In other
programming methodologies objects generally are implicitly created, and
subsequently destroyed after being used. In contrast, C++ offers user control of
dynamic memory allocation. Selection and definition of objects in C++ allows
memory to be apportioned for the object and its associated data. (If a data structure
or object is not used at some point in the program, memory can at that juncture be
deallocated as well.) Thus, selection of objects is a key to a number of facets in an
OOP approach to problem-solving.
The code in the Appendix merits closer examination to highlight the
functionality of certain portions of the class and the main program. Lines 3 through
8 consist of class definitions and inclusion of files for input/output, standard library
functions, mathematical functions, and string functions.
The String class (lines 10 through 75) consists of data associated with each
object that is an instance of the String class, and a number of class member
functions. The data includes private data members for a pointer to a character, and
the length of the object; and a public String function defining a null string.