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

📄 xt5.htm

📁 有关C++程序设计的复习资料
💻 HTM
📖 第 1 页 / 共 2 页
字号:
            <P align=left><A class=mb14 
            href="http://www.nuist.edu.cn/JSJ/syjx/jxcg.htm"> 教学成果</A></P></DIV></TD></TR>
        <TR>
          <TD height=26>
            <DIV align=right><IMG height=15 src="xt5.files/article_common.gif" 
            width=9></DIV></TD>
          <TD>
            <DIV align=left><A 
            href="http://www.nuist.edu.cn/JSJ/syjx/jxkh.htm"> 教学考核</A></DIV></TD></TR>
        <TR>
          <TD height=26>
            <DIV align=right><IMG height=15 src="xt5.files/article_common.gif" 
            width=9></DIV></TD>
          <TD>
            <DIV align=left><A class=mb14 
            href="http://www.nuist.edu.cn/JSJ/syjx/zwpj.htm"> 自我评价</A></DIV></TD></TR>
        <TR>
          <TD height=26>
            <DIV align=right><IMG height=15 src="xt5.files/article_common.gif" 
            width=9></DIV></TD>
          <TD>
            <DIV align=left><A class=mb14 
            href="http://www.nuist.edu.cn/JSJ/syjx/jsgh.htm"> 建设规划</A></DIV></TD></TR>
        <TR>
          <TD height=26>
            <DIV align=right><IMG height=15 src="xt5.files/article_common.gif" 
            width=9></DIV></TD>
          <TD>
            <DIV align=left><A class=mb14 
            href="http://www.nuist.edu.cn/JSJ/syjx/jcxy.htm"> 教材选用</A></DIV></TD></TR>
        <TR>
          <TD height=2></TD>
          <TD></TD></TR></TBODY></TABLE></TD>
    <TD vAlign=top colSpan=2>
      <TABLE class=bg height=241 cellSpacing=0 cellPadding=0 width="100%" 
      border=0>
        <TBODY>
        <TR>
          <TD width="11%" height=33><IMG height=34 src="xt5.files/homedha.gif" 
            width=55><IMG height=34 src="xt5.files/homedhbg.gif" width=8></TD>
          <TD 
            width="89%"><!-- InstanceBeginEditable name="EditRegion4" --><SPAN 
            class=s14><STRONG>习题五</STRONG></SPAN><!-- InstanceEndEditable --></TD></TR>
        <TR>
          <TD colSpan=2><!-- InstanceBeginEditable name="EditRegion5" -->
            <TABLE cellSpacing=1 cellPadding=1 width="100%" border=1>
              <TBODY>
              <TR>
                <TD>
                  <DIV align=center>第5章 类和对象</DIV></TD></TR>
              <TR>
                <TD>
                  <P class=xt12>5.1 
                  任何类中允许有三种类型的数据public、private、protected,类中的数据和成员函数的默认类型为私有。</P>
                  <P class=xt12>5.2 
                  在类内部定义的private数据不能被不属于该类的成员函数来操作,定义为public的数据,函数可以在类的外部进行操作。</P>
                  <P class=xt12>5.3 以下程序的运行结果是 。<BR>#include 
                  &lt;iostream.h&gt;<BR>class CSample<BR>{ private:<BR> int 
                  i;<BR> static int k;<BR> public:<BR> CSample();<BR> void 
                  Display();<BR>};<BR>CSample::CSample()<BR>{ <BR> i=0;<BR> k++;<BR>}<BR>void 
                  CSample::Display()<BR>{<BR> Cout&lt;&lt;”i=”&lt;&lt;i&lt;&lt;”,k=”&lt;&lt;k&lt;&lt;endl;<BR>}<BR>int 
                  CSample::k=0;<BR>void main()<BR>{ CSample 
                  a,b;<BR> a.Display();<BR> b.Display();<BR>}<BR>答: 
                  i=0,k=2<BR>  i=0,k=2</P>
                  <P class=xt12>5.4 以下程序的运行结果是 。<BR>#include 
                  &lt;iostream.h&gt;<BR>Class CSample<BR>{<BR>  int 
                  i;<BR> public:<BR> CSample();<BR> void 
                  Display();<BR> ~Csample();<BR>};<BR>CSample::CSample()<BR>{<BR> cout&lt;&lt;”Constructor”&lt;&lt;”,”; <BR> i=0;<BR>}<BR>void 
                  CSample::Display()<BR>{<BR> cout&lt;&lt;”i=”&lt;&lt;i&lt;&lt;”,”;<BR>}<BR>CSample::~CSample()<BR>{<BR> cout&lt;&lt;”Destructor”&lt;&lt;endl;<BR>}<BR>void 
                  main()<BR>{ CSample 
                  a;<BR> a.Display();<BR>}<BR>答:Constructor,i=0,Destructor</P>
                  <P class=xt12>5.5 以下程序的运行结果是 。<BR>#include 
                  &lt;iostream.h&gt;<BR>class CSample<BR>{<BR> int 
                  i;<BR> public:<BR> CSample();<BR> CSample(int val);<BR> void 
                  Display();<BR> ~CSample();<BR>};<BR>CSample::CSample()<BR>{<BR> cout&lt;&lt;”Constructor1”&lt;&lt;endl;<BR> i=0;<BR>}<BR>CSample::CSample(int 
                  val)<BR>{<BR> cout&lt;&lt;”Contructor2”&lt;&lt;endl;<BR> i=val;<BR>}<BR>void 
                  CSample::Display()<BR>{<BR> cout&lt;&lt;”i=”&lt;&lt;i&lt;&lt;endl;<BR>}<BR>CSample::~CSample()<BR>{<BR> cout&lt;&lt;”Destructor”&lt;&lt;endl;<BR>}<BR>void 
                  main()<BR>{<BR> CSample 
                  a,b(10);<BR> a.Display();<BR> b.Display();<BR>}<BR>答:Constructor1<BR>Constructor2<BR>i=0<BR>i=10<BR>Destructor<BR>Destructor</P>
                  <P class=xt12>5.6 以下程序的运行结果是 。<BR>#include 
                  &lt;stdio.h&gt;<BR>class 
                  B<BR>{<BR> public:<BR> B(){};<BR> B(int i, int j);<BR> void 
                  printb();<BR> private:<BR> int a,b;<BR>};<BR>class 
                  A<BR>{<BR> public:<BR> A(){};<BR> A(int i, int j);<BR> void 
                  printa();<BR> private:<BR> B c;<BR>}<BR>A::A(int i, int 
                  j):c(i,j)<BR>{};<BR>void 
                  A::printa()<BR>{<BR> c.printb();<BR>}<BR>B::B(int i, int 
                  j)<BR>{<BR> a=i; b=j;<BR>}<BR>void 
                  B::printb()<BR>{<BR> cout&lt;&lt;”a=”&lt;&lt;a&lt;&lt;”,b=”&lt;&lt;b&lt;&lt;endl;<BR>}<BR>void 
                  main()<BR>{<BR> A m(7,8);<BR> m.printa();<BR>}<BR>答: 
                  a=7,b=8</P>
                  <P class=xt12>5.7 以下程序的运行结果是 。<BR>#include 
                  &lt;iostream.h&gt;<BR>class Sample<BR>{<BR> int 
                  x;<BR> public:<BR> void setx(int i){x=i};<BR> int 
                  putx(){return x};<BR>};<BR>void main()<BR>{<BR> Sample 
                  *p;<BR> Sample 
                  A[3];<BR> A[0].setx(5);<BR> A[1].setx(6);<BR> A[2].setx(7);<BR> p=&amp;A[0];<BR> for(int 
                  j=0;j&lt;3;j++)<BR> cout&lt;&lt;p++-&gt;putx()&lt;&lt;” 
                  ”;<BR> cout&lt;&lt;endl;<BR>}<BR>答:5,6,7</P>
                  <P class=xt12>5.8 以下程序的运行结果是 。<BR>#include 
                  &lt;iostream.h&gt;<BR>class A<BR>{ public:<BR> int 
                  x;<BR> A(int i){x=i;}<BR> void fun1(int j) <BR> { 
                  x+=j;<BR>  cout&lt;&lt;”fun1:”&lt;&lt;x&lt;&lt;endl;<BR> } 
                  <BR>void fun2(int j) <BR> { 
                  x+=j;<BR>  cout&lt;&lt;”fun2:”&lt;&lt;x&lt;&lt;endl;<BR> } 
                  <BR>};<BR>void main()<BR>{ A c1(2),c2(5);<BR> void 
                  (A::*pfun)(int)=A::fun1;<BR> (c1.*pfun)(5);<BR> pfun=A::fun2;<BR> (c2.*fun)(10);<BR>}<BR>答:fun1:7<BR>fun2:15</P>
                  <P class=xt12>5.9 以下程序的运行结果是 。<BR>#include 
                  &lt;iostream.h&gt;<BR>class Sample<BR>{<BR> int 
                  x;<BR> public:<BR> Sample(){};<BR> void setx(int 
                  i){x=i;}<BR> friend int fun(Sample B[],int n)<BR> { int 
                  m=0;<BR>  for (int i=0;i&lt;n;i++)<BR>  if(B[i].x&gt;m) 
                  m=B[i].x;<BR>  return m;<BR> }<BR>};<BR>void main()<BR>{ 
                  Sample A[10];<BR> int 
                  Arr[]={90,87,42,78,97,84,60,55,78,65};<BR> for(int 
                  i=0;i&lt;10;i++)<BR> A[i].setx(Arr[i]);<BR> cout&lt;&lt;fun(A,10)&lt;&lt;endl;<BR>}<BR>答:97</P>
                  <P class=xt12>5.10 改正以下程序中的错误<BR>#include 
                  &lt;iostream.h&gt;<BR>class point<BR>{<BR> int 
                  x1,x2;<BR> public:<BR> point(int x, int y);<BR>};<BR>void 
                  main()<BR>{ point 
                  data(5,5);<BR> cout&lt;&lt;data.x1&lt;&lt;endl;<BR> cout&lt;&lt;data.x2&lt;&lt;endl;<BR>}<BR>答:该程序中point类的构造函数定义不正确,在mian()函数中对数据成员的访问方法不正确。</P>
                  <P class=xt12>5.11 编写一个程序,设计一个产品类Product,其定义如下:<BR>class 
                  Product<BR>{<BR> char *name; //产品名称<BR> int price; 
                  //产品单价<BR> int quantity; //剩余产品数量<BR> public:<BR> Product(char 
                  *n, int p, int q); //构造函数<BR> ~Product(); //析构函数<BR> void 
                  buy(int money); //购买产品<BR> void get() const; 
                  //显示剩余产品数量<BR>};<BR>并用数据进行测试<BR>答:本题参考程序如下:<BR>#include 
                  &lt;iostream.h&gt;<BR>#include &lt;string.h&gt;<BR>class 
                  Product<BR>{<BR> char *name;<BR> int price;<BR> int 
                  quantity;<BR> public:<BR> Product(char *n, int p, int 
                  q);<BR> { name=new 
                  char[strlen(n)+1];<BR>  strcpy(name,n);<BR>  price=p;<BR>  quantity=q;<BR> }<BR> ~Product();<BR> {<BR>  if(name)<BR>  {delete[] 
                  name;<BR>  name=0;<BR> }<BR>}<BR>void buy(int money)<BR>{ int 
                  n,r;<BR> n=money/price;<BR> if(n&gt;quantity)<BR>  cout&lt;&lt;”数量不够”&lt;&lt;endl;<BR> else<BR> {<BR>  quantity=m;<BR>  r=money%price;<BR>  cout&lt;&lt;”产品:”&lt;&lt;name&lt;&lt;”单价:”&lt;&lt;price&lt;&lt;”元 
                  顾客”;<BR>  cout&lt;&lt;money&lt;&lt;”元,买了”&lt;&lt;n&lt;&lt;”台,剩余”&lt;&lt;r&lt;&lt;”元”&lt;&lt;endl;<BR> }<BR>}<BR>void 
                  get() 
                  const<BR>{<BR> cout&lt;&lt;”产品:”&lt;&lt;name&lt;&lt;”单价:”&lt;&lt;price&lt;&lt;”元 
                  剩余”&lt;&lt;quantity&lt;&lt;”台”&lt;&lt;endl;<BR>}<BR>};<BR>void 
                  main()<BR>{ Product 
                  p1(“电视机”,2000,15);<BR> p1.buy(7000);<BR> p1.get();<BR> p1.buy(4500);<BR> p1.get;<BR>}</P>
                  <P class=xt12>5.12 
                  编写一个程序计算两个给定长方形的面积,其中在设计类成员函数address()(用于计算两个长方形的总面积)时使用对象作为参数<BR>答: 
                  #include &lt;iostream.h&gt;<BR>#include 
                  &lt;iomanip.h&gt;<BR>class 
                  rectangle<BR>{<BR> private:<BR> float 
                  ledge,sedge;<BR> public:<BR> rectangle(){};<BR> rectangle(float 
                  a, float b)<BR> { <BR>  ledge=a;sedge=b;<BR> };<BR> float 
                  area()<BR> {return ledge*sedge;<BR> };<BR> void 
                  addarea(rectangle r1, rectangle 
                  r2)<BR> {<BR>  cout&lt;&lt;”总面积:”&lt;&lt;r1.ledge*r1.sedge+r2.ledge*r2.sedge&lt;&lt;endl;<BR> }<BR>};<BR>void 
                  main()<BR>{<BR> rectangle A(3.5,2.5),B(4.2,3.8), 
                  c;<BR> c.addarea(A,B);<BR>}<BR>本程序的执行结果为:<BR>总面积:24.71</P>
                  <P class=xt12>5.13 
                  编写一个程序输入3个学生的英语和计算机成绩,并按总分从高到低排序。要求设计一个学生类Student,其定义如下:<BR>class 
                  Student<BR>{<BR> int English, computer, 
                  total;<BR> public:<BR> void getscore(); //获取一个学生的成绩<BR> void 
                  display(); //显示一个学生的成绩<BR>};<BR>排序过程在main()函数中实现。<BR>答: 
                  #include &lt;iostream.h&gt;<BR>class Student<BR>{ int 
                  English,computer,total;<BR> public:<BR> void getscore(); 
                  <BR> void display(); <BR> int retotal()<BR> {return 
                  total;}<BR>};<BR>void 
                  Student::getscore()<BR>{<BR> cout&lt;&lt;”输入英语成绩:”;<BR> cin&gt;&gt;English;<BR> cout&lt;&lt;”输入计算机成绩:”;<BR> cin&gt;&gt;computer;<BR> total=English+computer;<BR>}<BR>void 
                  Student::display()<BR>{<BR> cout&lt;&lt;”英语=”&lt;&lt;English&lt;&lt;”计算机=”<BR> &lt;&lt;computer&lt;&lt;”总分=”&lt;&lt;total&lt;&lt;endl;<BR>}<BR>void 
                  main()<BR>{<BR> void sort(Student **,Student **);<BR> Student 
                  *A[3];<BR> for(int j=0;j&lt;3;j++)<BR> {<BR>  A[j]=new 
                  Student;<BR>  cout&lt;&lt;”学生”&lt;&lt;j+1&lt;&lt;endl;<BR>  A[j]-&gt;getscore();<BR> }<BR> int 
                  i;<BR> for(j=0;j&lt;2;j++)<BR>  for(i=0;i&lt;2;i++)<BR>   sort(A+i,A+i+1);<BR>   cout&lt;&lt;endl&lt;&lt;”排序结果如下:”&lt;&lt;endl;<BR>  for(i=0;i&lt;3;i++)<BR>  A[i]-&gt;display();<BR>}<BR>void 
                  sort(Student **p1,Student 
                  **p2)<BR>{<BR> if((*p1)-&gt;retotal()&lt;(*p2) 
                  -&gt;retotal())<BR> {<BR>  Student 
                  *tmp=*p1;<BR>  *p1=*p2;<BR>  *p2=tmp;<BR> }<BR>}<BR></P></TD></TR></TBODY></TABLE><!-- InstanceEndEditable --></TD></TR></TBODY></TABLE></TD></TR>
  <TR vAlign=center bgColor=#eeeeee>
    <TD colSpan=3 height=28>
      <DIV align=center><A 
      onclick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.nuist.edu.cn')" 
      href="http://www.nuist.edu.cn/JSJ/syjx/xt/xt5.htm#"><SPAN 
      class=mb14>设为首页</SPAN></A><STRONG>|</STRONG><A class=mb14 
      href="javascript:addme()">加入收藏</A><STRONG>|</STRONG><A 
      href="http://www.nuist.edu.cn/"><SPAN 
      class=mb14>学校首页</SPAN></A><STRONG>|</STRONG><A 
      href="http://www.nuist.edu.cn/jsj"><SPAN 
    class=mb14>计科系首页</SPAN></A></DIV></TD></TR>
  <TR>
    <TD class=mb14 vAlign=top bgColor=#176bab colSpan=2 height=88>
      <DIV align=center><BR><FONT color=#ffffff>Copyright&copy; 2004-2005 Depart of 
      Computer Science &amp; Technology Nuist</FONT></DIV></TD>
    <TD width=15 bgColor=#176bab>&nbsp;</TD></TR>
  <TR>
    <TD height=1></TD>
    <TD width=581></TD>
    <TD></TD></TR></TBODY></TABLE><MAP name=Map><AREA shape=RECT coords=9,9,75,25 
  href="http://www.nuist.edu.cn/JSJ/syjx/index.htm"><AREA shape=RECT 
  coords=86,9,150,26 href="http://www.nuist.edu.cn/JSJ/syjx/jxdg.htm"><AREA 
  shape=RECT coords=157,9,220,27 
  href="http://www.nuist.edu.cn/JSJ/syjx/skja.htm"><AREA shape=RECT 
  coords=230,9,310,26 href="http://www.nuist.edu.cn/JSJ/syjx/dmt.htm"><AREA 
  onclick="MM_goToURL('parent','../xt.htm');return document.MM_returnValue" 
  shape=RECT coords=320,10,397,26 
  href="http://www.nuist.edu.cn/JSJ/syjx/xt.htm"><AREA 
  onclick="MM_showHideLayers('Layer1','','hide','Layer2','','show')" shape=RECT 
  coords=405,10,469,25 href="http://www.nuist.edu.cn/JSJ/syjx/xt/xt5.htm#"><AREA 
  onclick="MM_showHideLayers('Layer1','','show','Layer2','','hide')" shape=RECT 
  coords=476,10,538,26 href="http://www.nuist.edu.cn/JSJ/syjx/xt/xt5.htm#"><AREA 
  shape=RECT coords=549,10,612,26 
  href="http://www.nuist.edu.cn/JSJ/syjx/ckwx.htm"><AREA shape=RECT 
  coords=620,9,684,26 
href="http://www.nuist.edu.cn/JSJ/syjx/zlxz.htm"></MAP></CENTER><!-- InstanceEnd --></BODY></HTML>

⌨️ 快捷键说明

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