📄 chap07.htm
字号:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Gordon Dodrill">
<META NAME="GENERATOR" CONTENT="Mozilla/4.04 [en] (Win95; I) [Netscape]">
<TITLE>C++ Tutorial - Chapter 7</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<B>C++ Tutorial - Chapter 7</B>
<P><B><FONT SIZE=+3>I</FONT><FONT SIZE=+2>NHERITANCE</FONT></B>
<P>One reason to use inheritance is that it permits you to reuse code from
a previous project, but gives you the flexibility to slightly modify it
if the old code doesn't do exactly what you need for the new project. It
doesn't make sense to start every new project from scratch since some code
will certainly be repeated in several programs and you should strive to
build on what you did previously. However, it is easy to make an error
if you try to modify the original class. You are less likely to make an
error if you leave the original alone and only add to it. Another reason
for using inheritance is if the project requires the use of several classes
which are very similar but slightly different.
<P>In this chapter we will concentrate on the mechanism of inheritance
and how to build it into a program. A better illustration of why you would
use inheritance will be given in later chapters where we will discuss some
practical applications of object oriented programming. The principle of
inheritance is available with several modern programming languages and
is handled slightly differently with each. C++ allows you to inherit all
or part of the members and methods of a class, modify some, and add new
ones not available in the parent class. You have complete flexibility,
and as usual, the method used with C++ has been selected to result in the
most efficient code execution.
<P><B>A SIMPLE CLASS TO START WITH</B>
<P>Example program ------> <B><A HREF="VEHICLE.H">VEHICLE.H</A></B>
<P><IMG SRC="CPP0701.GIF" HSPACE=20 VSPACE=20 BORDER=0 HEIGHT=209 WIDTH=260>
<BR>Examine the file named VEHICLE.H for a simple class which we will use
to begin our study of inheritance. There is nothing unusual about this
class header, it has been kept very simple. It consists of four simple
methods which can be used to manipulate data pertaining to our vehicle.
What each method does is not especially important at this time. We will
eventually refer to this as a base class or parent class, but for the time
being, we will simply use it like any other class to show that it is indeed
identical to the classes already studied. Note that we will explain the
added keyword <B>protected </B>shortly. Figure 7-1 is a graphical representation
of the <B>vehicle </B>class.
<P>Ignore lines 4, 5, and 19 until the end of this chapter where they will
be explained in detail. This file cannot be compiled or executed because
it is only a header file.
<P><B>THE IMPLEMENTATION FOR VEHICLE</B>
<P>Example program ------> <B><A HREF="VEHICLE.CPP">VEHICLE.CPP</A></B>
<P>Examine the file named VEHICLE.CPP and you will find that it is the
implementation of the <B>vehicle </B>class. The <B>initialize() </B>method
assigns the values input as parameters to the <B>wheels </B>and <B>weight
</B>variables. We have methods to return the number of <B>wheels </B>and
the <B>weight</B>, and finally, we have one that does a trivial calculation
to return the loading on each wheel. We will have a few examples of methods
that do some significant processing later, but at this point, we are more
interested in learning how to set up the interface to the classes, so the
implementations will be kept trivial.
<P>As stated above, this is a very simple class which will be used in the
next program. Later in this tutorial we will use it as a base class. You
should compile this class at this time in preparation for the next example
program, but you cannot execute it because there is no entry point.
<P><B>USING THE VEHICLE CLASS</B>
<P>Example program ------> <B><A HREF="TRANSPRT.CPP">TRANSPRT.CPP</A></B>
<P>The file named TRANSPRT.CPP uses the <B>vehicle </B>class in exactly
the same manner as we illustrated in the last chapter. This should be an
indication to you that the <B>vehicle </B>class is truly nothing more than
a normal class as defined in C++. We will make it a little special, however,
by using it unmodified as a base class in the next few example files to
illustrate inheritance. Inheritance uses an existing class and adds functionality
to it to accomplish another, possibly more complex job.
<P>You should have no problem understanding the operation of this program.
It declares four objects of the <B>vehicle </B>class, initializes them,
and prints out a few of the data values to illustrate that the <B>vehicle
</B>class can be used as a simple class because it is a simple class. We
are referring to it as a simple class as opposed to calling it a base class
or derived class as we will do shortly.
<P>If you thoroughly understand this program, you should compile and execute
it, remembering to link the <B>vehicle </B>object file with this object
file.
<P><B>OUR FIRST DERIVED CLASS</B>
<P>Example program ------> <B><A HREF="CAR.H">CAR.H</A></B>
<P>Examine the file named CAR.H for our first example of the use of a derived
class or child class. The <B>vehicle </B>class is inherited due to the
<B>": public vehicle"</B> added to line 7. This derived class named <B>car
</B>is composed of all of the information included in the base class <B>vehicle</B>,
and all of its own additional information. Even though we did nothing to
the class named <B>vehicle</B>, we made it into a base class because of
the way we are using it here. To go a step further, even though it will
be used as a base class in an example program later in this chapter, there
is no reason it cannot continue to be used as a simple class in the previous
example program. In fact, it can be used as a simple class and a base class
in the same program. The question of whether it is a simple class or a
base class is answered by the way it is used.
<P>A discussion of terminology is needed here. When discussing object oriented
programming in general, a class that inherits another is often called a
derived class or a child class, but the most proper term as defined for
C++, is a derived class. Since these terms are very descriptive, and most
writers tend to use the terms interchangeably, we will also use these terms
in this tutorial. Likewise the proper C++ terminology for the inherited
class is to call it a base class, but parent class and super class are
sometimes used also.
<P>A base class is a rather general class which can cover a wide range
of objects, whereas a derived class is somewhat more restricted but at
the same time more useful. For example if we had a base class named programming
language and a derived class named C++, then we could use the base class
to define Pascal, Ada, C++, or any other programming language, but it would
not tell us about the use of classes in C++ because it can only give a
general view of each language. On the other hand, the derived class named
C++ could define the use of classes, but it could not be used to describe
the other languages because it is too narrow. A base class tends to be
more general, and a derived class is more specific.
<P>In this case, the <B>vehicle </B>base class can be used to declare objects
that represent trucks, cars, bicycles, or any number of other vehicles
you can think up. The class named <B>car </B>however can only be used to
declare an object that is of type <B>car </B>because we have limited the
kinds of data that can be intelligently used with it. The <B>car </B>class
is therefore more restrictive and specific than the <B>vehicle </B>class.
The <B>vehicle </B>class is more general than the car class.
<P>If we wished to get even more specific, we could define a derived class
using <B>car </B>as the base class, name it <B>sports_car</B>, and include
such information as <B>red_line_limit</B> for the tachometer which would
be silly for the family station wagon. The <B>car </B>class would therefore
be used as a derived class and a base class at the same time, so it should
be clear that these names refer to how a class is used.
<P><B>HOW DO WE DECLARE A DERIVED CLASS?</B>
<P>Enough generalities about classes, let's get down to the specifics.
A derived class is defined by including the header file for the base class
as is done in line 5, then the name of the base class is given following
the name of the derived class separated by a colon as is illustrated in
line 7. Ignore the keyword <B>public </B>immediately following the colon
in this line. This defines public inheritance and we will study it in detail
in the next chapter. All objects declared as being of class <B>car </B>therefore
are composed of the two variables from the class <B>vehicle </B>because
they inherit those variables, and the single variable declared in the class
<B>car </B>named <B>passenger_load</B>.
<P><IMG SRC="CPP0702.GIF" HSPACE=20 VSPACE=20 BORDER=0 HEIGHT=221 WIDTH=478>
<P>An object of this class will have three of the four methods of <B>vehicle
</B>and the two new ones declared here. The method named <B>initialize()</B>
which is part of the <B>vehicle </B>class will not be available here because
it is hidden by the local version of <B>initialize()</B> which is a part
of the <B>car </B>class. The local method will be used if the name is repeated
allowing you to customize your new class. Figure 7-2 is a graphical representation
of an object of this class.
<P>Note once again that the implementation for the base class only needs
to be supplied in its compiled form. The source code for the implementation
can be hidden for economic reasons to aid software developers. Hiding the
source code also allows the practice of information hiding. The header
for the base class must be available as a text file since the class definitions
are required in order to use the class.
<P><B>THE CAR CLASS IMPLEMENTATION</B>
<P>Example program ------> <B><A HREF="CAR.CPP">CAR.CPP</A></B>
<P>Examine the file named CAR.CPP which is the implementation file for
the <B>car </B>class. The first thing you should notice is that this file
has no indication of the fact that it is a derived class of any other file,
that can only be determined by inspecting the header file for the class.
Since we can't tell if it is a derived class or not, it is written in exactly
the same way as any other class implementation file.
<P>The implementations for the two new methods are written in exactly the
same way as methods are written for any other class. If you think you understand
this file, you should compile it for later use.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -