Significance of Classes

In C++ a class definition can be placed into a header file. For example, suppose you defined a class called BankAccount and placed the class's definition into the header file: bank.h. This header file could then be attached to the program with a #include using the following statement:

image from book

 #include "bank.h" 

image from book

Notice in the above statement that a .h was attached to the end of the header file name. This is not required but is traditional for non system headers.

Notice also those quotation marks were used instead of < and > as with system headers like iostream and string that were used previously. These quotation marks indicate that the compiler can find this header file in the same folder as the program source file. The < and > are used when the header file is in the compiler's default system header folder.

When the statement above is used in a program, the compiler looks for the header in the same folder as the location of the C++ source file. If a company wants to use header files for different programs, then the header files can be placed in a company folder. In such a case there needs to be redirection in the statement to the company folder in question (also called the component library).

See example bnkwhedr.cpp and header bank.h. When the program bnkwhedr.cpp is compiled, the header file bank.h will be attached to the program during the preprocessor process. Take these two files and place them into the compiler. Store bnkwhedr.cpp as the C++ program and bank.h as a header. Compile and run the program. Open Windows Explorer and look at the project's folder to observer where the header file is located.

A second approach to handling classes is to place the class definitions into a header .h file while placing the method definitions into a separate .cpp source file. For example look at invclass2.cpp, date.h, invoice.h, date.cpp and invoice.cpp. Place these files into the compiler, compile and run them.

When the above approach is used, the .h files are added as header files and the .cpp files are added as source files. Look in particular at the file date.h and its supporting file: date.cpp. Notice that the definition of the class Date is in the header file date.h and the method definitions are in the file date.cpp. Similarly notice that the definition of the class Invoice is in the file invoice.h and the method definitions of this class are in the file invoice.cpp. Open Windows Explorer and look at the project's folder to observer where the header files are located.

Notice that since no reference was made to the class Date in the program invclass2.cpp and that only the class Invoice was referenced only the header invoice.h was included in the program invclass2.cpp. Someone looking at the code of the program would not know that the class Date was even used.

The files DATE.CPP and INVOICE.CPP required their respective header files so the header files were included into these files.

Since it is possible that in a major program, header files like date.h and invoice.h could be required more than once, the program must ensure that these classes are defined at most once. To ensure this, the preprocessor command: #ifndef and its closing command #endif were used in each header file along with #define . The first time the headers are called, the terms have not been defined so these terms are defined as are the classes. The next time the header files are called by the compiler these terms are already defined so the classes are not defined a second time.

One of the advantages of using this technique is that the header files: bank.h, date.h and invoice.h as well as the supporting files: date.cpp and invoice.cpp could therefore be used in multiple programs. In fact some companies create header files and supporting files of different classes and then provide these header files to others to be added to their programs. These files are frequently kept in a company component library.

Whether you realize it or not, you have been using C++ classes from your very first C++ program. Built into the header iostream are several classes including ios, streambuf, istream, ostream and iostream. The cin is an object of the istream class and the cout is an object of the ostream class. The development of numerous class libraries should eventually make programming faster because these libraries will be used rather than recoding the same class libraries again and again. For example the Windows' class libraries contain slide bars, push buttons, drop down menus, icon activators, etc. The programmer does not therefore have to code these constructs only call them.

 Note:  The classes Date and Invoice above had their definitions and their method definitions in separate files. While this is true in this particular example, these two definition files and the two header files could have been combined into just one header file and one method definition file.

Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
Year: 2007
Pages: 142 © 2008-2017.
If you may any questions please contact us: