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

📄 chap09.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 9</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<B>C++ Tutorial - Chapter 9</B>

<P><B><FONT SIZE=+3>M</FONT><FONT SIZE=+2>ULTIPLE</FONT><FONT SIZE=+3>
I</FONT><FONT SIZE=+2>NHERITANCE</FONT><FONT SIZE=+3> A</FONT><FONT SIZE=+2>ND</FONT><FONT SIZE=+3>
F</FONT><FONT SIZE=+2>UTURE </FONT><FONT SIZE=+3>D</FONT><FONT SIZE=+2>IRECTIONS</FONT></B>

<P>Multiple inheritance is the ability to inherit data and methods from
more than one base class into a derived class. Multiple inheritance and
a few of the other recent additions to the language will be discussed in
this chapter along with some of the expected future directions of the language.

<P>Several companies have C++ compilers available in the marketplace, and
others are sure to follow. Because the example programs in this tutorial
are designed to be as generic as possible, most should be compilable with
any good quality C++ compiler provided it follows the AT&amp;T definition
of version 2.1 or newer. Many of these examples will not work with earlier
definitions because the language was significantly changed with the version
2.1 update.

<P>After completing this tutorial, you should have enough experience with
the language to study additional new constructs on your own as they are
implemented by the various compiler writers. We will update the entire
tutorial as soon as practical following procurement of any new compiler,
but hopefully the language will not change rapidly enough now to warrant
an update oftener than semi-annually.

<P><B>MULTIPLE INHERITANCE</B>

<P>A major recent addition to the C++ language is the ability to inherit
methods and variables from two or more parent classes when building a new
class. This is called multiple inheritance, and is purported by many people
to be a major requirement for an object oriented programming language.
Some writers, however, have expressed doubts as to the utility of multiple
inheritance. To illustrate the validity of this, it was not easy to think
up a good example of the use of multiple inheritance as an illustration
for this chapter. In fact, the resulting example is sort of a forced example
that really does nothing useful. It does however, illustrate the mechanics
of the use of multiple inheritance with C++, and that is our primary concern
at this time.

<P>The biggest problem with multiple inheritance involves the inheritance
of variables or methods with duplicated names from two or more parent classes.
Which variable or method should be chosen as the inherited variable or
method if two or more have the same name? This will be illustrated in the
next few example programs.

<P><B>SIMPLE MULTIPLE INHERITANCE</B>

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

<P>An examination of the file named MULTINH1.CPP will reveal the definition
of two very simple classes in lines 4 through 29 named <B>moving_van</B>
and <B>driver</B>.

<P>In order to keep the program as simple as possible, all of the member
methods are defined as <B>inline </B>functions. This puts the code for
the methods where it is easy to find and study. You will also notice that
all variables in both classes are declared to be <B>protected </B>so they
will be readily available for use in any class that inherits them. The
code for each class is kept very simple so that we can concentrate on studying
the interface to the methods rather than spending time trying to understand
complex methods. As mentioned previously, chapter 12 will illustrate the
use of non-trivial methods.

<P>Beginning in line 32, we define another class named <B>driven_truck</B>
which inherits all of the data and all of the methods from both of the
previously defined classes. In the last two chapters, we studied how to
inherit a single class into another class, and to inherit two or more classes,
the same technique is used except that we use a list of inherited classes
separated by commas as illustrated in line 32. The observant student will
notice that we use the keyword <B>public </B>prior to the name of each
inherited class in order to be able to freely use the methods within the
subclass. In this case, we didn't define any new variables, but we did
introduce two new methods into the subclass in lines 35 through 42.

<P>We define an object named <B>chuck_ford</B> which presumably refers
to someone named Chuck who is driving a Ford moving van. The object named
<B>chuck_ford</B> is composed of four variables, three from the <B>moving_van</B>
class, and one from the <B>driver </B>class. Any of these four variables
can be manipulated in any of the methods defined within the <B>driven_truck</B>
class in the same way as in a singly inherited situation. A few examples
are given in lines 50 through 59 of the main program and the diligent student
should be able to add additional output messages to this program if he
understands the principles involved.

<P>All of the rules for <B>private </B>or protected <B>variables </B>and
<B>public </B>or <B>private </B>method inheritance as used with single
inheritance extends to multiple inheritance.

<P><B>DUPLICATED METHOD NAMES</B>

<P>You will notice that both of the parent classes have a method named
<B>initialize()</B>, and both of these are inherited into the subclass
with no difficulty. However, if we attempt to send a message to one of
these methods, we will have a problem, because the system does not know
which we are referring to. This problem will be solved and illustrated
in the next example program.

<P>Before going on to the next example program, it should be noted that
we have not declared any objects of the two parent classes in the main
program. Since the two parent classes are normal classes themselves, it
should be apparent that there is nothing magic about them and they can
be used to define and manipulate objects in the usual fashion. You may
wish to do this to review your knowledge of simple classes and objects
of those classes.

<P>Be sure to compile and execute this program after you understand its
operation completely.

<P><B>MORE DUPLICATE METHOD NAMES</B>

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

<P>The second example program in this chapter named MULTINH2.CPP, illustrates
the use of classes with duplicate method names being inherited into a derived
class.

<P>If you study the code, you will find that a new method has been added
to all three of the classes named <B>cost_per_full_day()</B>. This was
done intentionally to illustrate how the same method name can be used in
all three classes. The class definitions are no problem at all, the methods
are simply named and defined as shown. The problem comes when we wish to
use one of the methods since they are all the same name and they have the
same numbers and types of parameters and identical return types. This prevents
some sort of an overloading rule to disambiguate the message sent to one
or more of the methods.

<P>The method used to disambiguate the method calls are illustrated in
lines 63, 67, and 71 of the main program. The solution is to prepend the
class name to the method name with the double colon as used in the method
implementation definition. This is referred to as qualifying the method
name. Qualification is not necessary in line 71 since it is the method
in the derived class and it will take precedence over the other method
names. Actually, you could qualify all method calls, but if the names are
unique, the compiler can do it for you and make your code easier to write
and read.

<P>Be sure to compile and execute this program and study the results. The
observant student will notice that there is a slight discrepancy in the
results given in lines 84 through 86, since the first two values do not
add up to the third value exactly. This is due to the limited precision
of the <B>float </B>variable but should cause no real problem.

<P><B>WE VIOLATED SOME OOP PRINCIPLES</B>

<P>One of the hallmarks of object oriented programming is that when you
inherit a base class into a derived class, you are implying that the derived
class "is a" kind of the base class, with emphasis on the words "is a"
in this statement. It would be silly to inherit the characteristics of
a polar bear into an object meant to describe a skyscraper, because we
cannot say that a "skyscraper is a polar bear." However, since a "skyscraper
is a building", it would make sense to inherit the characteristics of a
building into a skyscraper class and add the unique properties of a skyscraper
to the class.

<P>The real problem is that multiple inheritance doesn't fit this problem
chosen as an example too well, except for the fact that the entities within
these classes are easy to visualize and therefore relatively easy to grasp
the concepts. At this point we are more interested in illustrating the
mechanics of multiple inheritance than principles of good object oriented
programming. You will have lots of time to study the proper use of all
forms of inheritance later. For the time being, we are interested in giving
you a complete tool box of C++ constructs.

<P><B>DUPLICATED VARIABLE NAMES</B>

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

<P>If you will examine the example program named MULTINH3.CPP, you will
notice that each base class has a variable with the same name.

<P>According to the rules of inheritance, an object of the <B>driven_truck</B>
class will have two variables with the same name, <B>weight</B>. This would
be a problem if it weren't for the fact that C++ has defined a method of
accessing each one in a well defined way. You have probably guessed that
we will use qualification to access each variable. Lines 41 and 48 illustrate
the use of the variables. It may be obvious, but it should be explicitly
stated, that there is no reason that the derived class itself cannot have
a variable of the same name as those inherited from the parent classes.
In order to access it, no qualification would be required, but qualification
with the derived class name is permitted.

<P>It should be apparent to you that once you understand single inheritance,
multiple inheritance is nothing more than an extension of the same rules.
Of course, if you inherit two methods or variables of the same name, you
must use qualification to allow the compiler to select the correct one.

<P>Constructors are called for both classes before the derived class constructor
is executed. The constructors for the base classes are called in the order
they are declared in the class header line.

<P><B>PRACTICAL MULTIPLE INHERITANCE</B>

<P>Example program ------> <B><A HREF="DATETIME.H">DATETIME.H</A></B>

<P>Examine the example program named DATETIME.H for a practical example
using multiple inheritance. You will notice that we are returning to our
familiar <B>new_date</B> and <B>time_of_day</B> classes from earlier chapters.

<P>There is a good deal to be learned from this very short header file
since it is our first example of member initialization used with multiple
inheritance. There are two constructors for this class, the first being
a very simple constructor that does nothing in itself, as is evident from
an examination of line 13. This constructor allows the default constructors
to be executed for the classes <B>new_date</B> and <B>time_of_day</B>.
In both cases the constructor that requires no parameters will be executed.

⌨️ 快捷键说明

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