📄 xt5.htm
字号:
<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
<iostream.h><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<<”i=”<<i<<”,k=”<<k<<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
<iostream.h><BR>Class CSample<BR>{<BR> int
i;<BR> public:<BR> CSample();<BR> void
Display();<BR> ~Csample();<BR>};<BR>CSample::CSample()<BR>{<BR> cout<<”Constructor”<<”,”; <BR> i=0;<BR>}<BR>void
CSample::Display()<BR>{<BR> cout<<”i=”<<i<<”,”;<BR>}<BR>CSample::~CSample()<BR>{<BR> cout<<”Destructor”<<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
<iostream.h><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<<”Constructor1”<<endl;<BR> i=0;<BR>}<BR>CSample::CSample(int
val)<BR>{<BR> cout<<”Contructor2”<<endl;<BR> i=val;<BR>}<BR>void
CSample::Display()<BR>{<BR> cout<<”i=”<<i<<endl;<BR>}<BR>CSample::~CSample()<BR>{<BR> cout<<”Destructor”<<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
<stdio.h><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<<”a=”<<a<<”,b=”<<b<<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
<iostream.h><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=&A[0];<BR> for(int
j=0;j<3;j++)<BR> cout<<p++->putx()<<”
”;<BR> cout<<endl;<BR>}<BR>答:5,6,7</P>
<P class=xt12>5.8 以下程序的运行结果是 。<BR>#include
<iostream.h><BR>class A<BR>{ public:<BR> int
x;<BR> A(int i){x=i;}<BR> void fun1(int j) <BR> {
x+=j;<BR> cout<<”fun1:”<<x<<endl;<BR> }
<BR>void fun2(int j) <BR> {
x+=j;<BR> cout<<”fun2:”<<x<<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
<iostream.h><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<n;i++)<BR> if(B[i].x>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<10;i++)<BR> A[i].setx(Arr[i]);<BR> cout<<fun(A,10)<<endl;<BR>}<BR>答:97</P>
<P class=xt12>5.10 改正以下程序中的错误<BR>#include
<iostream.h><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<<data.x1<<endl;<BR> cout<<data.x2<<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
<iostream.h><BR>#include <string.h><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>quantity)<BR> cout<<”数量不够”<<endl;<BR> else<BR> {<BR> quantity=m;<BR> r=money%price;<BR> cout<<”产品:”<<name<<”单价:”<<price<<”元
顾客”;<BR> cout<<money<<”元,买了”<<n<<”台,剩余”<<r<<”元”<<endl;<BR> }<BR>}<BR>void
get()
const<BR>{<BR> cout<<”产品:”<<name<<”单价:”<<price<<”元
剩余”<<quantity<<”台”<<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 <iostream.h><BR>#include
<iomanip.h><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<<”总面积:”<<r1.ledge*r1.sedge+r2.ledge*r2.sedge<<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 <iostream.h><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<<”输入英语成绩:”;<BR> cin>>English;<BR> cout<<”输入计算机成绩:”;<BR> cin>>computer;<BR> total=English+computer;<BR>}<BR>void
Student::display()<BR>{<BR> cout<<”英语=”<<English<<”计算机=”<BR> <<computer<<”总分=”<<total<<endl;<BR>}<BR>void
main()<BR>{<BR> void sort(Student **,Student **);<BR> Student
*A[3];<BR> for(int j=0;j<3;j++)<BR> {<BR> A[j]=new
Student;<BR> cout<<”学生”<<j+1<<endl;<BR> A[j]->getscore();<BR> }<BR> int
i;<BR> for(j=0;j<2;j++)<BR> for(i=0;i<2;i++)<BR> sort(A+i,A+i+1);<BR> cout<<endl<<”排序结果如下:”<<endl;<BR> for(i=0;i<3;i++)<BR> A[i]->display();<BR>}<BR>void
sort(Student **p1,Student
**p2)<BR>{<BR> if((*p1)->retotal()<(*p2)
->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© 2004-2005 Depart of
Computer Science & Technology Nuist</FONT></DIV></TD>
<TD width=15 bgColor=#176bab> </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 + -