USING A NORMAL VARIABLE
There is another variable named piggy declared and used throughoutthis example program that illustrates that a normal variable can be intermixed with the objects and used in the normal manner. Theuse of this variable should pose no problem to you, so after youunderstand the program, be sure to compile and execute it. Itwould be a good exercise for you to remove the comments from lines31 and 32 to see what kind of error message your compiler issues.
This program illustrates information hiding but it will not beclear to you that it really does anything worthwhile until we studythe next two programs. Be sure to compile and execute this programbefore continuing on to the next example program.
A PROGRAM WITH PROBLEMS
Examine the program named OPENPOLE.CPP for an example of a program with a few serious problems that will be overcome in the next example program by using the principles ofencapsulation.
We have two structures declared, one being a rectangle and theother being a pole. The data fields should be self explanatorywith the exception of the depth of the flagpole which is the depthit is buried in the ground, the overall length of the pole istherefore the sum of the length and the depth.
Based on your experience with ANSI-C, you should have no problemunderstanding exactly what this program is doing, but you may bea bit confused at the meaning of the result found in line 38 wherewe multiply the height of the square with the width of the box.This is perfectly legal to do in ANSI-C or C++, but the result hasno earthly meaning because the data are for two different entities.Likewise, the result calculated in line 40 is even sillier becausethe product of the height of the square and the depth of theflagpole has absolutely no meaning in any real world physicalsystem we can think up.
Wouldn’t it be neat if we had a way to prevent such stupid thingsfrom happening in a large production program. If we had a goodprogram that defined all of the things we can do with a square andanother program that defined everything we could do with a pole,and if the data could be kept mutually exclusive, we could preventthese silly things from happening.
It should come as no real surprise to you that the next programwill do just those things for us and do it in a very elegant way.Before proceeding on to the next example program, you shouldcompile and execute this one even though it displays some sillyresults.
OBJECTS PROTECT DATA
Examine the program named CLASPOLE.CPP as an example of data protection in a very simpleprogram.
In this program the rectangle is changed to aclass with the same two variables which are now private, and twomethods to handle the private data. One method is used toinitialize the values of the objects created and the other methodto return the area of the object. The two methods are defined inlines 12 through 21 in the manner described earlier in thischapter. The pole is left as a structure to illustrate that thetwo can be used together and that C++ is truly an extension ofANSI-C.
In line 33 we declare two objects, once again named box and square,but this time we cannot assign values directly to their individualcomponents because they are private elements of the class. Lines36 through 38 are commented out for that reason and the messagesare sent to the objects in lines 40 and 41 to tell them toinitialize themselves to the values input as parameters. Theflag_pole is initialized in the same manner as in the previousprogram. Using the class in this way prevents us from making thesilly calculations we did in the last program. The compiler is nowbeing used to prevent the erroneous calculations. The end resultis that the stupid calculations we did in the last program are notpossible in this program so lines 50 through 53 have been commentedout. Once again, it is difficult to see the utility of this insuch a simple program. In a large program, using the compiler toenforce the rules can pay off in a big way.
Figure 5-2 is a graphical illustration of the two objects availablefor use within the calling program. Even though the square and thebox are both objects of class rectangle, their private data ishidden from each other such that neither can purposefully oraccidentally change the others data.
This is the abstract data type mentioned earlier in this chapter,a model with an allowable set of variables for data storage and aset of allowable operations that can be performed on that storeddata. The only operations that can be performed on the data arethose defined by the methods which prevents many kinds of erroneousor silly operations. Encapsulation and data hiding bind the dataand procedures, or methods, tightly together and limit the scopeand visibility of each. Once again, we have the divide and conquertechnique in which an object is separated from the rest of the codeand carefully developed in complete isolation from it. Only thenis it integrated into the rest of the code with a few very simpleinterfaces.