Examine the program named CLAS.CPP for our first example of a program with a little information hiding contained in it. This program is identical to the last one except for the way it does a few of its operations. We will take the differences one at a time and explain what is happening here. 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 much more complicated program.

The first difference is that we have a class instead of a structure beginning in line 4 of this program. 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 as illustrated here.

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. Stay with us, we will tie this all together in a meaningful and useful way very soon.


A private section of a class is a section of data which cannot beaccessed outside of the class, it is hidden from any outsideaccess. Thus, the variable named data_store which is a part of theobject (an object will be defined completely later) named dog1declared in line 23 is not available for use anywhere in the main

program. It is as if we have built a “brick wall” around thevariables to protect them from accidental corruption by outsideprogramming influences. It seems a little dumb to declare avariable in the main program that we cannot use, but that isexactly what we did.

Figure 5-1 is a graphical representation of the class with its”brick wall” built around the data to protect it. You will noticethe small peep holes we have opened up to allow the user to gainaccess to the functions. The peep holes were opened by declaringthe functions in the public section of the class.


A new keyword, public, is introduced in line 6 which states thatanything following this keyword can be accessed from outside ofthis class. Because the two functions are defined following thekeyword public, they are both public and available for use in thecalling function or any other function that is within the scope ofthe calling function. This opens two small peepholes in the solidwall of protection. You should keep in mind that the privatevariable is not available to the calling program. Thus, we canonly use the variable by calling one of the two functions definedas a part of the class. These are called member functions becausethey are members of the class.

Since we have declared two functions, we need to define them bysaying what each function will actually do. This is done in lines11 through 19 where they are each defined in the normal way, exceptthat the class name is prepended onto the function name andseparated from it by a double colon. These two functiondefinitions are called the implementation of the functions. Theclass name is required because we can use the same function namein other classes and the compiler must know with which class toassociate each function implementation.

One of the key points to be made here is that the private datacontained within the class is available within the implementationof the member functions of the class for modification or readingin the normal manner. You can do anything with the private datawithin the function implementations which are a part of that class,but the private data of other classes is hidden and not availablewithin the member functions of this class. This is the reason wemust prepend the class name to the function names of this classwhen defining them.

It would be well to mention at this point that it is legal toinclude variables and functions in the private part and additionalvariables and functions in the public part. In most practicalsituations, variables are included in only the private part andfunctions are included in only the public part of a classdefinition. Occasionally, variables or functions are used in the

other part. This sometimes leads to a very practical solution toa particular problem, but in general, the entities are used onlyin the places mentioned.

In C++ we have three scopes of variables, local, file and class.Local variables are localized to a single function and filevariables are available anywhere in a file following theirdefinition. A variable with class scope is available anywherewithin the scope of a class and nowhere else.

You must be very confused by this point since we have given a lotof rules but few reasons for doing all of this. Stay with us andyou will soon see that there are very practical reasons for doingall of this.