📄 cjj114.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>C++习题与解析(引用-04)</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++习题与解析(引用-04)</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="body12black" >题6.阅读下面的程序与输出结果,添加一个拷贝构造函数来完善整个程序<BR>
#include<iostream.h><BR>class
Cat<BR>{<BR>
public:<BR>
Cat();<BR>
Cat(const Cat
&);<BR>
~Cat();<BR>
int getage()const{return
*itsage;}<BR>
void setage(int
age){*itsage=age;}<BR>
protected:<BR>
int
*itsage;<BR>};<BR>Cat::Cat()<BR>{<BR>
itsage=new int;<BR>
*itsage=5;<BR>}<BR>Cat::~Cat()<BR>{<BR>
delete itsage;<BR>
itsage=0;<BR>}<BR>void
main()<BR>{<BR> Cat
frisky;<BR>
cout<<"frisky's
age:"<<frisky.getage()<<endl;<BR>
cout<<"setting frisky to
6...\n";<BR>
frisky.setage(6);<BR>
cout<<"creating boots from
frisky\n";<BR> Cat
boots(frisky);<BR>
cout<<"frisky's
age:"<<frisky.getage()<<endl;<BR>
cout<<"boots'age:"<<boots.getage()<<endl;<BR>
cout<<"setting frisky to
7...\n";<BR>
frisky.setage(7);<BR>
cout<<"frisky's
age:"<<frisky.getage()<<endl;<BR>
cout<<"boots'age:"<<boots.getage()<<endl;<BR>}<BR><BR>当添加了拷贝构造函数后,程序的运行结果为:<BR>frisky's
age:5<BR>setting frisky to 6...<BR>creating boots
from frisky<BR>frisky's
age:6<BR>boots'age:6<BR>setting frisky to
7...<BR>frisky's
age:7<BR>boots'age:6<BR><BR>解:<BR>
添加的拷贝构造函数如下:<BR>Cat::Cat(const Cat&
c)<BR>{<BR> itsage=new
int;<BR>
*itsage=*c.itsage;<BR>}<BR><BR>-----------------------------------------------<BR><BR><FONT
color=#0000ff>题7.设计一个类Sample,有一个私有数据成员,建立该类的四个对象s1(n=10)、s2(n=20)、s3(n=30)、和s4(n=40),建立一个成员函数实现这些对象n值的累加。<BR></FONT>解:<BR>
依题意,建立一个成员函数add(),其参数为Sample对象引用,用于累加对象n值。<BR>程序如下:<BR>#include<iostream.h><BR>class
Sample<BR>{<BR> int
n;<BR>
public:<BR>
Sample(){}<BR>
Sample (int
i){n=i;}<BR>
void add(Sample
&s) //
对象引用作为参数<BR>
{<BR>
if(&s==this) //
不能自己相加,this是当前对象的指针<BR>
cout<<"自己不能相加"<<endl;<BR>
else
n+=s.n;<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -