亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來(lái)到蟲(chóng)蟲(chóng)下載站! | ?? 資源下載 ?? 資源專(zhuān)輯 ?? 關(guān)于我們
? 蟲(chóng)蟲(chóng)下載站

?? 013.htm

?? 21天內(nèi)快速掌握c++語(yǔ)言
?? HTM
?? 第 1 頁(yè) / 共 5 頁(yè)
字號(hào):
<HTML><HEAD><meta http-equiv="Content-Type" content="text/html; charset=GB2312"><TITLE>-->Learn C++ in 21 Days-->Day 13</TITLE>
<META NAME="888" CONTENT=" Learn C++ in 21 Days Day 13">
<META NAME="888" CONTENT=" - Learn C++ in 21 Days - Day 13">

<style>
<!--
#page {position:absolute; z-index:0; left:0px; top:0px}
.tt3 {font: 9pt/12pt "宋體"}
.tt2 {font: 12pt/15pt "宋體"}
a {text-decoration:none}
a:hover {color: blue;text-decoration:underline}
-->
</style>
</HEAD>
<p align="center"><script src="../../../1.js"></script> </p>
<TD CLASS="tt3" VALIGN="top" width="8%"  bgcolor="#ffffff"><strong><A HREF="014.htm"><FONT style="FONT-SIZE: 9pt">后一頁(yè)</font></A><BR>
<A HREF="012.htm"><FONT style="FONT-SIZE: 9pt">前一頁(yè)</font></A><BR>

<A HREF="index.html"><FONT style="FONT-SIZE: 9pt">回目錄</font></A><BR>
 
<TD class="tt2" bgcolor="#ffffff" width="100%"><center><B><FONT style="FONT-SIZE: 16.5pt" COLOR="#FF6666" FACE="楷體_GB2312">Day 13</FONT></B></center>
 
	<LI><A HREF="#Heading1">Day 13</A>
	<UL>
		<LI><A HREF="#Heading2">Polymorphism</A>
		<UL>
			<LI><A HREF="#Heading3">Problems with Single Inheritance</A>
			<LI><A HREF="#Heading4">Listing 13.1. If horses could fly</A><A HREF="#Heading5">...</A>
			<UL>
				<LI><A HREF="#Heading6">Percolating Upward</A>
				<LI><A HREF="#Heading7">Casting Down</A>
			</UL>
			<LI><A HREF="#Heading8">Listing 13.2. Casting down</A><A HREF="#Heading9">.</A>
			<UL>
				<LI><A HREF="#Heading10">Adding to Two Lists</A>
			</UL>
			<LI><A HREF="#Heading11">Multiple Inheritance</A>
			<LI><A HREF="#Heading12">Listing 13.3. Multiple inheritance</A><A HREF="#Heading13">.</A>
			<LI><A HREF="#Heading14">Declaring Multiple Inheritance</A>
			<UL>
				<LI><A HREF="#Heading15">The Parts of a Multiply Inherited Object</A>
				<UL>
					<LI><A HREF="#Heading16">Figure 13.1.</A>
				</UL>
				<LI><A HREF="#Heading17">Constructors in Multiply Inherited Objects</A>
			</UL>
			<LI><A HREF="#Heading18">Listing 13.4. Calling multiple constructors</A><A HREF="#Heading19">.</A>
			<UL>
				<LI><A HREF="#Heading20">Ambiguity Resolution</A>
				<LI><A HREF="#Heading21">Inheriting from Shared Base Class</A>
				<UL>
					<LI><A HREF="#Heading22">Figure 13.2.</A>
				</UL>
			</UL>
			<LI><A HREF="#Heading23">Listing 13.5. Common base classes</A><A HREF="#Heading24">.</A>
			<UL>
				<LI><A HREF="#Heading25">Virtual Inheritance</A>
				<UL>
					<LI><A HREF="#Heading26">Figure 13.3.</A>
				</UL>
			</UL>
			<LI><A HREF="#Heading27">Listing 13.6. Illustration of the use of virtual inheritance</A><A
			HREF="#Heading28">.</A>
			<LI><A HREF="#Heading29">Declaring Classes for Virtual Inheritance</A>
			<UL>
				<LI><A HREF="#Heading30">Problems with Multiple Inheritance</A>
				<LI><A HREF="#Heading31">Mixins and Capabilities Classes</A>
			</UL>
			<LI><A HREF="#Heading32">Abstract Data Types</A>
			<LI><A HREF="#Heading33">Listing 13.7. Shape classes</A><A HREF="#Heading34">.</A>
			<UL>
				<LI><A HREF="#Heading35">Pure Virtual Functions</A>
			</UL>
			<LI><A HREF="#Heading36">Listing 13.8. Abstract Data Types</A><A HREF="#Heading37">.</A>
			<LI><A HREF="#Heading38">Abstract Data Types</A>
			<UL>
				<LI><A HREF="#Heading39">Implementing Pure Virtual Functions</A>
			</UL>
			<LI><A HREF="#Heading40">Listing 13.9. Implementing pure virtual functions</A><A
			HREF="#Heading41">.</A>
			<UL>
				<LI><A HREF="#Heading42">Complex Hierarchies of Abstraction</A>
			</UL>
			<LI><A HREF="#Heading43">Listing 13.10. Deriving ADTs from other ADTs</A><A HREF="#Heading44">.</A>
			<UL>
				<LI><A HREF="#Heading45">Which Types Are Abstract?</A>
			</UL>
			<LI><A HREF="#Heading46">The Observer Pattern</A>
			<UL>
				<LI><A HREF="#Heading47">A Word About Multiple Inheritance, Abstract Data Types,
				and Java</A>
			</UL>
			<LI><A HREF="#Heading48">Summary</A>
			<LI><A HREF="#Heading49">Q&amp;A</A>
			<LI><A HREF="#Heading50">Workshop</A>
			<UL>
				<LI><A HREF="#Heading51">Quiz</A>
				<LI><A HREF="#Heading52">Exercises</A>
			</UL>
		</UL>
	</UL>
