chap10.htm
来自「Since the field of object oriented progr」· HTM 代码 · 共 306 行 · 第 1/2 页
HTM
306 行
<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 10</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<B>C++ Tutorial - Chapter 10</B>
<P><B><FONT SIZE=+3>V</FONT><FONT SIZE=+2>IRTUAL</FONT><FONT SIZE=+3> F</FONT><FONT SIZE=+2>UNCTIONS</FONT></B>
<P>Once again we are into a completely new topic with terminology which
will be new to you. If you are new to object oriented programming, you
should follow along in this chapter very carefully because every attempt
has been made to define every detail of this new and somewhat intimidating
topic. However, if you are well versed in object oriented programming,
and simply learning C++ as a new language, you may wish to skip the first
four programs in this chapter and go directly to the example program named
VIRTUAL5.CPP and continue from there to the end of the chapter.
<P>One term which must be defined is polymorphism, a rather large word
that simply means similar when used in the context of object oriented programming.
Objects are polymorphic if they have some similarities but are still somewhat
different. We will see how it is used in the context of object oriented
programming as we proceed through this chapter.
<P>We have already studied operator overloading and function overloading
in this tutorial, and they are a subtle form of polymorphism since in both
cases, a single entity is used to refer to two or more things. The use
of virtual functions can be a great aid in programming some kinds of projects
as you will see in these two chapters.
<P><B>A SIMPLE PROGRAM WITH INHERITANCE</B>
<P>Example program ------> <B><A HREF="VIRTUAL1.CPP">VIRTUAL1.CPP</A></B>
<P>Examine the example program named VIRTUAL1.CPP for the basic program
outline we will use for all discussion in this chapter. Since this program
has nothing to do with virtual functions, the name may be somewhat misleading.
It is named VIRTUAL1.CPP because it is part of a series of programs intended
to illustrate the use of virtual functions. The last program in this chapter
will illustrate the proper use of virtual functions.
<P>The first program is very simple and you will recognize it as being
similar to the programs studied earlier in this tutorial except that this
program is greatly simplified in order to effectively instruct you in the
use of a virtual function. You will notice that many of the methods from
the last chapter have been completely dropped from this example for simplicity,
and a new method has been added to the parent class, the method named <B>message()</B>
in line 9. Throughout this chapter we will be studying the operation of
the method named <B>message()</B> in the base class and the derived classes.
For that reason, there is a method named <B>message()</B> in the <B>car</B>
class as well as in the new class named <B>boat </B>in lines 30 through
36.
<P>You will also notice that there is a lack of a method named <B>message()</B>
in the <B>truck </B>class. This has been done on purpose to illustrate
the use of the virtual method, or if you prefer, you can refer to it as
a virtual function. You will recall that the method named <B>message()</B>
from the base class is available in the <B>truck </B>class because the
method from the base class is inherited with the keyword <B>public </B>included
in line 21.
<P>The method named <B>message()</B> in the base class and in the derived
classes has been kept very simple on purpose. Once again, we are interested
in the technique of the virtual method rather than a long complicated example.
<P>The main program is as simple as the classes, one object of each of
the classes is defined in lines 41 through 44 and the method named <B>message()</B>
is called once for each object. The result of executing the program indicates
that the method for each is called except for the object named <B>semi</B>,
which has no method named <B>message()</B>. As discussed in the last chapter,
the method named <B>message() </B>from the parent class is called and the
data output to the monitor indicates that this did happen since it displays
"Vehicle message" for the object named <B>semi</B>.
<P>The data for the objects is of no concern in this chapter so all data
is allowed to default to <B>private </B>type and none is inherited into
the derived classes. Some of the data is left in the example program simply
to make the classes look like classes. Based on your experience with C++
by now, you realize that the data could be removed since it is not used.
<P>After you understand this program, compile and execute it to see if
your compiler gives the same result of execution.
<P><B>ADDING THE KEYWORD VIRTUAL</B>
<P>Example program ------> <B><A HREF="VIRTUAL2.CPP">VIRTUAL2.CPP</A></B>
<P>As you examine the next example program named VIRTUAL2.CPP, you will
notice that there is one small change in line 9. The keyword <B>virtual
</B>has been added to the declaration of the method named <B>message()</B>
in the parent class.
<P>It may be a bit of a disappointment to you to learn that this program
operates no differently than the last example program. This is because
we are using objects directly and virtual methods have nothing to do with
objects, only with pointers to objects as we will see soon. There is an
additional comment in line 50 illustrating that since all four objects
are of different classes, it is impossible to assign any object to any
other object in this program. We will soon see that some pointer assignments
are permitted between objects of dissimilar classes.
<P>After you are sure that the fact that they are virtual functions, or
methods, has nothing to do with the objects as they are instantiated, compile
and execute this example program to see if your compiler results in the
same output as that listed.
<P><B>USING OBJECT POINTERS</B>
<P>Example program ------> <B><A HREF="VIRTUAL3.CPP">VIRTUAL3.CPP</A></B>
<P>Examine the example program named VIRTUAL3.CPP and you will find a repeat
of the first program but with a different main program.
<P>In this program the keyword <B>virtual </B>has been removed from the
method declaration in the parent class in line 9, and the main program
defines pointers to the objects rather than defining the objects themselves
in lines 41 through 44. Since we only defined pointers to the objects,
we find it necessary to allocate the objects before using them by using
the <B>new </B>operator in lines 46 through 53. Upon running the program,
we find that even though we are using pointers to the objects we have done
nothing different than what we did in the first program. Upon execution,
we find that the program operates in exactly the same manner as the first
example program in this chapter. This should not be surprising because
a pointer to a method can be used to operate on an object in the same manner
as an object can be manipulated.
<P>Be sure to compile and execute this program before continuing on to
the next example program. The observant student will notice that we failed
to check the allocation to see that it did allocate the objects properly,
and we also failed to deallocate the objects prior to terminating the program.
As always, in such a simple program, it doesn't matter because the heap
will be cleaned up automatically when we return to the operating system.
<P><B>A POINTER AND A VIRTUAL FUNCTION</B>
<P>Example program ------> <B><A HREF="VIRTUAL4.CPP">VIRTUAL4.CPP</A></B>
<P>The example program named VIRTUAL4.CPP is identical to the last program
except for the addition of the keyword <B>virtual </B>to line 9 once again.
<P>I hope you are not terribly disappointed to find that this program,
including the keyword <B>virtual</B>, is still identical to the last program.
Once again we are simply using pointers to each of the objects, and in
every case the pointer is of the same type as the object to which it points.
You will begin to see some changes in the next example program, so be patient,
we are almost there.
<P>Once again, it would be best for you to compile and execute this program.
<P>The four previous programs were meant to instruct you in what virtual
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?