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

📄 right3-4.htm

📁 Visual C++面向对象程序设计教程(配套习题资源)
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>第 3 章</title>
<base target="rbottom">
<style>
<!--
.右标题   { font-size: 10pt; color: #000080; text-indent: 0; margin: 0 }
.右内容   { font-size: 10pt; text-align: left; text-indent: 0; line-height: 100%; margin: 
               0 }
-->
</style>
</head>

<body>

<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><span style="position: absolute; left: 4; top: -8"><font size="2"><img border="0" src="1.gif" width="63" height="70"></font></span></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left"><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                          
&nbsp;&nbsp;&nbsp; <span style="background-color: #CCFF99">第 3 章&nbsp; </span>&gt;<span style="background-color: #CCFF99"> 第  
4 节 </span>&gt;<span style="background-color: #CCFF99">  
 3.4 静态成员</span></font></p>          
<hr color="#008000" size="1">

<p style="line-height: 150%; text-indent: 0; margin: 0" class="右标题" align="left"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">同一种“类”的对象如何实现数据共享呢?</font>C++<font FACE="??ì?,SimSun" LANG="ZH-CN">中可以将数据成员设置为静态数据成员来实现数据的共享。一个给定类的每个对象都有类中定义的所有数据的拷贝,如果在类中将数据成员用<b><font color="#FF6600">static</font></b>说明为静态的,则这个类成员只有一个拷贝并被这个类的所有对象所共享。</font></font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font FACE="oúì?,SimHei" LANG="ZH-CN" size="2">1.  
静态数据成员</font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">如果类的数据成员为静态的,称为静态数据成员。静态数据成员有下面两个特点:</font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">①  
公有静态数据成员可被类的外部访问,保护或私有的静态数据成员只可被类的内部访问。</font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">②  
静态数据成员是类的所有对象的共享成员,而不是属于某个对象的成员。</font></p>
<blockquote>
  <blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">class  
    Myclass</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">public:</font></p>
    <blockquote>
      <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">Myclass(int 
      a,int b,int c);</font></p>
      <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">void 
      PrintNumber( );</font></p>
      <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">void 
      PrintSum( );</font></p>
      <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">private:</font></p>
      <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int 
      A,B,C;</font></p>
      <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font color="#008000">static</font><font color="#FF6600"> 
      </font>int Sum; <font color="#008000">//静态数据成员</font></font></p> 
    </blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">};</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">int 
    Myclass</font><font size="2" color="#FF6600">::</font><font size="2" color="#008000">Sum=0;</font><font size="2" color="#FF6600"> 
    //<font FACE="??ì?,SimSun" LANG="ZH-CN">必须在类外对静态数据成员初始化</font></font></p> 
  </blockquote>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2" color="#FF6600">注意:</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">①  
静态数据成员作为全局变量,其存储空间是在类外分配的,因此静态数据成员不能在类内初始化,也不能用构造函数初始化静态数据成员。</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">② 
静态数据成员初始化时不需要加</font>static<font FACE="??ì?,SimSun" LANG="ZH-CN">,但静态数据成员前面必须使用作用域运算符“</font>::<font FACE="??ì?,SimSun" LANG="ZH-CN">”来标明它所属的类。</font></font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"> </p>
<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example3-13.htm" target="_blank">【例</a></font><a href="example3-13.htm" target="_blank">3-13<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">指出下面程序的输出结果。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example3-14.htm" target="_blank">【例</a></font><a href="example3-14.htm" target="_blank">3-14<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">指出下面程序的输出结果。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example3-15.htm" target="_blank">【例</a></font><a href="example3-15.htm" target="_blank">3-15<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">指出下面程序的输出结果。</font></font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font FACE="oúì?,SimHei" LANG="ZH-CN" size="2">2.  
静态成员函数</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">如果类的成员函数使用</font>static<font FACE="??ì?,SimSun" LANG="ZH-CN">关键字声明为静态的,称为静态成员函数。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">同静态数据成员一样,静态成员函数也属于整个类,由一个类的所有对象共同维护,为这些对象所共享。</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">作为成员函数,它的访问属性可以受到类的严格控制。<font color="#008000">对于公有的静态成员函数,可以通过类名或对象名来调用</font>;而一般的非静态成员函数只能通过对象名来调用。</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2" color="#008000">静态成员函数可以直接访问该类的静态数据和函数成员;而访问非静态数据成员,必须通过参数传递方式得到对象名,然后通过对象名来访问。</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example3-16.htm" target="_blank">【例</a></font><a href="example3-16.htm" target="_blank">3-16<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">指出下面程序中的错误。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example3-17.htm" target="_blank">【例</a></font><a href="example3-17.htm" target="_blank">3-17<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">指出下面程序的输出结果。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example3-18.htm" target="_blank">【例</a></font><a href="example3-18.htm" target="_blank">3-18<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">指出下面程序的输出结果。</font></font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"> </p>

<hr size="1" color="#008000">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">&nbsp;&nbsp; <span style="position: absolute; left: 35; top: 823"><a href="right3-3.htm" target="_self"><img border="0" src="rightd1.gif" width="113" height="70"></a></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;             
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="position: absolute; left: 488; top: 827"><a href="right3-5.htm" target="_self"><img border="0" src="rightd2.gif" width="124" height="63"></a></span></font></p>     

</body>

</html>

⌨️ 快捷键说明

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