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

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

?? 014.htm

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

<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="015.htm"><FONT style="FONT-SIZE: 9pt">后一頁(yè)</font></A><BR>
<A HREF="013.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 14</FONT></B></center>
 
	<LI><A HREF="#Heading1">Day 14</A>
	<UL>
		<LI><A HREF="#Heading2">Special Classes and Functions</A>
		<UL>
			<LI><A HREF="#Heading3">Static Member Data</A>
			<LI><A HREF="#Heading4">Listing 14.1. Static member data.</A>
			<LI><A HREF="#Heading5">Listing 14.2. Accessing static members without an object</A><A
			HREF="#Heading6">.</A>
			<LI><A HREF="#Heading7">Listing 14.3. Accessing static members using non-static member
			functions</A><A HREF="#Heading8">.</A>
			<LI><A HREF="#Heading9">Static Member Functions</A>
			<LI><A HREF="#Heading10">Listing 14.4. Static member functions</A><A HREF="#Heading11">.</A>
			<LI><A HREF="#Heading12">Static Member Functions</A>
			<LI><A HREF="#Heading13">Pointers to Functions</A>
			<LI><A HREF="#Heading14">Listing 14.5. Pointers to functions</A><A HREF="#Heading15">.</A>
			<LI><A HREF="#Heading16">Pointer to Function</A>
			<UL>
				<LI><A HREF="#Heading17">Why Use Function Pointers?</A>
			</UL>
			<LI><A HREF="#Heading18">Listing 14.6. Rewriting Listing 14.5 without the pointer
			to function</A><A HREF="#Heading19">.</A>
			<LI><A HREF="#Heading20">Shorthand Invocation</A>
			<UL>
				<LI><A HREF="#Heading21">Arrays of Pointers to Functions</A>
			</UL>
			<LI><A HREF="#Heading22">Listing 14.7. Demonstrates use of an array of pointers to
			functions</A><A HREF="#Heading23">.</A>
			<UL>
				<LI><A HREF="#Heading24">Passing Pointers to Functions to Other Functions</A>
			</UL>
			<LI><A HREF="#Heading25">Listing 14.8. Passing pointers to functions</A>
			<LI><A HREF="#Heading26">as function arguments.</A>
			<UL>
				<LI><A HREF="#Heading27">Using typedef with Pointers to Functions</A>
			</UL>
			<LI><A HREF="#Heading28">Listing 14.9. Using typedef to make pointers to functions
			more readable</A><A HREF="#Heading29">.</A>
			<LI><A HREF="#Heading30">Pointers to Member Functions</A>
			<LI><A HREF="#Heading31">Listing 14.10. Pointers to member functions</A><A HREF="#Heading32">.</A>
			<UL>
				<LI><A HREF="#Heading33">Arrays of Pointers to Member Functions</A>
			</UL>
			<LI><A HREF="#Heading34">Listing 14.11. Array of pointers to member functions</A><A
			HREF="#Heading35">.</A>
			<LI><A HREF="#Heading36">Summary</A>
			<LI><A HREF="#Heading37">Q&amp;A</A>
			<LI><A HREF="#Heading38">Workshop</A>
			<UL>
				<LI><A HREF="#Heading39">Quiz</A>
				<LI><A HREF="#Heading40">Exercises</A>
			</UL>
		</UL>
	</UL>
</UL>
<BR>
<P>
<HR SIZE="4">
<BR>
<H2 ALIGN="CENTER"><BR>
<A NAME="Heading1"></A><FONT COLOR="#000077">Day 14</FONT></H2>
<BR>
<H2 ALIGN="CENTER"><A NAME="Heading2"></A><FONT COLOR="#000077">Special Classes and
Functions</FONT></H2>
<P>C++ offers a number of ways to limit the scope and impact of variables and pointers.
So far you've seen how to create global variables, local function variables, pointers
to variables, and class member variables. Today you learn
<BR>
<UL>
	<LI>What static member variables and static member functions are.
	<P>
	<LI>How to use static member variables and static member functions.
	<P>
	<LI>How to create and manipulate pointers to functions and pointers to member functions.
	<P>
	<LI>How to work with arrays of pointers to functions.
</UL>
<BR>
<H3 ALIGN="CENTER"><A NAME="Heading3"></A><FONT COLOR="#000077">Static Member Data</FONT></H3>
<P>Until now, you have probably thought of the data in each object as unique to that
object and not shared among objects in a class. For example, if you have five <TT>Cat</TT>
objects, each has its own age, weight, and other data. The age of one does not affect
the age of another.</P>
<P>There are times, however, when you'll want to keep track of a pool of data. For
example, you might want to know how many objects for a specific class have been created
in your program, and how many are still in existence. Static member variables are
shared among all instances of a class. They are a compromise between global data,
which is available to all parts of your program, and member data, which is usually
available only to each object.</P>
<P>You can think of a static member as belonging to the class rather than to the
object. Normal member data is one per object, but static members are one per class.
Listing 14.1 declares a <TT>Cat</TT> object with a static data member, <TT>HowManyCats</TT>.
This variable keeps track of how many <TT>Cat</TT> objects have been created. This
is done by incrementing the static variable, <TT>HowManyCats</TT>, with each construction
and decrementing it with each destruction.</P>
<BR>
<P><A NAME="Heading4"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 14.1. Static
member data.</B></FONT>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">1:     //Listing 14.1 static data members
2:
3:     #include &lt;iostream.h&gt;
4:
5:     class Cat
6:     {
7:     public:
8:        Cat(int age):itsAge(age){HowManyCats++; }
9:        virtual ~Cat() { HowManyCats--; }
10:       virtual int GetAge() { return itsAge; }
11:       virtual void SetAge(int age) { itsAge = age; }
12:       static int HowManyCats;
13:
14:    private:
15:       int itsAge;
16:
17:    };
18:
19:    int Cat::HowManyCats = 0;
20:
21:    int main()
22:    {
23:       const int MaxCats = 5; int i;
24:       Cat *CatHouse[MaxCats];
25:       for (i = 0; i&lt;MaxCats; i++)
26:          CatHouse[i] = new Cat(i);
27:
28:       for (i = 0; i&lt;MaxCats; i++)
29:       {
30:          cout &lt;&lt; &quot;There are &quot;;
31:          cout &lt;&lt; Cat::HowManyCats;
32:          cout &lt;&lt; &quot; cats left!\n&quot;;
33:          cout &lt;&lt; &quot;Deleting the one which is &quot;;
34:          cout &lt;&lt; CatHouse[i]-&gt;GetAge();
35:          cout &lt;&lt; &quot; years old\n&quot;;
36:          delete CatHouse[i];
37:          CatHouse[i] = 0;
38:       }
39:     return 0;
<TT>40: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: There are 5 cats left!
Deleting the one which is 0 years old
There are 4 cats left!
Deleting the one which is 1 years old
There are 3 cats left!
Deleting the one which is 2 years old
There are 2 cats left!
Deleting the one which is 3 years old
There are 1 cats left!
Deleting the one which is 4 years old
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>On lines 5 to 17 the simplified
class <TT>Cat</TT> is declared. On line 12, <TT>HowManyCats</TT> is declared to be
a static member variable of type <TT>int</TT>.</P>
<P>The declaration of <TT>HowManyCats</TT> does not define an integer; no storage
space is set aside. Unlike the non-static member variables, no storage space is set
aside by instantiating a <TT>Cat</TT> object, because the <TT>HowManyCats</TT> member
variable is not in the object. Thus, on line 19 the variable is defined and initialized.</P>
<P>It is a common mistake to forget to define the static member variables of classes.
Don't let this happen to you! Of course, if it does, the linker will catch it with
a pithy error message such as the following:</P>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">undefined symbol Cat::HowManyCats
</FONT></PRE>
<P>You don't need to do this for <TT>itsAge</TT>, because it is a non-static member
variable and is defined each time you make a <TT>Cat</TT> object, which you do here
on line 26.</P>
<P>The constructor for <TT>Cat</TT> increments the static member variable on line
8. The destructor decrements it on line 9. Thus, at any moment, <TT>HowManyCats</TT>
has an accurate measure of how many <TT>Cat</TT> objects were created but not yet
destroyed.</P>
<P>The driver program on lines 21-40 instantiates five <TT>Cat</TT>s and puts them
in an array. This calls five <TT>Cat</TT> constructors, and thus <TT>HowManyCats</TT>
is incremented five times from its initial value of <TT>0</TT>.</P>
<P>The program then loops through each of the five positions in the array and prints
out the value of <TT>HowManyCats</TT> before deleting the current <TT>Cat</TT> pointer.
The printout reflects that the starting value is <TT>5</TT> (after all, 5 are constructed),
and that each time the loop is run, one fewer <TT>Cat</TT> remains.</P>
<P>Note that <TT>HowManyCats</TT> is public and is accessed directly by <TT>main()</TT>.
There is no reason to expose this member variable in this way. It is preferable to
make it private along with the other member variables and provide a public accessor
method, as long as you will always access the data through an instance of <TT>Cat</TT>.
On the other hand, if you'd like to access this data directly without necessarily
having a <TT>Cat</TT> object available, you have two options: keep it public, as
shown in Listing 14.2, or provide a static member function, as discussed later in
this chapter.</P>
<BR>
<P><A NAME="Heading5"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 14.2. Accessing
static members without an object.</B></FONT>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">1:     //Listing 14.2 static data members
2:
3:     #include &lt;iostream.h&gt;
4:
5:     class Cat
6:     {
7:     public:
8:        Cat(int age):itsAge(age){HowManyCats++; }
9:        virtual ~Cat() { HowManyCats--; }
10:       virtual int GetAge() { return itsAge; }
11:       virtual void SetAge(int age) { itsAge = age; }
12:       static int HowManyCats;
13:
14:    private:
15:       int itsAge;
16:
17:    };
18:
19:    int Cat::HowManyCats = 0;
20:
21:    void TelepathicFunction();
22:
23:    int main()
24:    {
25:       const int MaxCats = 5; int i;
26:       Cat *CatHouse[MaxCats];
27:       for (i = 0; i&lt;MaxCats; i++)
28:       {
29:          CatHouse[i] = new Cat(i);
30:          TelepathicFunction();
31:       }
32:
33:       for ( i = 0; i&lt;MaxCats; i++)
34:       {
35:          delete CatHouse[i];
36:          TelepathicFunction();
37:       }
38:       return 0;
39:    }
40:
41:    void TelepathicFunction()
42:    {
43:       cout &lt;&lt; &quot;There are &quot;;
44:       cout &lt;&lt; Cat::HowManyCats &lt;&lt; &quot; cats alive!\n&quot;;
<TT>45: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: There are 1 cats alive!
There are 2 cats alive!
There are 3 cats alive!
There are 4 cats alive!
There are 5 cats alive!
There are 4 cats alive!
There are 3 cats alive!
There are 2 cats alive!
There are 1 cats alive!
There are 0 cats alive!
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>Listing 14.2 is much like
Listing 14.1 except for the addition of a new function, <TT>TelepathicFunction()</TT>.
This function does not create a <TT>Cat</TT> object, nor does it take a <TT>Cat</TT>
object as a parameter, yet it can access the <TT>HowManyCats</TT> member variable.
Again, it is worth reemphasizing that this member variable is not in any particular
object; it is in the class as a whole, and, if public, can be accessed by any function
in the program.</P>
<P>The alternative to making this member variable public is to make it private. If
you do, you can access it through a member function, but then you must have an object
of that class available. Listing 14.3 shows this approach. The alternative, static
member functions, is discussed immediately after the analysis of Listing 14.3.</P>
<BR>
<P><A NAME="Heading7"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 14.3. Accessing
static members using non-static member functions.</B></FONT>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">1:     //Listing 14.3 private static data members
2:
3:     #include &lt;iostream.h&gt;
4:
5:     class Cat
6:     {
7:     public:
8:        Cat(int age):itsAge(age){HowManyCats++; }
9:        virtual ~Cat() { HowManyCats--; }
10:       virtual int GetAge() { return itsAge; }
11:       virtual void SetAge(int age) { itsAge = age; }
12:       virtual int GetHowMany() { return HowManyCats; }
13:
14:
15:    private:
16:       int itsAge;
17:       static int HowManyCats;
18:    };
19:
20:    int Cat::HowManyCats = 0;
21:
22:    int main()
23:    {
24:       const int MaxCats = 5; int i;
25:       Cat *CatHouse[MaxCats];
26:       for (i = 0; i&lt;MaxCats; i++)
27:          CatHouse[i] = new Cat(i);
28:
29:       for (i = 0; i&lt;MaxCats; i++)
30:       {
31:          cout &lt;&lt; &quot;There are &quot;;
32:          cout &lt;&lt; CatHouse[i]-&gt;GetHowMany();
33:          cout &lt;&lt; &quot; cats left!\n&quot;;
34:          cout &lt;&lt; &quot;Deleting the one which is &quot;;
35:          cout &lt;&lt; CatHouse[i]-&gt;GetAge()+2;
36:          cout &lt;&lt; &quot; years old\n&quot;;
37:          delete CatHouse[i];
38:          CatHouse[i] = 0;
39:       }
40:     return 0;
<TT>41: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: There are 5 cats left!
Deleting the one which is 2 years old
There are 4 cats left!
Deleting the one which is 3 years old
There are 3 cats left!
Deleting the one which is 4 years old
There are 2 cats left!
Deleting the one which is 5 years old
There are 1 cats left!
Deleting the one which is 6 years old

?? 快捷鍵說(shuō)明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
亚洲va中文字幕| 色婷婷av一区| 老鸭窝一区二区久久精品| 亚洲成人综合网站| 亚洲一区二区美女| 亚洲一区二区三区四区中文字幕 | 国产精品久久久久9999吃药| 亚洲精品在线电影| 精品久久久久一区二区国产| 欧美成人在线直播| 久久综合久久综合九色| 久久免费精品国产久精品久久久久| 日韩一级免费观看| 26uuu亚洲综合色| 久久精品一区四区| 国产精品日韩成人| 国产精品久久久久久久久动漫 | 免费观看久久久4p| 精品一区二区免费看| 久久99精品国产麻豆不卡| 国产综合色在线视频区| 国产成人综合自拍| av在线不卡免费看| 色噜噜狠狠色综合中国| 欧美日韩国产在线观看| 日韩免费在线观看| 国产蜜臀97一区二区三区| 中文字幕五月欧美| 亚洲综合免费观看高清完整版| 亚洲成av人片在线观看| 美国欧美日韩国产在线播放| 激情综合五月天| 成人a区在线观看| 91久久奴性调教| 91麻豆精品国产自产在线| 日韩色视频在线观看| 国产亚洲va综合人人澡精品| 亚洲人成电影网站色mp4| 午夜精品在线看| 国产一区二区在线观看免费| 99视频精品全部免费在线| 欧美日韩久久久| 久久综合给合久久狠狠狠97色69| 国产精品国产三级国产aⅴ中文 | 欧美最猛黑人xxxxx猛交| 91麻豆精品久久久久蜜臀| 久久蜜桃av一区二区天堂| 最新欧美精品一区二区三区| 亚洲444eee在线观看| 国产乱码一区二区三区| 91麻豆福利精品推荐| 日韩一级欧美一级| 亚洲天堂福利av| 日韩专区欧美专区| 国产精品1区二区.| 欧美日韩在线播放一区| 国产亚洲精品中文字幕| 亚洲va韩国va欧美va| 国产精品18久久久久久久网站| 在线一区二区三区四区五区| 精品国产sm最大网站| 亚洲人精品午夜| 国产馆精品极品| 欧美另类z0zxhd电影| 国产精品久久二区二区| 男男成人高潮片免费网站| 99久精品国产| 久久久777精品电影网影网| 亚洲成人av中文| jizzjizzjizz欧美| 精品久久久久一区| 丝袜诱惑亚洲看片| 95精品视频在线| 久久先锋影音av鲁色资源| 亚洲第一综合色| 99精品热视频| 国产视频一区不卡| 蜜桃视频一区二区三区| 欧美性猛交xxxxxxxx| 亚洲欧洲日产国产综合网| 国产电影精品久久禁18| 日韩免费观看高清完整版在线观看| 一区二区成人在线| 成人午夜视频福利| 久久精品一区二区三区不卡 | 91精品91久久久中77777| 久久亚区不卡日本| 久久国产精品无码网站| 精品视频123区在线观看| 亚洲免费视频成人| 成人动漫av在线| 国产日韩欧美不卡在线| 激情欧美一区二区| 日韩精品一区二区三区在线播放 | 国产精品911| 欧美不卡一区二区| 免费观看成人av| 日韩一区二区中文字幕| 日产国产高清一区二区三区| 欧美日韩一二三| 亚洲国产日韩综合久久精品| 色播五月激情综合网| 亚洲欧美综合色| av在线不卡电影| 亚洲三级在线观看| 99久久久精品免费观看国产蜜| 国产精品美女久久久久aⅴ国产馆 国产精品美女久久久久av爽李琼 国产精品美女久久久久高潮 | 欧美日韩另类一区| 午夜天堂影视香蕉久久| 欧美视频在线观看一区二区| 亚洲一区二区三区四区在线 | 欧美性xxxxxxxx| 一区二区视频在线| 欧美体内she精高潮| 亚洲第一激情av| 欧美浪妇xxxx高跟鞋交| 日韩不卡一二三区| 9191精品国产综合久久久久久| 日韩精品一区第一页| 日韩一级免费观看| 国产在线不卡一卡二卡三卡四卡| 26uuu久久天堂性欧美| 国产精品亚洲第一区在线暖暖韩国 | 国产成人夜色高潮福利影视| 日本一区二区三区dvd视频在线| 丁香天五香天堂综合| 亚洲欧洲性图库| 欧美日韩亚洲另类| 秋霞午夜av一区二区三区| 精品久久久久久久久久久久久久久| 国产一区二三区好的| 国产精品乱码久久久久久| 色av综合在线| 丝袜脚交一区二区| 欧美精品一区视频| 国产999精品久久久久久绿帽| 中文字幕中文字幕中文字幕亚洲无线 | 99热在这里有精品免费| 一区二区久久久久| 精品理论电影在线| 成人av先锋影音| 亚洲国产日韩一级| 久久久夜色精品亚洲| 99久久婷婷国产综合精品| 天天影视色香欲综合网老头| 精品国偷自产国产一区| 99re这里只有精品首页| 五月激情综合网| 国产人伦精品一区二区| 欧美私人免费视频| 国产一区三区三区| 亚洲欧美激情插| 日韩精品中文字幕在线不卡尤物| 风间由美性色一区二区三区| 亚洲尤物在线视频观看| 26uuu欧美| 欧美系列一区二区| 丁香另类激情小说| 午夜伦欧美伦电影理论片| 日本一区二区三区四区在线视频 | 日韩一区欧美小说| 欧美一级黄色大片| 99精品视频在线观看免费| 日本91福利区| 亚洲人123区| 久久先锋影音av| 欧美日韩国产一级| 91在线观看地址| 国产中文一区二区三区| 亚洲高清免费一级二级三级| 国产亚洲成aⅴ人片在线观看| 欧美人动与zoxxxx乱| 91一区二区三区在线播放| 极品少妇xxxx精品少妇| 婷婷综合久久一区二区三区| 国产精品久久二区二区| 久久一区二区三区四区| 欧美一区二区三区视频免费播放| 99久久免费视频.com| 国产精品影视在线| 日本欧美一区二区在线观看| 综合av第一页| 国产欧美一区二区精品性色| 日韩欧美一区电影| 欧美日韩mp4| 色综合久久久久综合体| 粉嫩绯色av一区二区在线观看| 日本欧美一区二区| 亚洲国产视频直播| 一区二区三区在线视频免费| 亚洲欧美在线视频观看| 久久久国产午夜精品 | 亚洲成人激情社区| 一区二区三区日韩| 成人免费一区二区三区视频 | 久久精品999| 日韩电影网1区2区| 日日夜夜精品视频免费| 午夜精品久久久久久久久久| 一区二区三区91|