面向对象程序设计C山师习题答案.docx
《面向对象程序设计C山师习题答案.docx》由会员分享,可在线阅读,更多相关《面向对象程序设计C山师习题答案.docx(30页珍藏版)》请在冰豆网上搜索。
面向对象程序设计C山师习题答案
第六章习题答案
选择填空
1
、
A2、C
3、
D
4
、B
5
、D
6
、
A7
、C
8、
A
9、D10
、A
11
、
C12
、A
13
、B
14、
C
15、C
16
、
D
17
、
B18
、C
19
、A
20
、D
21
、C22、
B
、
判
断下列描述的正确性
生
对
者
划V,
错者划X。
1
、
V2、
X
3
、
X
4
、X
5、
V
6、
V
7、
X
8、V
9
、
X
10
、
V
11
、V12、
V
13
、
V
14
、V
15、
X
16、
V
17、
V
18、V
19
、
V
20
、
X
21
、X22、
X
三、分析下列程序的输出结果。
1、运行该程序输出结果如下所示
Defaultconstructorcalled
Constructorcalled
a=0,b=0
a=4,b=8
2、运行该程序输出结果如下所示
a=7,b=9
3、运行该程序输出结果如下所示
104
4、运行该程序输出结果如下所示。
1035,789.504
5、运行该程序输出结果如下所示。
1
{}
{0,1,2,3,4,5,6,7,8}
1
{11,12,13,14,15,16,17,18,19}
{19,18,17,16,15,14,13,12,11}
6、运行该程序输出结果如下所示。
Starting1:
Defaultconstructorcalled.
Defaultconstructorcalled.
Defaultconstructorcalled.Eding1:
Starting2:
Constructor:
a=5,b=6
Constructor:
a=7,b=8
Constructor:
a=9,b=10
Ending2:
Destructorcalled.a=9,b=10
Destructorcalled.a=7,b=8
Destructorcalled.a=5,b=6
Destructorcalled.a=5,b=6
Destructorcalled.a=3,b=4
Destructorcalled.a=1,b=2
7、运行该程序输出结果如下所示Defaultconstructorcalled.Defaultconstructorcalled.Defaultconstructorcalled.Defaultconstructorcalled.Destructorcalled.
Constructor1called.
Destructorcalled.
Constructor2called.
Destructorcalled.
x=0,y=0
x=5,y=0
x=2,y=3
Destructorcalled.
Destructorcalled.
Destructorcalled.
8、运行该程序输出结果如下所示。
Constructorcalled.0
Constructorcalled.5
Destructorcalled.5
5
Destructorcalled.5
9、运行该程序输出结果如下所示。
Constructorcalled.5
5
Destructorcalled.5
10、运行该程序输出结果如下所示
DefaultConstructorcalled.
Constructor:
real=6.8,imag=0
Constructor:
real=5.6,imag=7.9
0+0I
6.8+0I
5.6+7.9I
Constructor:
real=1.2,imag=3.4
Constructor:
real=5,imag=0
DefaultConstructorcalled.
1.2+3.4I
5+0I
0+0I
11、答:
⑴该程序中用string.h中所包含的函数有3种,它们是strcpy()、strcat()和strlen().
⑵该程序中使用了函数重载。
它们是下述两个构造函数重载
String()和String(constchar*str)。
⑶类中成员函数Setc()的功能是用来设置字符数组中某位置的一个指定字符。
⑷类中成员函数Getc()的功能是用来从某个字符数组中获取指定位置的字符。
⑸类中成员函数Append()的功能是在一个字符数组中追加一个指定的字符串,即将指定的字符串添加到已知串的后边。
⑹不行。
⑺该程序中有两处使用了new运算符。
⑻运行该程序输出结果如下所示。
empty.
astring.
9
astring.
thisastring.
四、改正以下程序中的错误。
1、该程序中point类的构造函数定义不正确,在main()中队数据成员的访问不正确,修改如下:
#include
classpoint
{
intx1,x2;
public:
point(intx,inty){point:
:
x=x;point:
:
y=y;}
voiddisp()
{
cout<cout<}
//……
};
voidmain()
{
pointdata(5,5);
data.disp();
}
2、在main()函数中的p.x+=5和p.y+=6两个语句是错误的,因为保护数据仅能被类的成员函数访问。
五、按下列要求编写程序。
1、程序内容如下所示。
#include
classTest
{
public:
Test(){}
Test(inti,intj=20)
{
t1=i;
t2=j;
t+=j-i;
}
staticintfun(Test&T);
friendintadd(Test&T);
private:
intt1,t2;
staticintt;
};
intTest:
:
fun(Test&T)
{
t+=T.t;
returnt;
}
intadd(Test&T)
{
intn=T.t1+T.t2;
returnn;
}
intTest:
:
t=5;
voidmain()
{
Testa1,a2(10),a3(15,25);
cout<cout<:
fun(a2)<}
2、程序内容如下所示。
#include
#include
classProduct
{
char*name;
intprice;
intquantity;
public:
Product(char*n,intp,intq)
{
name=newchar[strlen(n)+1];strcpy(name,n);
price=p;
quantity=q;
}
~Product()
{
if(name)
{
delete[]name;
name=0;
}
voidbuy(intmoney)
{
intn,r;
n=money/price;
if(n>quantity)
cout<<"数量不够"<else
{
quantity-=n;
r=money%price;
cout<<"产品:
"<"<cout<}
}
voidget()const
{
cout<<"产品:
"<"<}
};
voidmain()
{
Productp1("电视机",,15);
p1.buy(7000);
p1.get();
p1.buy(4500);
p1.get();
}
3、程序内容如下所示。
#include
#include
classCDate
{
private:
intm_nDay;
intm_nMonth;
intm_nYear;
public:
CDate();
CDate(intday,intmonth,intyear);
voidDisplay();
voidAddDay();
voidSetDate(intday,intmonth,intyear);
~CDate();
private:
boolIsLeapYear();//判断该年是否为闰年};
CDate:
:
CDate()
{
}
CDate:
:
CDate(intday,intmonth,intyear)
{
m_nDay=day;
m_nMonth=month;m_nYear=year;
}
voidCDate:
:
Display()
{
charday[5];
charmonth[5];
charyear[5];
_itoa(m_nDay,day,10);
_itoa(m_nMonth,month,10);
_itoa(m_nYear,year,10);
cout<}
voidCDate:
:
AddDay()
{
m_nDay++;
if(IsLeapYear())
{
if((m_nMonth==2)&&(m_nDay==30))
{
m_nMonth++;
m_nDay=1;
return;
}
}
else
{
if((m_nMonth==2)&&(m_nDay==29))
{
m_nMonth++;
m_nDay=1;
return;
}
}if(m_nDay>31)
{
if(m_nMonth==12)
{
m_nYear++;m_nMonth=1;
m_nDay=1;
}
else
{
m_nMonth++;m_nDay=1;
}
}
}
voidCDate:
:
SetDate(intday,intmonth,intyear)
m_nDay=day;
m_nMonth=month;m_nYear=year;
}
CDate:
:
~CDate()
{
}
boolCDate:
:
IsLeapYear()
{
bool