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

📄 example3-14.htm

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

<head>
<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-14</title>
</head>

<body>

<blockquote>
  <blockquote>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">#include 
    &lt;iostream.h&gt;</font></p>
    <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( 
      );</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">void 
      GetSum(int a);</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int 
      Sum;</font></p>
    </blockquote>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">private:</font></p>
    <blockquote>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int 
      A;</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">Myclass::Myclass( 
    )</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{ 
    </font></p>
    <blockquote>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">Sum=10;</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">void 
    Myclass::GetSum(int a)</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
    <blockquote>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">A=a;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">Sum+=A;</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">void 
    PrintSum(Myclass&amp; A)</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
    <blockquote>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#FF6600">cout&lt;&lt;&quot;Sum=&quot;&lt;&lt;A.Sum&lt;&lt;endl; 
      //<font FACE="??ì?,SimSun" LANG="ZH-CN">由于</font>Sum<font FACE="??ì?,SimSun" LANG="ZH-CN">是一个公共数据成员,因此可以在非成员函数中访问。</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">void 
    main( )</font></p>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
    <blockquote>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">Myclass 
      M,N;</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">M.GetSum(3);</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">N.GetSum(7);</font></p>
      <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">PrintSum(M);</font></p>
    </blockquote>
    <p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">}</font></p>
  </blockquote>
</blockquote>
<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">sum=13</font></p>
  </blockquote>
</blockquote>
<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>M<font FACE="??ì?,SimSun" LANG="ZH-CN">和</font>N<font FACE="??ì?,SimSun" LANG="ZH-CN">是两个</font>Myclass<font FACE="??ì?,SimSun" LANG="ZH-CN">对象,各自都有独立的数据成员</font>Sum<font FACE="??ì?,SimSun" LANG="ZH-CN">,程序中只输出了</font>M<font FACE="??ì?,SimSun" LANG="ZH-CN">对象的数据成员</font>Sum<font FACE="??ì?,SimSun" LANG="ZH-CN">的值。</font></font></p>

</body>

</html>

⌨️ 快捷键说明

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