| [ Introduction to OOP ] [ Encapsulation ] [ Objects ] [ More on Objects ] [ Abstract Data Types ] [ Inheritance ] [ Abstract Classes ] [ Templates ] |
2. Encapsulation
2.1 Constants encapsulated in a header file
Aside from any special object oriented features like classes, C++ contains a simple mechanism for encapsulation. This mechanism is a "header file".
Header files are commonly
used to place related data items and functions together so that they are
accessible to mutltiple programs. Encapsulation can be demonstrated using
a header file as seen in the following example.
| //
The header file "MyConstants.h" is a collection of constants
// This is a form of encapsulation. const float PI = 3.142; const float KM_TO_MILES = 0.625; const float MILES_TO_KM = 1.60; const float CM_TO_INCHES = 0.393; const float INCHES_TO_CM = 2.54; const float LITERS_TO_GALLONS = 0.2702; const float GALLONS_TO_LITERS = 3.70; //
This is a progrm that makes use of the above header file
|
Consider a case where only constants are
enclosed in a class. In order to assign values to the constants, a constructor
is required. This is because direct setting of constants is not permitted
in a class.
| #include<iostream.h>
// The
class MyConstants encapsulates public constants
int main(void){
|
The encapsulation of variables in a class can be defined by declaring the variables either public or private. The public variables form the visible part of the package (class). This part contains all the information that another program unit is able to know about this class. The private variables, on the other hand, contain the information, which can be used only within the class.
In the following example, data is acquired
from the satellite and encapsulated in the class called WeatherVars. For
the sake of simplicity, a single value is returned from all calls to the
functions of the class Satellite. In reality this will not be the case
as the variables Temperature, Humidity, WindSpeed and WindDirection change
with time. Different values are returned when these functions are called
in a real time environment.
| // The
class WeatherVars encapsulates weather related parameters.
class WeatherVars {public: float Temperature[30]; float Humidity[30]; float WindSpeed[30]; int WindDirection[30]; }; // The
class Satellite acquires data from the satellite.
// The
main function reads data from satellite. It takes 30 samples
|
In addition to constants and variables,
functions can also be encapsulated in a class.
| // Class
MathOperations encapsulates various mathematical functions.
// They are all declared as public and can be accessed by users. class MathOperations {public: int AddTwoNums(int a, int b); int SubTwoNums(int a, int b); float DivTwoNums(float a, float b); int MulTwoNums(int a, int b); int GetCube(int a); int GetSqr (int a); float GetSqrt(int a); }; int MathOperations::AddTwoNums(int
a, int b){ return (a + b);}
int main(void){
|
2
+ 3 = 5
2
- 3 = -1
2
/ 3 = 0.666667
2
* 3 = 6
cube
of 2 = 8
square
of 2 = 4
square
root of 4 = 2.0
The object-oriented meaning of encapsulation is to enclose related data, routines and definitions in a class capsule. This does not necessarily mean data hiding. This is evident from the above examples. The classes MyConstants, MyVariables, and MathOperations are defined to contain only public members. They do not hide any data. They merely enclose related data.
It is generally not required to know the implementation details of methods. This results in separating the contents of class into a public interface (only method declarations are present in the class header) and the implementation details. The interface is the visible surface of the capsule, while the implementation is hidden in the capsule.
2.5 Class Earth
Data hiding is evident in the class Earth
below. Both data and functions can be encapsulated in a class.
| // Class
Earth defines properties of planet Earth.
class Earth{ public: int NumOfOceans; int NumOfContinents; Earth(); double GetVolume(); double GetEscapeVel(); private: double Mass; double Density; double EquatorialRadius; double GravConstant; }; // This
constructor assigns values to the inherent properties of the class earth.
double
Earth::GetVolume() {return Mass/Density;}
int main(void){
|
2.6 Class to format text
The next example applies the concept to
a more informal entity, a format. Formatting is primarily the organization
of text to suit a user's needs. Multiple instances of the format class
below do not make sense as each instance would be performing the same operations.
| // This
class defines report properties.
class Format{ public : format() {PageNum = 1;}; char Tab() {return "\t";}; char Newline() {return "\n";}; char* PageTitle() {return " Class Report ";}; char* Header() {return "EECS 6500, 1999";}; int Footer() {return PageNum; }; void IncrementPage() {PageNum++;}; private: int PageNum; }; // This
program simply demonstrates the use of the format class.
|
References:
1. STC Ada Language Reference
Manual. Western Digital Corporation, 1983.
2. Henry F. Ledgard. The
Little Book of Object-Oriented Programming. Prentice-Hall, 1996.
3. Bjarne Stroustrup. What
Is Object-Oriented Programming. IEEE Software, May 1988.
| [ Introduction to OOP ] [ Encapsulation ] [ Objects ] [ More on Objects ] [ Abstract Data Types ] [ Inheritance ] [ Abstract Classes ] [ Templates ] |