USING THE BOX OBJECT

Examine the file named BOXES2.CPP and you will find that the class we defined previously is used within this file. In fact, these last three programs taken together are identical to the program named BOXES1.CPP studied earlier.

The BOX.H file is included here, in line 3, since the definition of the box class is needed to declare three objects and use their methods. You should have no trouble seeing that this is a repeat of the previous program and will execute in exactly the same way. There is a big difference in BOXES1.CPP and BOXES2.CPP as we will see shortly.

A very important distinction must be made at this point. We are not merely calling functions and changing the terminology a little to say we are sending messages. There is an inherent difference in the two operations. Since the data for each object is tightly bound up in the object, there is no way to get to the data except through the methods and we send a message to the object telling it to perform some operation based on its internally stored data. However, whenever we call a function, we take along the data for

it to work with as parameters since it doesn’t contain its own data.

Be sure to compile and execute this program, but when you come to the link step, you will be required to link this program along with the result of the compilation when you compiled the class named box. The file is probably named BOX.OBJ that must be linked with this file. You may need to consult the documentation for your C++ compiler to learn how to do this. Even if it seems to be a lot of trouble to learn how to link several files together, it will be worth your time to do so now because we will be linking several more multifile C++ programs in the remainder of this tutorial.

If you are using Turbo C++, this is your first opportunity to use a project file. If you are using Zortech C++ or one of the other implementations, you can use the “make” facility included with your compiler. Regardless of which C++ compiler you are using, it would pay you to stop and learn how to use the multifile technique provided with your compiler because you will need to use it several times before the end of this tutorial. The nature of C++ tends to drive the programmer to use many files for a given programming project and you should develop the habit early.

INFORMATION HIDING

The last three example programs illustrate a method of information hiding that can have a significant impact on the quality of software developed for a large project. Since the only information the user of the class really needs is the class header, that is all he needs to be given. The details of implementation can be kept hidden from him to prevent him from studying the details and possibly using a quirk of programming to write some rather obtuse code. Since he doesn’t know exactly what the implementor did, he must follow only the definition given in the header file. This can have a significant impact on a large project. As mentioned earlier, accidental corruption of data is prevented also.

Another reason for hiding the implementation is economic. The company that supplied you with your C++ compiler gave you many library functions but did not supply the source code to the library functions, only the interface to each function. You know how to use the file access functions but you do not have the details of implementation, nor do you need them. Likewise a class library industry can develop which supplies users with libraries of high quality, completely developed and tested classes, for a licensing fee of course. Since the user only needs the interface defined, he can be supplied with the interface and the object (compiled) code for the class and can use it in any way he desires. The suppliers source code is protected from accidental or intentional compromise and he can maintain complete control over it.

It is very important that you understand the principles covered in this chapter before proceeding on to the next chapter. If you feel you are a little weak in any of the areas covered here, you should go over them again before proceeding on. A point that should be made here that may be obvious to you, is that it requires some amount of forethought to effectively use classes.

Share
Tweet
Share
Pin