Object oriented programming is a new way of approaching the job of programming. Object oriented programming will seem very unnatural to a programmer with a lot of procedural programming experience. This tutorial is the beginning of the definition of object oriented programming, and we will study the topic of encapsulation which is a “divide and conquer” technique. As stated earlier, there are a lot of new terms used with object oriented programming. Don’t be intimidated by the new terminology, study the terms one at a time in a meaningful order.

Encapsulation is the process of forming objects. An encapsulated object is oftencalled an abstract data type and it is what object oriented programming is all about. Without encapsulation, which involves the use of one or more classes, there is no object oriented programming. Of course there are other topics concerning object oriented programming, but this is the cornerstone.

Most important feature of Object Oriented Programming is the object. Object is a logical entity which contains the data and code that manipulated that data. Within an object some data is private, and that data is inaccessible out side that object. So in this way an object protects the data from being modified by any third party. The linkage of data and code in this way is called encapsulation.


We need encapsulation because we are human, and humans make errors. When we properly encapsulate some 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 the tutorial.


The program named OPEN.CPP is a really stupid program because it does next to nothing, but it will be the beginning point for our discussion of encapsulation, otherwise known as information hiding. Information hiding is an important part of object oriented programming and you should have a good grasp of what it is by the time we finish this chapter.

A very simple structure is defined in lines 4 through 6 which contains a single int type variable within the structure. This is sort of a silly thing to do but it will illustrate the problem we wish to overcome in this chapter. Three variables are declared in line 10, 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 the operations are illustrated in lines 13 through 21 and should be self explanatory to anyone with a little experience with the C programming language.

An isolated local variable named piggy is declared and used in the same section of code to illustrate that there is nothing magic about this code.Study this simple program carefully because it is the basis for beginning our study of encapsulation. Be sure to compile and execute this program, then we will go on to the next example program.