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

📄 page609.html

📁 wqeqwvrw rkjqhwrjwq jkhrjqwhrwq jkhrwq
💻 HTML
字号:
<HTML>
<HEAD>
<TITLE>Polymorphism</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
 <img src="cover75.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cover75.gif" alt="Logo" align=right>
<b>Data Structures and Algorithms 
with Object-Oriented Design Patterns in C++</b><br>
<A NAME="tex2html9430" HREF="page610.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page610.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html9428" HREF="page606.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page606.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html9422" HREF="page608.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page608.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html9432" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html9433" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <BR><HR>
<H2><A NAME="SECTION0018420000000000000000">Polymorphism</A></H2>
<P>
<em>Polymorphism</em><A NAME=57500>&#160;</A>
literally means ``having many forms.''
Polymorphism arises when a set of distinct classes share a common interface
because they are all derived from the same base class(es).
Because the derived classes are distinct,
their implementations may differ.
However, because the derived classes share a common interface,
instances of those classes are used in exactly the same way.
<P>
Consider a program for creating simple drawings.
Suppose the program provides a set of primitive graphical objects,
such as circles, rectangles and squares.
The user of the program selects the desired objects,
and then invokes commands to draw, to erase, or to move them about.
Ideally, all graphical objects support the same set of operations.
Nevertheless,
the way that the operations are implemented
varies from one object to the next.
<P>
We implement this as follows:
First, we define a <tt>GraphicalObject</tt> class
which represents the common interface shared by all graphical objects.
Then, we derive from <tt>GraphicalObject</tt> classes
to represent circles, rectangles and squares.
<P>
Consider the <tt>GraphicalObject</tt> class declared in Program&nbsp;<A HREF="page609.html#proggraphic1h" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page609.html#proggraphic1h"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>.
This class has a single member variable, <tt>center</tt>,
which is an instance of the <tt>Point</tt> class
and which represents the position in the drawing
of the center-point of the graphical object.
The constructor for the <tt>GraphicalObject</tt> class takes
as its lone argument a reference to a <tt>Point</tt> and
initializes the <tt>center</tt> member variables accordingly.
<P>
<P><A NAME="57755">&#160;</A><A NAME="proggraphic1h">&#160;</A> <IMG WIDTH=575 HEIGHT=429 ALIGN=BOTTOM ALT="program57510" SRC="img2606.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2606.gif"  ><BR>
<STRONG>Program:</STRONG> <tt>Point</tt> and <tt>GraphicalObject</tt> Class Definitions<BR>
<P>
<P>
In addition to the constructor and destructor,
three member functions are declared.
The <tt>Draw</tt> routine draws the object,
the <tt>Erase</tt> routine erase the object,
and the <tt>MoveTo</tt> routine moves the object to a new position.
<P>
Program&nbsp;<A HREF="page609.html#proggraphic1c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page609.html#proggraphic1c"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> shows a possible implementation
for the <tt>Erase</tt> function:
In this case we assume that the image is drawn using an imaginary pen.
Assuming that we know how to draw a graphical object,
we can erase the object by changing the color of the pen
so that it matches the background color
and then redrawing the object.
<P>
<P><A NAME="57764">&#160;</A><A NAME="proggraphic1c">&#160;</A> <IMG WIDTH=575 HEIGHT=258 ALIGN=BOTTOM ALT="program57536" SRC="img2607.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2607.gif"  ><BR>
<STRONG>Program:</STRONG> <tt>GraphicalObject</tt> Class <tt>MoveTo</tt> and <tt>Erase</tt>     Member Function Definitions<BR>
<P>
<P>
Once we can erase an object as well as draw it,
then moving it is easy.
Just erase the object,
change its <tt>center</tt> point,
and then draw it again.
This is how the <tt>MoveTo</tt> function shown in
Program&nbsp;<A HREF="page609.html#proggraphic1c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page609.html#proggraphic1c"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> is implemented.
<P>
We have seen that the <tt>GraphicalObject</tt> class provides
implementations for the <tt>Erase</tt> and <tt>MoveTo</tt> member functions.
However, the <tt>GraphicalObject</tt> class does not provide
an implementation for the <tt>Draw</tt> function.
Why not?
Because until we know what kind of object it is,
we cannot possibly know how to draw it!
<P>
Consider the <tt>Circle</tt> class defined in Program&nbsp;<A HREF="page609.html#proggraphic2h" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page609.html#proggraphic2h"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>.
This class is derived from <tt>GraphicalObject</tt>.
Therefore, it inherits the member variable <tt>center</tt>
and the member functions <tt>Erase</tt> and <tt>MoveTo</tt>.
The <tt>Circle</tt> class adds an additional member variable,
<tt>radius</tt>,
and it overrides the <tt>Draw</tt> routine.
The body of the <tt>Draw</tt> routine is not shown in Program&nbsp;<A HREF="page609.html#proggraphic2h" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page609.html#proggraphic2h"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>.
However, we shall assume that it draws a circle with the given radius
and center point.
<P>
<P><A NAME="57767">&#160;</A><A NAME="proggraphic2h">&#160;</A> <IMG WIDTH=575 HEIGHT=524 ALIGN=BOTTOM ALT="program57567" SRC="img2608.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2608.gif"  ><BR>
<STRONG>Program:</STRONG> <tt>Circle</tt>, <tt>Rectangle</tt> and <tt>Square</tt> Class Definitions<BR>
<P>
<P>
Using the <tt>Circle</tt> class defined in Program&nbsp;<A HREF="page609.html#proggraphic2h" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page609.html#proggraphic2h"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>
we can write code like this:
<PRE>Circle c (Point (0, 0), 5);
c.Draw ();
c.MoveTo (Point (10, 10));
c.Erase ();</PRE>
This code sequence declares a circle object with its center
initially at position (0,0) and radius&nbsp;5.
The circle is then drawn, moved to (10,10), and then erased.
<P>
Program&nbsp;<A HREF="page609.html#proggraphic2h" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page609.html#proggraphic2h"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> also defines
the classes <tt>Rectangle</tt> and <tt>Square</tt>.
The <tt>Rectangle</tt> class is also derived from <tt>GraphicalObject</tt>.
Therefore, it inherits the member variable <tt>center</tt>
and the member functions <tt>Erase</tt> and <tt>MoveTo</tt>.
The <tt>Rectangle</tt> class adds two additional member variables,
<tt>height</tt> and <tt>width</tt>,
and it overrides the <tt>Draw</tt> routine.
The body of the <tt>Draw</tt> routine is not shown in Program&nbsp;<A HREF="page609.html#proggraphic2h" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page609.html#proggraphic2h"><IMG  ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>.
However, we shall assume that it draws a rectangle with the given dimensions
and center point.
<P>
The <tt>Square</tt> class is derived from the <tt>Rectangle</tt> class.
No new member variables or functions are declared--those inherited from <tt>GraphicalObject</tt> or from
<tt>Rectangle</tt> are sufficient.
The constructor simply arranges to make sure that the <tt>height</tt>
and <tt>width</tt> of a square are equal!
<P>
<BR> <HR>
<UL> 
<LI> <A NAME="tex2html9434" HREF="page610.html#SECTION0018421000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page610.html#SECTION0018421000000000000000">Virtual Member Functions</A>
<LI> <A NAME="tex2html9435" HREF="page611.html#SECTION0018422000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page611.html#SECTION0018422000000000000000">Abstract Classes and Concrete Classes</A>
<LI> <A NAME="tex2html9436" HREF="page612.html#SECTION0018423000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page612.html#SECTION0018423000000000000000">Algorithmic Abstraction</A>
</UL>
<HR><A NAME="tex2html9430" HREF="page610.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page610.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html9428" HREF="page606.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page606.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html9422" HREF="page608.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page608.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html9432" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html9433" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <P><ADDRESS>
<img src="bruno.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/bruno.gif" alt="Bruno" align=right>
<a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html">Copyright &#169; 1997</a> by <a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html">Bruno R. Preiss, P.Eng.</a>  All rights reserved.

</ADDRESS>
</BODY>
</HTML>

⌨️ 快捷键说明

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