📄 cjj102.htm
字号:
<!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<iostream.h><BR>
class Sample<BR>
{<BR>
public:<BR>
int x;<BR>
int y;<BR>
void
disp()<BR>
{<BR>
cout<<"x="<<x<<",y="<<y<<endl;<BR>
}<BR>
};<BR>
<BR>
void main()<BR>
{<BR>
int Sample::*pc;<BR>
Sample s;<BR>
pc=&Sample::x;<BR>
s.*pc=10;<BR>
pc=&Sample::y;<BR>
s.*pc=20;<BR>
s.disp();<BR>
}<BR>
<BR>
解:本题说明了类数据成员指针的使用方法。在main()中定义的pc是一个指向Sample类数据成员的指针。执行pc=&Sample::x时,pc指向数据成员x,语句s.*pc=10等价于s.x=10(为了保证该语句正确执行,Sample类中的x必须是公共成员);执行pc=&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>
Test a;<BR>
a.init(68,55);<BR>
a.print();<BR>
}<BR>
<BR>
其执行结果为:<BR>
测试结果:68-55=13<BR>
<BR>
解:本题是要设计Test类,其设计方法很多,这里给出一种解法。Test类包含两个私有数据成员x、y,以及两个公共成员函数init()和print(),前者用于给数据成员赋值,后者用于x,y的减法运算和输出相应的结果。<BR>
#include<iostream.h><BR>
class Test<BR>
{<BR>
int x,y;<BR>
public:<BR>
void
init(int,int);<BR>
void
print();<BR>
};<BR>
<BR>
void Test::init(int i,int j)<BR>
{<BR>
x=i;y=j;<BR>
}<BR>
<BR>
void Test::print()<BR>
{<BR>
cout<<"测试结果:"<<x<<"-"<<y<<"="<<x-y<<endl;<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -