Encapsulation
Encapsulation is the basic process of forming objects. An encapsulated
object is often called an abstract data type. Without encapsulation,
which involves the use of one or more classes, there is really no basis
for object-oriented programming.
Why use encapsulation?
When we fully encapsulate code, we actually build an impenetrable wall
to protect the contained code from accidental corruption due to the silly
little errors that we are all prone to make. We also tend to isolate errors
to small sections of code to make them easier to find and fix. We will
have a lot more to say about the benefits of encapsulation as we
progress through this Module.
Encapsulation is the basic method for producing
information hiding inobject-oriented methodology. The following program [OPEN.CPP] is a
‘conventional’ program that incorporates
no information hiding, largelybecause of the use of global identifiers.
#include <iostream.h>
struct one_datum {
int data_store;
};
void main()
{
one_datum dog1, dog2, dog3;
int piggy;
dog1.data_store = 12;
dog2.data_store = 17;
dog3.data_store = -13;
piggy = 123;
cout << "The value of dog1 is " << dog1.data_store << "\n";
cout << "The value of dog2 is " << dog2.data_store << "\n";
cout << "The value of dog3 is " << dog3.data_store << "\n";
cout << "The value of piggy is " << piggy << "\n";
}
A very simple structure is defined in lines 2 to 4 which contains a single
int type variable within the structure. Three variables are declared in
line 7, each of which contains a single int type variable, and each of the
three variables are available anywhere within the main function. Each
variable can be assigned, incremented, read, modified, or have any
number of operations performed on it; a few of these operations are
illustrated in lines 9 to 16.
The next program [CLAS.CPP] has limited information hiding. This
program is identical to the last one except for the way it does some of
its operations. We will take the differences one at a time and explain
what is happening. Keep in mind that this is a trivial program and the
safeguards built into it are not needed for such a simple program but are
used here to illustrate how to use these techniques in a larger and more
complicated program.
#include <iostream.h>
class one_datum {
int data_store;
public:
void set(int in_value);
int get_value(void);
};
void one_datum::set(int in_value)
{
data_store = in_value;
}
int one_datum::get_value(void)
{
return data_store;
}
void main()
{
one_datum dog1, dog2, dog3;
int piggy;
dog1.set(12);
dog2.set(17);
dog3.set(-13);
piggy = 123;
// dog1.data_store = 115; This is illegal in C++
// dog2.data_store = 211; This is illegal in C++
cout << "The value of dog1 is " << dog1.get_value() << "\n";
cout << "The value of dog2 is " << dog2.get_value() << "\n";
cout << "The value of dog3 is " << dog3.get_value() << "\n";
cout << "The value of piggy is " << piggy << "\n";
}
The first difference is that we have the definition of a
class instead of astructure beginning in line 2. The only difference between a class and a
structure is that a class begins with a private section whereas a structure
has no private section automatically defined. The keyword class is used
to declare a class.
The class named one_datum is composed of the single variable named
data_store and two functions, one named set( ) and the other named
get_value( ). A more complete definition of a class is a group of
variables and one or more functions that can operate on that data.
The PRIVATE section
A private section of a class is a section of data which cannot be
accessed outside of the class; it is hidden from any outside access. Thus
the variable named data_store, which is a part of the object (an object
will be defined completely later) named dog1 declared in line 18, is not
available for use anywhere in the main function. It is as if we have built
a ‘brick wall’ around the variables to protect them from accidental
corruption by outside programming influences. It seems a little strange
to declare a variable in the main program that we cannot use, but that is
exactly what we did.
The PUBLIC section
A new keyword,
public, is introduced in line 4 which states thatanything following this keyword can be accessed from outside of this
class. Because the two functions are defined following the keyword
public, they are both public and available for use in the calling function
or any other function that is within the scope of the calling function.
This opens two small peepholes in the solid wall of protection. You
should keep in mind that the private variable is not available to the
calling program. Thus, we can only use the variable by calling one of
the two functions defined as a part of the class. These are called
member functions because they are members of the class.
Since we have declared two functions, we need to define them by
saying what each function will actually do. This is done in lines 8 to 15
where they are each defined in the normal way, except that the class
name is prepended onto the function name and separated from it by a
double colon. These two function definitions are called the
implementation of the functions. The class name is required because we
can use the same function name in other classes and the compiler must
know with which class to associate each function implementation.
One of the key points to be made here is that the private data contained
within the class is available within the implementation of the member
functions of the class for modification or reading in the normal manner.
You can do anything with the private data within the function
implementations which are a part of that class, but the private data of
other classes is hidden and not available within the member functions of
this class. This is the reason we must prepend the class name to the
function names of this class when defining them.
Note that it is legal to include variables and functions in the private part
and additional variables and functions in the public part. In most
practical situations, variables are included in only the private part and
functions are included in only the public part of a class definition.
Occasionally, variables or functions are used in the other part. This
sometimes leads to a very practical solution to a particular problem, but
in general, the entities are used only in the places mentioned.
In C++ we have three scopes of variables: local, file and class. Local
variables are localised to a single function and file variables are available
anywhere in a file following their definition. A variable with class scope
is available anywhere within the scope of a class and nowhere else.
To clarify the use of the various OOP terms we have introduced, Table
1 presents definitions of the four terms we have used so far in this
Module, together with their use in C++.
Class: A grouping of data and methods (functions). A class is
similar to a type in ANSI-C.
Object: An instance of a class, similar to a variable defined as an
instance of a type.
Method: A function contained within the class, designed to operate
on the data within the class.
Message: Equivalent to a function call in ANSI-C. In object-oriented
programming, we send messages instead of calling functions.
Table 1: OOP Terminology in C++
Note that lines 5 and 6 of this program are actually the prototypes for
the two methods within a class. The method named set requires one
parameter of type int and returns nothing, so the return type is void.
The method named get_value ( ) has no input parameters but returns an
int type value to the caller.
Using a normal variable
There is another variable named piggy declared and used throughout
this program that illustrates that a standard C variable can be intermixed
with the objects and used in the normal manner. It would be a good
exercise for you to remove the comments from lines 24 and 25 to see
what kind of error message your compiler issues.
Links: Home C Programming Guide C++ programming Guide