The method in line 10 contains the implementation for the method as a part of the declaration because it is very simple, and because it introduces another new topic which you will use often in C++ programming. When the implementation is included in the declaration, it will be assembled inline wherever this function is called leading to much faster code. This is because there is no overhead to accomplish the call to the method. In some cases this will lead to code that is both smaller and faster. This is yet another illustration of the efficiency built into the C++ programming language.

Compile and execute this program in preparation for our study of the next three examples which are a repeat of this program in a slightly different form.


If you examine BOX.H carefully, you will see that it is only the class definition. No details are given of how the various methods are implemented except of course for the inline method named get_area(). This gives the complete definition of how to use the class with no implementation details. You would be advised to keep a hardcopy of this file available as we study the next two files. You will notice that it contains lines 4 through 12 of the previous example program named BOXES1.CPP.

This is called the class header file and cannot be compiled or executed.


Examine the file named BOX.CPP for the implementation of the methods declared in the class header file. Notice that the class header file is included into this file in line 2 which contains all of the prototypes for its methods. The code from lines 15 through 34 of BOXES1.CPP is contained in this file which is the implementation of the methods declared in the class named box.

This file can be compiled but it cannot be executed because there is no main entry point which is required for all ANSI-C or C++ programs. When it is compiled, the object code will be stored in the current directory and available for use by other programs. It should be noted here that the result of compilation is usually referred to as an object file because it contains object code. This use of the word object has nothing to do with the word object as used in object oriented programming. It is simply a matter of overloading the use of the word. The practice of referring to the compiled result as an object file began long before the method of object oriented programming was ever considered.

The separation of the definition and the implementation is a major step forward in software engineering. The definition file is all the user needs in order to use this class effectively in a program. He needs no knowledge of the actual implementation of the methods. If he had the implementation available, he may study the code and find a trick he could use to make the overall program slightly more efficient, but this would lead to nonportable software and possible bugs later if the implementor changed the implementation without changing the interface. The purpose of object oriented programming is to hide the implementation in such a way that the implementation can not affect anything outside of its own small and well defined boundary or interface.

You should compile this implementation file now and we will use the result with the next example program.