⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap08.htm

📁 Since the field of object oriented programming is probably new to you, you will find that there is a
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<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 8</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<B>C++ Tutorial - Chapter 8</B>

<P><B><FONT SIZE=+3>M</FONT><FONT SIZE=+2>ORE</FONT><FONT SIZE=+3> I</FONT><FONT SIZE=+2>NHERITANCE</FONT></B>

<P>In the last chapter we developed a model using modes of transportation
to illustrate the concept of inheritance. In this chapter we will use that
model to illustrate some of the finer points of inheritance and what it
can be used for. If it has been a while since you read and studied chapter
7, it would be good for you to return to that material and review it in
preparation for a more detailed study of the topic of inheritance.

<P><B>REORGANIZED FILE STRUCTURE</B>

<P>Example program ------> <B><A HREF="INHERIT1.CPP">INHERIT1.CPP</A></B>

<P>A close examination of the file named INHERIT1.CPP will reveal that
it is identical to the program developed in chapter 7 named ALLVEHIC.CPP
except that the program text is rearranged. The biggest difference is that
some of the simpler methods in the classes have been changed to inline
code to shorten the file considerably. In a practical programming situation,
methods that are this short should be programmed inline since the actual
code to return a simple value is shorter than the code required to send
a message to a non-inline method.

<P>The only other change is the reordering of the classes and associated
methods with the classes all defined first, followed by the main program.
This puts all class interface definitions on a single page to make the
code easier to study. The implementations for the methods are deferred
until the end of the file where they are available for quick reference
but are not cluttering up the class definitions which we wish to study
carefully in this chapter. This should be an indication to you that there
is considerable flexibility in the way the classes and methods can be arranged
in C++. Of course you realize that this violates the spirit of C++ and
its use of separate compilation, but is only done here for convenience.
The best way to package all of the example programs in this chapter is
like the packaging illustrated in chapter 7.

<P>As mentioned before, the two derived classes, <B>car </B>and <B>truck</B>,
each have a variable named <B>passenger_load</B> which is perfectly legal.
The <B>car </B>class has a method of the same name, <B>initialize()</B>,
as one declared in the super-class named <B>vehicle</B>. The rearrangement
of the files in no way voids this allowable repeating of names.

<P>After you have convinced yourself that this program is truly identical
to the program named ALLVEHIC.CPP from chapter 7, compile and execute it
with your compiler to assure yourself that this arrangement is legal. Due
to this means of code packaging, you will not need a "make" file or a "project"
capability to compile and execute this code. This code arrangement is designed
to make it easy to compile and execute the example programs in this chapter.

<P><B>THE SCOPE OPERATOR</B>

<P>Because the method <B>initialize()</B> is declared in the derived <B>car
</B>class, it hides the method of the same name which is part of the base
class, and there may be times you wish to send a message to the method
in the base class for use in the derived class object. This can be done
by using the scope operator in the following manner in the main program;
<PRE>&nbsp;&nbsp;&nbsp; sedan.vehicle::initialize(4, 3500.0);</PRE>
As you might guess, the number and types of parameters must agree with
those of the method in the base class because it will respond to the message.

<P><B>WHAT IS PROTECTED DATA?</B>

<P>If the data within a base class were totally available in all classes
inheriting that base class, it would be a simple matter for a programmer
to inherit the base class into a derived class and have free access to
all data in the parent class. This would completely override the protection
afforded by the use of information hiding. For this reason, the data in
a class are not automatically available to the methods of an inheriting
class. There are times when you may wish to automatically inherit all variables
directly into the subclasses and have them act just as though they were
declared as a part of those classes also. For this reason, the designer
of C++ has provided the keyword <B>protected</B>.

<P>In the present example program, the keyword <B>protected </B>is given
in line 6 so that all of the data of the <B>vehicle </B>class can be directly
imported into any derived classes but are not available outside of the
base class or derived classes. As we have mentioned before, all data are
automatically defaulted to <B>private </B>at the beginning of a class if
no specifier is given.

<P>You will notice that the variables named <B>wheels </B>and <B>weight
</B>are available to use in the method named <B>initialize() </B>in lines
83 through 88 just as if they were declared as a part of the <B>car </B>class
itself. They are available because they were declared <B>protected </B>in
the base class. Of course, they would be available here if they had been
decared <B>public </B>in the base class, but then they would be available
outside of both classes and we would lose our protection. Note that the
two variables are available for use in the base class as illustrated in
lines 77 and 78. We can now state the rules for the three means of defining
variables and methods.
<UL>
<LI>
<B>private </B>- The variables and methods are not available to any outside
calling routines, and they are not available to any derived classes inheriting
this class.</LI>

<LI>
<B>protected </B>- The variables and methods are not available to any outside
calling routines, but they are directly available to any derived class
inheriting this class.</LI>

