📄 chap07.htm
字号:
<P><B>ANOTHER DERIVED CLASS</B>
<P>Example program ------> <B><A HREF="TRUCK.H">TRUCK.H</A></B>
<P>Examine the file named TRUCK.H for an example of another class that
uses the <B>vehicle </B>class and adds to it. Of course, it adds different
things to it, because it will specialize in those things that pertain to
trucks. In fact, it adds two more variables and three more methods. Once
again, ignore the keyword <B>public </B>following the colon in line 7 for
a few minutes and we will cover it in detail in the next chapter of this
tutorial. See figure 7-3 for a graphical representation of the <B>truck
</B>class.
<P><IMG SRC="CPP0703.GIF" HSPACE=20 VSPACE=20 BORDER=0 HEIGHT=259 WIDTH=474>
<P>A very important point that must be made is that the <B>car </B>class
and the <B>truck </B>class have absolutely nothing to do with each other,
they only happen to be derived classes of the same base class or parent
class as it is sometimes called.
<P>Note that both the <B>car </B>and the <B>truck </B>classes have methods
named <B>passengers() </B>but this causes no problems and is perfectly
acceptable. If classes are related in some way, and they certainly are
if they are both derived classes of a common base class, you would expect
them to be doing somewhat similar things. In this situation there is a
good possibility that a method name would be repeated in both child classes.
<P><B>THE TRUCK IMPLEMENTATION</B>
<P>Example program ------> <B><A HREF="TRUCK.CPP">TRUCK.CPP</A></B>
<P>Examine the file named TRUCK.CPP for the implementation of the <B>truck
</B>class. It has nothing unusual included in it.
<P>You should have no problem understanding this implementation. Your assignment
at this point is to compile it in preparation for our example program that
uses all three of the classes defined in this chapter.
<P><B>USING ALL THREE CLASSES</B>
<P>Example program ------> <B><A HREF="ALLVEHIC.CPP">ALLVEHIC.CPP</A></B>
<P>Examine the program named ALLVEHIC.CPP for an example that uses all
three of the classes we have been discussing in this chapter. It uses the
parent class <B>vehicle </B>to declare objects and also uses the two child
classes to declare objects. This was done to illustrate that all three
classes can be used in a single program.
<P>All three of the header files for the classes are included in lines
3 through 5 so the program can use the components of the classes. Notice
that the implementations of the three classes are not in view here and
do not need to be in view. This allows the code to be used without access
to the source code for the actual implementation of the class. However,
it should be clear that the header file definition must be available.
<P>In this example program, only one object of each class is declared and
used but as many as desired could be declared and used in order to accomplish
the programming task at hand. You will notice how clean and uncluttered
the source code is for this program. The classes were developed, debugged,
and stored away previously, and the interfaces were kept very simple. There
is nothing new here so you should have no trouble understanding the operation
of this program.
<P>Compiling and executing this program will take a bit of effort but the
process is not complicated. The three classes and the <B>main() </B>program
can be compiled in any order desired. All four must be compiled prior to
linking the four resulting object (or binary) files together. Finally,
you can execute the complete program. Be sure you do the required steps
to compile and execute this program because the effective use of C++ will
require you to compile many separate files and link them together. This
is because of the nature of the C++ language, but it should not be a burden
if a good "make" capability exists with your compiler. If you are using
an implementation of C++ that has a "project" capability, it will make
this a snap.
<P><B>WHY THE #ifndef VEHICLE_H ?</B>
<P>We promised to return to the strange looking preprocessor directive
in lines 4, 5 and 19 in the VEHICLE.H file, and this is the time for it.
When we define the derived class <B>car</B>, we are required to supply
it with the full definition of the interface to the <B>vehicle </B>class
since <B>car </B>is a derived class of <B>vehicle </B>and must know all
about its parent. We do that by including the <B>vehicle </B>class into
the <B>car </B>class, and the <B>car </B>class can be compiled. The <B>vehicle
</B>class must also be included in the header file of the <B>truck </B>class
for the same reason.
<P>When we get to the ALLVEHIC.CPP program, we must inform it of the details
of all three classes, so all three header files must be included as is
done in lines 3 through 5 of ALLVEHIC.CPP, but this leads to a problem.
When the preprocessor gets to the <B>car </B>class, it includes the <B>vehicle
</B>class because it is listed in the <B>car </B>class header file, but
since the <B>vehicle </B>class was already included in line 3 of ALLVEHIC.CPP,
it is included twice and we attempt to redeclare the class <B>vehicle</B>.
Of course it is the same declaration, but the system doesn't care, it simply
doesn't allow redeclaration of a class. We allow the double inclusion of
the file and at the same time prevent the double inclusion of the class
by building a bridge around it using the word VEHICLE_H. If the word is
already defined, the declaration is skipped, but if the word is not defined,
the declaration is included and VEHICLE.H is defined at that time. The
end result is the actual inclusion of the class only once, even though
the file is included more than once. You should have no trouble understanding
the logic of the includes if you spend a little time studying this program
sequence.
<P>Even though ANSI-C allows multiple definitions of entities, provided
the definitions are identical, C++ does not permit this. The primary reason
is because the compiler would have great difficulty in knowing if it has
already made a constructor call for the redefined entity, if there is one.
A multiple constructor call for a single object could cause great havoc,
so C++ was defined to prevent any multiple constructor calls by making
it illegal to redefine any entity. This is not a problem in any practical
program.
<P>The name VEHICLE_H was chosen as the word because it is the name of
the file, with the period replaced by the underline. If the name of the
file is used systematically in all of your class definitions, you cannot
have a name clash because the filename of every class must be unique provided
you keep all files in the same directory. It would be good for you to get
into the practice of building the optional skip around all of your class
headers. All class definition files in the remainder of this tutorial will
include this skip around to prevent multiple inclusions and to be an example
for you. You should get into the practice of adding the skip around to
all of your class headers no matter how trivial they may seem to be.
<P><B>OUR FIRST PRACTICAL INHERITANCE</B>
<P>Example program ------> <B><A HREF="NEWDATE.H">NEWDATE.H</A></B>
<P>Continuing where we started in chapter 5, we will inherit the <B>date
</B>class into the file named NEWDATE.H and add a member variable and a
new method to the class. Actually, this is not a good way to add the <B>day_of_year</B>
to the <B>date </B>class since it is available in the structure returned
from the system call in the <B>date </B>class. However, we are more interested
in illustrating inheritance in a practical example than we are in developing
a perfect class, so we will live with this inefficiency. You will note
that we add one variable and one method to create our new class.
<P>Example program ------> <B><A HREF="NEWDATE.CPP">NEWDATE.CPP</A></B>
<P>The program named NEWDATE.CPP contains the implementation for the added
method and should be easy for the student to understand. This class implementation
uses the array <B>days[]</B> from the <B>date </B>class implementation
since it was defined as a global variable there. The method named <B>get_time_of_day()</B>
involves very simple logic. It doesn't even adjust for leap years. Once
again, we are not really interested in writing a good <B>date </B>class,
but in learning the mechanics of inheritance.
<P>Example program ------> <B><A HREF="TRYNDATE.CPP">TRYNDATE.CPP</A></B>
<P>Finally, the example program named TRYNDATE.CPP will use the new class
in a very simple way to illustrate that the derived class is as easy to
use as the base class and in fact the main program has no way of knowing
that it is using a derived class.
<P>You should compile and link this program to gain the experience of doing
so. Remember that it will be necessary to link in the object code for the
original <B>date </B>class as well as the object code from the <B>newdate
</B>class and the <B>main()</B> program.
<P><B>PROGRAMMING EXERCISES</B>
<OL>
<LI>
Add another object of the <B>vehicle </B>class to ALLVEHIC.CPP named <B>bicycle</B>,
and do some of the same operations as were done to the <B>unicycle</B>.
You will only need to recompile the <B>main() </B>program and link all
four files together to get an executable file, the three classes will not
require recompilation.</LI>
<LI>
Add a new method to the <B>truck </B>class to return the total weight of
the truck plus its payload and add code to ALLVEHIC.CPP to read the value
out and display it on the monitor. This will require an addition to TRUCK.H,
another addition to TRUCK.CPP, and of course the changes to the main program
named ALLVEHIC.CPP. The answer is given as three files named CH07_3A.H
(TRUCK.H), CH07_3B.CPP (TRUCK.CPP) and the changed <B>main()</B> program
is found in CH07_3C.CPP in the answers for this tutorial.</LI>
<LI>
Add a variable named <B>sex </B>of type <B>char </B>to the <B>name </B>class
you developed in chapter 5 as well as methods to set and retrieve the value
of this variable. The only legal inputs are 'M' or 'F'. These additions
should be done by inheriting the <B>name </B>class into the new class.</LI>
</OL>
<A HREF="chap08.htm">Advance to Chapter 8</A>
<P><A HREF="cpplist.htm">Return to the Table of Contents</A>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -