USING A NORMAL VARIABLE

There is another variable named piggy declared and used throughout this example program that illustrates that a normal variable can be intermixed with the objects and used in the normal manner. These of this variable should pose no problem to you, so after you understand the program, be sure to compile and execute it. It would 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 be clear to you that it really does anything worthwhile until we study the next two programs. Be sure to compile and execute this program before 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 of encapsulation.

We have two structures declared, one being a rectangle and the other being a pole. The data fields should be self explanatory with the exception of the depth of the flagpole which is the depth it 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 problem understanding exactly what this program is doing, but you may bea bit confused at the meaning of the result found in line 38 where we 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 has no earthly meaning because the data are for two different entities.Likewise, the result calculated in line 40 is even sillier because the product of the height of the square and the depth of theflagpole has absolutely no meaning in any real world physical system we can think up.

Wouldn’t it be neat if we had a way to prevent such stupid things from happening in a large production program. If we had a good program that defined all of the things we can do with a square and another program that defined everything we could do with a pole,and if the data could be kept mutually exclusive, we could prevent these silly things from happening.

It should come as no real surprise to you that the next program will do just those things for us and do it in a very elegant way.Before proceeding on to the next example program, you should compile and execute this one even though it displays some silly results.

OBJECTS PROTECT DATA

Examine the program named CLASPOLE.CPP as an example of data protection in a very simple program.

In this program the rectangle is changed to a class with the same two variables which are now private, and two methods to handle the private data. One method is used to initialize the values of the objects created and the other method to return the area of the object. The two methods are defined in lines 12 through 21 in the manner described earlier in this chapter. The pole is left as a structure to illustrate that the two 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.