<LI>
<B>public </B>- All variables and methods are freely available to all outside
calling routines and to all derived classes.</LI>
</UL>
You will note that these three means of definition can also be used in
a <B>struct </B>type. The only difference with a <B>struct </B>is that
everything defaults to <B>public </B>until one of the other keywords is
used.

<P>Be sure to compile and execute this program before continuing on to
the next example program.

<P><B>WHAT IS PRIVATE DATA?</B>

<P>Example program ------> <B><A HREF="INHERIT2.CPP">INHERIT2.CPP</A></B>

<P>Examine the file named INHERIT2.CPP where the data in the base class
is permitted to use the <B>private </B>default because line 6 is commented
out. In this program, the data is not available for use in the derived
classes, so the only way the data in the base class can be used is by sending
messages to methods in the base class, even within the derived class.

<P>It seems a little silly to have to call methods in the base class to
access the data which is actually a part of the derived class, but that
is the way C++ is defined to work. This would indicate to you that you
should spend some time thinking about how any class you define will be
used. If you think somebody may wish to inherit your class into a new class
and expand it, you should make the data members <B>protected </B>so they
can be easily used in the new class. Lines 86 and 87 are invalid now since
the members are not visible, but line 88 now does the job they did before
they were hidden by calling the public method of the base class. Line 104
is also changed because of the hidden data. You will notice that the data
is still available in lines 77 and 78 just as they were before because
the member variables are protected in the <B>vehicle </B>class. Be sure
to compile and execute this program.

<P><B>HIDDEN METHODS</B>

<P>Example program ------> <B><A HREF="INHERIT3.CPP">INHERIT3.CPP</A></B>

<P>Examine the file named INHERIT3.CPP carefully and you will see that
it is a repeat of the first example program in this chapter with a few
minor changes.

<P>You will notice that the derived classes named <B>car </B>and <B>truck
</B>do not have the keyword <B>public </B>prior to the name of the base
class in the first line of each class declaration. The keyword <B>public</B>,
when included prior to the base class name, makes all of the methods defined
in the base class available for use in the derived class at the same security
level as they were defined at in the base class. Therefore, in the previous
program, we were permitted to call the methods defined as part of the base
class from the main program even though we were working with an object
of one of the derived classes.

<P>In this program, all entities are inherited as <B>private </B>due to
the use of the keyword <B>private </B>prior to the name of the base class.
They are therefore unavailable to any code outside of the derived class.
The general rule is that all elements are inherited into the derived class
at the most restrictive of the two restrictions placed on them, one being
the definition in the base class and the other being the restriction on
inheritance. This defines the way the elements are viewed outside of the
derived class.

<P>The elements are all inherited into the derived class such that they
have the same level of protection they had in the base class, as far as
their visibility restrictions within the derived class. We have returned
to the use of <B>protected </B>data instead of <B>private </B>in the base
class, therefore the member variables are available for use within the
derived class.

<P>In the present program, the only methods available for objects of the
<B>car </B>class, are those that are defined as part of the class itself,
and therefore we only have the methods named <B>initialize()</B> and <B>passengers()
</B>available for use with objects of class <B>car</B>.

<P>When we declare an object of type <B>car</B>, according to the definition
of the C++ language, it contains three variables. It contains the one defined
as part of its class named <B>passenger_load</B> and the two that are part
of its parent class, <B>wheels </B>and <B>weight</B>. All are available
for direct use within its methods because of the use of the keyword <B>protected
</B>in the base class. The variables are a part of an object of class <B>car
</B>when it is declared and are stored as part of the object.

<P>The observant student will notice that several of the output statements
have been commented out of the main program since they are no longer legal
or meaningful operations. Lines 57 through 59 have been commented out because
the methods named <B>get_weight()</B> and <B>wheel_loading()</B> are not
available as members of the <B>car </B>class because we are using <B>private
</B>inheritance. You will notice that <B>initialize() </B>is still available
but this is the one in the <B>car </B>class, not the method of the same
name in the <B>vehicle </B>class.

<P><B>USING THE TRUCK CLASS</B>

<P>Moving on to the use of the <B>truck </B>class in the main program,
we find that lines 63 and 65 are commented out for the same reason as given
above, but lines 66 and 67 are commented out for an entirely different
reason. Even though the method named <B>efficiency()</B> is available and
can be called as a part of the <B>truck </B>class, it cannot be used because
we have no way to initialize the <B>wheels </B>or <B>weight </B>of the
<B>truck </B>object. We can get the <B>weight </B>of the <B>truck </B>object,
as we have done in line 102, but since the <B>weight </B>has no way to
be initialized, the result is meaningless and lines 66 and 67 are commented
out.

<P><B>Private </B>inheritance is very similar to using an embedded object
and, in fact, is very rarely used. Until you gain a lot of experience with
C++ and the proper use of Object Oriented Programming, you should use <B>public
</B>inheritance exclusively. There is probably no reason to ever use <B>private
</B>or <B>protected </B>inheritance. They were probably added to the language

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -