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

📄 cjj102.htm

📁 c++的一些程序
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML><HEAD><TITLE>C++习题与解析(类和对象-02)</TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<SCRIPT language=JavaScript>
var currentpos,timer;

function initialize()
{
timer=setInterval("scrollwindow()",50);
}
function sc(){
clearInterval(timer);
}
function scrollwindow()
{
currentpos=document.body.scrollTop;
window.scroll(0,++currentpos);
if (currentpos != document.body.scrollTop)
sc();
}
document.onmousedown=sc
document.ondblclick=initialize
</SCRIPT>

<META content="MSHTML 5.00.2614.3500" name=GENERATOR>
<link rel="stylesheet" href="body1.css" type="text/css">
</HEAD>
<BODY topMargin=0 marginheight="0" bgcolor="#CCCCCC">
<TABLE align=center border=1 cellPadding=0 cellSpacing=1 
style="BORDER-COLLAPSE: collapse" width=550>
 <TBODY>
  <TR>
    <TD bgColor=#c1c1c1 height=35> <img src="jsdd.gif" width="159" height="57" > <img src="jjdd.gif" ></TD>
  </TR>
 </TBODY>
</TABLE>


<TABLE align=center border=1 cellPadding=0 cellSpacing=1 
style="BORDER-COLLAPSE: collapse" width=550>
  <TBODY>
  <TR>
    <TD width="100%">
      <TABLE border=0 borderColor=#e2ca9f cellPadding=0 cellSpacing=0 
      width="100%">
        <TBODY>
        <TR>
          <TD align=middle vAlign=top width="95%">
            <TABLE border=1 borderColor=#e2ca9f cellPadding=0 cellSpacing=0 
            width="100%">
              <TBODY>
              <TR>
                <TD align=middle background=002.gif 
                borderColor=#e2ca9f vAlign=top width="69%">
                  <TABLE align=center border=0 cellPadding=0 cellSpacing=0 
                  width="100%">
                    <TBODY> 
                    <TR> 
                      <TD height=35 width="100%"></TD>
                    </TR>
                    <TR> 
                      <TD align=middle bgColor=#dddddd height=20 
                      style="FONT-SIZE: 18px" vAlign=bottom 
                        width="85%" class="body18black">C++习题与解析(类和对象-02)</TD>
                      <BR>
                    </TR>
                    <TR> 
                      <TD align=middle width="100%"><BR>
                      </TD>
                    </TR>
                    <TR> 
                      <TD align=middle width="100%"> 
                        <!--下面的这一句是设置阅读文本区的宽度-->
                        
                        <TABLE align=center border=0 cellPadding=0 cellSpacing=0  width="75%">
                          <TBODY> 
                          <TR> 
                            <TD align=middle width="100%"></TD>
                          </TR>
                          <TR> 
                            <TD class="body12blue" > 题 1.5 分析以下程序的执行结果<span class="body12black"><BR>
                              #include&lt;iostream.h&gt;<BR>
                              class Sample<BR>
                              {<BR>
                              &nbsp;&nbsp;&nbsp; public:<BR>
                              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int x;<BR>
                              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int y;<BR>
                              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void 
                              disp()<BR>
                              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>
                              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                              cout&lt;&lt;"x="&lt;&lt;x&lt;&lt;",y="&lt;&lt;y&lt;&lt;endl;<BR>
                              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>
                              };<BR>
                              <BR>
                              void main()<BR>
                              {<BR>
                              &nbsp;&nbsp;&nbsp; int Sample::*pc;<BR>
                              &nbsp;&nbsp;&nbsp; Sample s;<BR>
                              &nbsp;&nbsp;&nbsp; pc=&amp;Sample::x;<BR>
                              &nbsp;&nbsp;&nbsp; s.*pc=10;<BR>
                              &nbsp;&nbsp;&nbsp; pc=&amp;Sample::y;<BR>
                              &nbsp;&nbsp;&nbsp; s.*pc=20;<BR>
                              &nbsp;&nbsp;&nbsp; s.disp();<BR>
                              }<BR>
                              <BR>
                              解:本题说明了类数据成员指针的使用方法。在main()中定义的pc是一个指向Sample类数据成员的指针。执行pc=&amp;Sample::x时,pc指向数据成员x,语句s.*pc=10等价于s.x=10(为了保证该语句正确执行,Sample类中的x必须是公共成员);执行pc=&amp;Sample::y时,pc指向数据成员y,语句s.*pc=20等价于s.y=20(同样,Sample类中的y必须是公共成员)。所以输出为: 
                              x=10,y=20。 </span> 
                              <P class="body12black">-----------------------------------------------------</P>
                              <P class="body12black"><FONT color=#0000ff>题 1.6 
                                下面是一个类的测试程序,设计出能使用如下测试程序的类。</FONT><BR>
                                void main()<BR>
                                {<BR>
                                &nbsp;&nbsp;&nbsp; Test a;<BR>
                                &nbsp;&nbsp;&nbsp; a.init(68,55);<BR>
                                &nbsp;&nbsp;&nbsp; a.print();<BR>
                                }<BR>
                                <BR>
                                其执行结果为:<BR>
                                &nbsp;&nbsp;&nbsp; 测试结果:68-55=13<BR>
                                <BR>
                                解:本题是要设计Test类,其设计方法很多,这里给出一种解法。Test类包含两个私有数据成员x、y,以及两个公共成员函数init()和print(),前者用于给数据成员赋值,后者用于x,y的减法运算和输出相应的结果。<BR>
                                #include&lt;iostream.h&gt;<BR>
                                class Test<BR>
                                {<BR>
                                &nbsp;&nbsp;&nbsp; int x,y;<BR>
                                &nbsp;&nbsp;&nbsp; public:<BR>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void 
                                init(int,int);<BR>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void 
                                print();<BR>
                                };<BR>
                                <BR>
                                void Test::init(int i,int j)<BR>
                                {<BR>
                                &nbsp;&nbsp;&nbsp; x=i;y=j;<BR>
                                }<BR>
                                <BR>
                                void Test::print()<BR>
                                {<BR>
                                &nbsp;&nbsp;&nbsp; cout&lt;&lt;"测试结果:"&lt;&lt;x&lt;&lt;"-"&lt;&lt;y&lt;&lt;"="&lt;&lt;x-y&lt;&lt;endl;<BR>

⌨️ 快捷键说明

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