|
Table of Contents
Learning C++:
An Index of Entry Points
2. The A reference document on the basic elements of C++.
3. The Patterns
|
A. Programmer Supplied Header Files With classes the separations are usually more distinct. It is common
practice to put the class declaration in one file, the class definition in
a second file, and the program that uses the class in a third file. If
there is more than one class in a program, each class gets its own
declaration file and definition file. (Some programmers combine a number
of class declarations in one file but the reasons for that are usually
based on issues we have not discussed here.) All the code we have written so far in this chapter
represents a class declaration and should be in one file. As you may
recall from our earlier work, all files should have at least two comment
lines at the top providing the name of the file and a brief description of
what the file is about. By convention, files that declare classes use the
file extension '.h' - for 'header' file. We have already used one header
file in the programs we wrote in earlier chapters - "iostream.h". That
file was provided by the compiler distributors and we included it by using
the "#include" directive followed by the file name surrounded by angle
brackets, <...>. Its purpose is to declare the classes used for
input and output. ]
Now that we are ready to write the definition for the Contract class,
we need to open a new file. As usual this file begins with a set of
comments describing what the file contains and its name:
// Definitions for the class Contract Note that the file has the same name as the file containing the class
declarations except that the extension is different. We use the .cpp
extension because this file contains instructions for the individual
functions. Since the specifications for the problem go in the file
containing 'main' they do not appear here or in the '.h' file. From what
we have learned already, what comes next is the line or lines to 'include'
any necessary files. In our analysis above we decided to not have any of
the functions provide for input or output. For that reason, we do not need
to have the line:
in our code. However, there is one header file we do need to include.
Our definitions for the class 'contract' are going to use the various
elements of the 'contract' declaration and, therefore, that information
needs to be available in this file. To make it available we need to
include the header file for the contract class - the file we called
'contract.h'. This is accomplished using the code: You can think of the 'include' directive as telling the computer
to copy in the code found in the file 'contract.h'. Note that the file
name in the code is surrounded by double quote marks. This is to indicate
that this is a programmer supplied header file and not a built-in one such
as 'iostream.h'. You might ask then why we don't just have one file that has the
declarations and the definitions as we did in our previous programs. For
simple programs such as the one we are presently working on we could do
exactly that. However, this would not be wise in more complex programming
situations. First, we have seen the importance of breaking our work up
into pieces. For programs of thousands, hundreds of thousands, even
millions of lines of code, having one file for the whole program would
simply be unworkable. Second, we would like to have our code be re-usable.
If someone writes a piece of code that provides specific functionality, it
would be smart to include that code in any program that requires that
functionality without including unnecessary additional code. Indeed, such
re-usability is supposed to be a key feature of the object oriented
approach. Once a class is declared and defined, it should be possible to
use the code for that class in any programming problem requiring that
class. Third, when we break a large program up into separate files, the code
in some of those files may need to know about some class. Thus, we may
need to include the declaration code for that class in each of those
files. We would not want, however, to include the class definitions in
each file because, if we did so, the final, combined program would have
more than one definition for the same member functions. This is illegal.
B. Defining Member Functions The 'change' functions all are 'one-liners', for example: void Contract::ChangeSquareFootage(int sqFootage) Note that aside from placing "Contract::" between the return type and
the function name, this looks like any other function definition. The
"Contract::" tells the compiler that this function is a member of the
class 'Contract'.
The 'provide' functions are also one liners as in:
A few points about this member function definition:
Here then is the completed class definition:
|