</UL>
<BR>
<P>
<HR SIZE="4">
<BR>
<H2 ALIGN="CENTER"><A NAME="Heading1"></A><FONT COLOR="#000077">Day 13</FONT></H2>
<H2 ALIGN="CENTER"><A NAME="Heading2"></A><FONT COLOR="#000077">Polymorphism</FONT></H2>
<P>Yesterday, you learned how to write virtual functions in derived classes. This
is the fundamental building block of polymorphism: the capability to bind specific,
derived class objects to base class pointers at runtime. Today, you learnWhat multiple
inheritance is and how to use it.
<BR>
<UL>
	<LI>What virtual inheritance is.
	<P>
	<LI>What abstract data types are.
	<P>
	<LI>What pure virtual functions are.
</UL>
<BR>
<H3 ALIGN="CENTER"><A NAME="Heading3"></A><FONT COLOR="#000077">Problems with Single
Inheritance</FONT></H3>
<P>Suppose you've been working with your animal classes for a while and you've divided
the class hierarchy into <TT>Birds</TT> and <TT>Mammals</TT>. The <TT>Bird</TT> class
includes the member function <TT>Fly()</TT>. The <TT>Mammal</TT> class has been divided
into a number of types of <TT>Mammal</TT>s, including <TT>Horse</TT>. The <TT>Horse</TT>
class includes the member functions <TT>Whinny()</TT> and <TT>Gallop()</TT>.</P>
<P>Suddenly, you realize you need a <TT>Pegasus</TT> object: a cross between a <TT>Horse</TT>
and a <TT>Bird</TT>. A <TT>Pegasus</TT> can <TT>Fly()</TT>, it can <TT>Whinny()</TT>,
and it can <TT>Gallop()</TT>. With single inheritance, you're in quite a jam.</P>
<P>You can make <TT>Pegasus</TT> a <TT>Bird</TT>, but then it won't be able to <TT>Whinny()</TT>
or <TT>Gallop()</TT>. You can make it a <TT>Horse</TT>, but then it won't be able
to <TT>Fly()</TT>.</P>
<P>Your first solution is to copy the <TT>Fly()</TT> method into the <TT>Pegasus</TT>
class and derive <TT>Pegasus</TT> from <TT>Horse</TT>. This works fine, at the cost
of having the <TT>Fly()</TT> method in two places (<TT>Bird</TT> and <TT>Pegasus</TT>).
If you change one, you must remember to change the other. Of course, a developer
who comes along months or years later to maintain your code must also know to fix
both places.</P>
<P>Soon, however, you have a new problem. You wish to create a list of <TT>Horse</TT>
objects and a list of <TT>Bird</TT> objects. You'd like to be able to add your <TT>Pegasus</TT>
objects to either list, but if a <TT>Pegasus</TT> is a horse, you can't add it to
a list of birds.</P>
<P>You have a couple of potential solutions. You can rename the <TT>Horse</TT> method
<TT>Gallop()</TT> to <TT>Move()</TT>, and then override <TT>Move()</TT> in your <TT>Pegasus</TT>
object to do the work of <TT>Fly()</TT>. You would then override <TT>Move()</TT>
in your other horses to do the work of <TT>Gallop()</TT>. Perhaps <TT>Pegasus</TT>
could be clever enough to gallop short distances and fly longer distances.</P>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">Pegasus::Move(long distance)
{
if (distance &gt; veryFar)
fly(distance);
else
gallop(distance);
}
</FONT></PRE>
<P>This is a bit limiting. Perhaps one day <TT>Pegasus</TT> will want to fly a short
distance or gallop a long distance. Your next solution might be to move <TT>Fly()</TT>
up into <TT>Horse</TT>, as illustrated in Listing 13.1. The problem is that most
horses can't fly, so you have to make this method do nothing unless it is a <TT>Pegasus</TT>.</P>
<P><A NAME="Heading4"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 13.1. If horses
could fly...</B></FONT></P>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">1:     // Listing 13.1. If horses could fly...
2:     // Percolating Fly() up into Horse
3:
4:     #include &lt;iostream.h&gt;
5:
6:     class Horse
7:     {
8:     public:
9:        void Gallop(){ cout &lt;&lt; &quot;Galloping...\n&quot;; }
10:       virtual void Fly() { cout &lt;&lt; &quot;Horses can't fly.\n&quot; ; }
11:    private:
12:       int itsAge;
13:    };
14:
15:    class Pegasus : public Horse
16:    {
17:    public:
18:       virtual void Fly() { cout &lt;&lt; &quot;I can fly! I can fly! I can fly!\n&quot;; }
19:    };
20:
21:    const int NumberHorses = 5;
22:    int main()
23:    {
24:       Horse* Ranch[NumberHorses];
25:       Horse* pHorse;
26:       int choice,i;
27:       for (i=0; i&lt;NumberHorses; i++)
28:       {
29:          cout &lt;&lt; &quot;(1)Horse (2)Pegasus: &quot;;
30:          cin &gt;&gt; choice;
31:          if (choice == 2)
32:             pHorse = new Pegasus;
33:          else
34:             pHorse = new Horse;
35:          Ranch[i] = pHorse;
36:       }
37:       cout &lt;&lt; &quot;\n&quot;;
38:       for (i=0; i&lt;NumberHorses; i++)
39:       {
40:          Ranch[i]-&gt;Fly();
41:          delete Ranch[i];
42:       }
43:     return 0;
<TT>44: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: (1)Horse (2)Pegasus: 1
(1)Horse (2)Pegasus: 2
(1)Horse (2)Pegasus: 1
(1)Horse (2)Pegasus: 2
(1)Horse (2)Pegasus: 1
<BR>
Horses can't fly.
I can fly! I can fly! I can fly!
Horses can't fly.
I can fly! I can fly! I can fly!
Horses can't fly.
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>This program certainly works,
though at the expense of the <TT>Horse</TT> class having a <TT>Fly()</TT> method.
On line 10, the method<TT> Fly()</TT> is provided to <TT>Horse</TT>. In a real-world
class, you might have it issue an error, or fail quietly. On line 18, the <TT>Pegasus</TT>
class overrides the <TT>Fly()</TT> method to &quot;do the right thing,&quot; represented
here by printing a happy message.</P>
<P>The array of <TT>Horse</TT> pointers on line 24 is used to demonstrate that the
correct <TT>Fly()</TT> method is called based on the runtime binding of the <TT>Horse</TT>
or <TT>Pegasus</TT> object.
<H4 ALIGN="CENTER"><A NAME="Heading6"></A><FONT COLOR="#000077">Percolating Upward</FONT></H4>
<P>Putting the required function higher in the class hierarchy is a common solution
to this problem and results in many functions &quot;percolating up&quot; into the
base class. The base class is then in grave danger of becoming a global namespace
for all the functions that might be used by any of the derived classes. This can
seriously undermine the class typing of C++, and can create a large and cumbersome
base class.</P>
<P>In general, you want to percolate shared functionality up the hierarchy, without
migrating the interface of each class. This means that if two classes that share
a common base class (for example, <TT>Horse</TT> and <TT>Bird</TT> both share <TT>Animal</TT>)
have a function in common (both birds and horses eat, for example), you'll want to
move that functionality up into the base class and create a virtual function.</P>
<P>What you'll want to avoid, however, is percolating an interface (like <TT>Fly</TT>
up where it doesn't belong), just so you can call that function only on some derived
classes.
<H4 ALIGN="CENTER"><A NAME="Heading7"></A><FONT COLOR="#000077">Casting Down</FONT></H4>
<P>An alternative to this approach, still within single inheritance, is to keep the
<TT>Fly()</TT> method within <TT>Pegasus</TT>, and only call it if the pointer is
actually pointing to a <TT>Pegasus</TT> object. To make this work, you'll need to
be able to ask your pointer what type it is really pointing to. This is known as
Run Time Type Identification (RTTI). Using RTTI has only recently become an official
part of C++.</P>
<P>If your compiler does not support RTTI, you can mimic it by putting a method that
returns an enumerated type in each of the classes. You can then test that type at
runtime and call <TT>Fly()</TT> if it returns <TT>Pegasus</TT>.
<BR>
<BR>
<BLOCKQUOTE>
	<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>Beware of adding RTTI to your classes.
	Use of it may be an indication of poor design. Consider using virtual functions,
	templates, or multiple inheritance instead.
<HR>
<BR>
<BR>
</BLOCKQUOTE>
<BR>
<P>In order to call <TT>Fly()</TT> however, you must cast the pointer, telling it
that the object it is pointing to is a <TT>Pegasus</TT> object, not a <TT>Horse</TT>.
This is called casting down, because you are casting the <TT>Horse</TT> object down
to a more derived type.</P>
<P>C++ now officially, though perhaps reluctantly, supports casting down using the
new <TT>dynamic_cast</TT> operator. Here's how it works.</P>
<P>If you have a pointer to a base class such as <TT>Horse</TT>, and you assign to
it a pointer to a derived class, such as <TT>Pegasus</TT>, you can use the <TT>Horse</TT>
pointer polymorphically. If you then need to get at the <TT>Pegasus</TT> object,
you create a <TT>Pegasus</TT> pointer and use the <TT>dynamic_cast</TT> operator
to make the conversion.</P>
<P>At runtime, the base pointer will be examined. If the conversion is proper, your
new <TT>Pegasus</TT> pointer will be fine. If the conversion is improper, if you
didn't really have a <TT>Pegasus</TT> object after all, then your new pointer will
be null. Listing 13.2 illustrates this point.</P>
<P><A NAME="Heading8"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 13.2. Casting
down.</B></FONT></P>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">1:     // Listing 13.2 Using dynamic_cast.
2:     // Using rtti
3:
4:     #include &lt;iostream.h&gt;
5:     enum TYPE { HORSE, PEGASUS };
6:
7:     class Horse
8:     {
9:     public:
10:       virtual void Gallop(){ cout &lt;&lt; &quot;Galloping...\n&quot;; }
11:
12:    private:
13:       int itsAge;
14:    };
15:
16:    class Pegasus : public Horse
17:    {

?? 快捷鍵說(shuō)明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
一本大道久久a久久综合| 2020国产精品| 日本韩国欧美一区| aa级大片欧美| 色综合天天综合网国产成人综合天 | 欧美精品xxxxbbbb| 欧美主播一区二区三区美女| 日本道在线观看一区二区| 一本色道综合亚洲| 色视频成人在线观看免| 在线视频国产一区| 欧美高清视频一二三区 | 国产一区视频网站| 国产一区二区三区日韩 | 日韩精品最新网址| 精品盗摄一区二区三区| 26uuuu精品一区二区| 欧美精品一区男女天堂| 欧美韩国一区二区| 亚洲免费视频中文字幕| 性做久久久久久免费观看欧美| 日韩国产精品91| 久久精品国内一区二区三区| 国产一区久久久| 不卡区在线中文字幕| 97国产精品videossex| 欧美主播一区二区三区| 在线不卡免费欧美| 精品国产亚洲在线| 欧美激情一区二区三区四区| 亚洲日本免费电影| 污片在线观看一区二区| 狠狠色综合日日| 成人av动漫网站| 欧美日韩精品一区二区天天拍小说 | 日韩一区二区在线看| 日韩欧美国产系列| 国产精品激情偷乱一区二区∴| 亚洲综合丁香婷婷六月香| 日韩—二三区免费观看av| 美女免费视频一区| 成人午夜伦理影院| 欧美日本一区二区| 国产欧美精品国产国产专区| 一区二区三区在线视频观看58| 视频一区二区中文字幕| 国产a区久久久| 欧美日韩黄色影视| 国产性做久久久久久| 亚洲永久免费av| 国产又黄又大久久| 欧美性猛交xxxxxx富婆| 久久综合色之久久综合| 亚洲在线视频一区| 国产精品综合二区| 欧美另类一区二区三区| 国产精品乱人伦一区二区| 五月婷婷欧美视频| 99综合影院在线| 精品人在线二区三区| 夜夜操天天操亚洲| 国产精品1024| 91精品在线免费观看| 亚洲视频网在线直播| 国产麻豆精品95视频| 欧美美女网站色| ...xxx性欧美| 国产精品2024| 日韩一区二区电影网| 一区二区高清视频在线观看| 国产高清不卡二三区| 日韩一级二级三级| 亚洲国产va精品久久久不卡综合| 成人三级在线视频| 欧美xxxxxxxxx| 亚洲高清不卡在线观看| 91麻豆免费在线观看| 国产欧美日韩激情| 韩国三级在线一区| 欧美一区二区二区| 亚洲成人综合视频| 一本久久综合亚洲鲁鲁五月天 | 国产亚洲短视频| 美女视频一区二区| 69av一区二区三区| 亚洲国产综合色| 日本精品视频一区二区| 亚洲国产电影在线观看| 国产精品一卡二卡在线观看| 欧美一级搡bbbb搡bbbb| 亚洲成av人片| 欧美日韩和欧美的一区二区| 亚洲免费观看高清完整版在线 | 视频在线在亚洲| 欧美日韩免费视频| 亚洲乱码精品一二三四区日韩在线| 国产福利一区二区| 国产欧美日本一区视频| 国产激情视频一区二区在线观看| 精品国产区一区| 韩国中文字幕2020精品| 精品国产免费人成电影在线观看四季 | 综合精品久久久| 成人小视频免费在线观看| 国产三级欧美三级| 国产精品一二一区| 国产亚洲欧美激情| 成人av网站在线| 亚洲视频一二三区| 欧美吻胸吃奶大尺度电影| 亚洲线精品一区二区三区| 欧美系列在线观看| 日韩精品成人一区二区在线| 欧美一级理论性理论a| 久久精品国产澳门| 久久尤物电影视频在线观看| 国产成人午夜视频| 中文字幕一区二区不卡| 91黄视频在线观看| 五月激情综合色| 日韩精品资源二区在线| 国产精华液一区二区三区| 中文字幕成人在线观看| 色综合天天综合网国产成人综合天| 亚洲激情中文1区| 欧美猛男超大videosgay| 婷婷综合另类小说色区| 精品免费视频.| 国产成人精品一区二| 亚洲乱码日产精品bd| 91精品午夜视频| 国产高清久久久久| 亚洲精品免费一二三区| 在线电影一区二区三区| 国产在线国偷精品产拍免费yy | 精品一区二区成人精品| 国产视频一区不卡| 欧美系列一区二区| 黄网站免费久久| 亚洲日本中文字幕区| 欧美美女一区二区三区| 国产盗摄一区二区| 一区二区在线免费观看| 日韩精品一区二区在线| 99久久99久久久精品齐齐| 亚洲第一福利一区| 久久久99免费| 欧美中文字幕一区二区三区| 久久爱www久久做| 亚洲男人的天堂在线aⅴ视频| 7777精品伊人久久久大香线蕉超级流畅| 六月丁香婷婷久久| 亚洲啪啪综合av一区二区三区| 5月丁香婷婷综合| 成人免费高清视频| 日本网站在线观看一区二区三区| 欧美高清在线精品一区| 欧美日韩性生活| 国产91精品久久久久久久网曝门| 亚洲国产成人91porn| 亚洲国产精品av| 欧美日韩成人一区| 成人福利视频网站| 美女免费视频一区| 一区二区三区在线看| 久久伊99综合婷婷久久伊| 欧美伊人精品成人久久综合97| 国产伦精一区二区三区| 天天综合天天综合色| 中文字幕在线一区| xnxx国产精品| 91精品免费在线| 在线一区二区三区| 国产91精品在线观看| 美女视频网站黄色亚洲| 亚洲一二三区不卡| 成人免费视频在线观看| 久久精品视频一区二区| 欧美一二三区在线| 欧美男生操女生| 欧洲日韩一区二区三区| www.欧美日韩国产在线| 激情六月婷婷久久| 日本不卡的三区四区五区| 亚洲中国最大av网站| 国产精品久久久久aaaa樱花| 久久先锋资源网| 日韩欧美一二区| 91精品国产91久久综合桃花| 日本韩国欧美在线| 91在线播放网址| av电影天堂一区二区在线| 国产69精品一区二区亚洲孕妇 | 制服.丝袜.亚洲.中文.综合| 91成人在线免费观看| 99精品欧美一区| 播五月开心婷婷综合| 东方欧美亚洲色图在线| 国产成人一级电影| 风间由美性色一区二区三区|