return0。
}
voidchange(int&a1,int&b1)
{inttemp。
temp=a1。
a1=b1。
b1=temp。
}
【2.31】编写C++风格的程序,用二分法求解f(x)=0的根。
【解】实现本题功能的程序如下:
#include
#include
usingnamespacestd。
inlinefloatf(floatx)
{return2*x*x*x-4*x*x+3*x-6。
}
intmain()
{floatleft,right,middle,ym,yl,yr。
cout<<"pleasetwonumber:
"<//接收输入,确定第一组数据区域
cin>>left>>right。
yl=f(left)。
yr=f(right)。
do
{middle=(right+left)/2。
ym=f(middle)。
if(yr*ym>0)
{right=middle。
yr=ym。
}
else
{left=middle。
yl=ym。
}
}while(fabs(ym)>=1e-6)。
cout<<"\nRootis:
"<return0。
}
第3章类和对象
(一)
【3.18】写出下面程序的运行结果。
#include
usingnamespacestd。
classtest
{public:
test()。
~test(){}。
private:
inti。
}。
test:
:
test()
{i=25。
for(intctr=0。
ctr<10。
ctr++)
{cout<<"Countingat"<}
}
testanObject。
intmain()
{return0。
}
【3.19】写出下面程序的运行结果。
#include
usingnamespacestd。
classTest{
private:
intval。
public:
Test()
{cout<<"default."<}
Test(intn)
{val=n。
cout<<"Con."<}
Test(constTest&t)
{val=t.val。
cout<<"Copycon."<}
}。
intmain()
{Testt1(6)。
Testt2=t1。
Testt3。
t3=t1。
return0。
}
【3.20】指出下列程序中的错误,并说明为什么。
#include
usingnamespacestd。
classStudent{
public:
voidprintStu()。
private:
charname[10]。
intage。
floataver。
}。
intmain()
{Studentp1,p2,p3。
p1.age=30。
…
return0。
}
【3.21】指出下列程序中的错误,并说明为什么。
#include
usingnamespacestd。
classStudent{
intsno。
intage。
voidprintStu()。
voidsetSno(intd)。
}。
voidprintStu()
{cout<<"\nSnois"<cout<<"ageis"<}
voidsetSno(ints)
{sno=s。
}
voidsetAge(inta)
{age=a。
}
intmain()
{Studentlin。
lin.setSno(20021)。
lin.setAge(20)。
lin.printStu()。
}
【3.22】指出下列程序中的错误,并说明为什么。
#include
usingnamespacestd。
classPoint{
public:
intx,y。
private:
Point()
{x=1。
y=2。
}
}。
intmain()
{Pointcpoint。
cpoint.x=2。
return0。
}
【3.23】下面是一个计算器类的定义,请完成该类成员函数的实现。
classcounter{
public:
counter(intnumber)。
voidincrement()。
//给原值加1
voiddecrement()。
//给原值减1
intgetvalue()。
//取得计数器值
intprint()。
//显示计数
private:
intvalue。
}。
【解】
classcounter{
public:
counter(intnumber)。
voidincrement()。
//给原值加1
voiddecrement()。
//给原值减1
intgetvalue()。
//取得计数器值
intprint()。
//显示计数
private:
intvalue。
}。
counter:
:
counter(intnumber)
{value=number。
}
voidcounter:
:
increment()
{value++。
}
voidcounter:
:
decrement()
{value--。
}
intcounter:
:
getvalue()
{returnvalue。
}
intcounter:
:
print()
{cout<<"valueis"<return0。
}
【3.24】根据注释语句的提示,实现类Date的成员函数。
#include
usingnamespacestd。
classDate{
public:
voidprintDate()。
//显示日期
voidsetDay(intd)。
//设置日的值
voidsetMonth(intm)。
//设置月的值
voidsetYear(inty)。
//设置年的值
private:
intday,month,year。
}。
intmain()
{DatetestDay。
testDay.setDay(5)。
testDay.setMonth(10)。
testDay.setYear(2003)。
testDay.printDate()。
return0。
}
【解】
voidDate:
:
printDate()
{cout<<"\nDateis"<cout<}
voidDate:
:
setDay(intd)
{day=d。
}
voidDate:
:
setMonth(intm)
{month=m。
}
voidDate:
:
setYear(inty)
{year=y。
}
【3.25】建立类cylinder,cylinder的构造函数被传递了两个double值,分别表示圆柱体的半径和高度。
用类cylinder计算圆柱体的体积,并存储在一个double变量中。
在类cylinder中包含一个成员函数vol,用来显示每个cylinder对象的体积。
【解】实现本题功能的程序如下:
#include
usingnamespacestd。
classcylinder{
public:
cylinder(doublea,doubleb)。
voidvol()。
private:
doubler,h。
doublevolume。
}。
cylinder:
:
cylinder(doublea,doubleb)
{r=a。
h=b。
volume=3.141592*r*r*h。
}
voidcylinder:
:
vol()
{cout<<"volumeis:
"<}
intmain()
{cylinderx(2.2,8.09)。
x.vol()。
return0。
}
【3.26】构建一个类Stock,含字符数组stockcode[]及整型数据成员quan、双精度型数据成员price。
构造函数含3个参数:
字符数组na[]及q、p。
当定义Stock的类对象时,将对象的第1个字符串参数赋给数据成员stockcode,第2和第3个参数分别赋给quan、price。
未设置第2和第3个参数时,quan的值为1000,price的值为8.98。
成员函数print没有形参,需使用this指针,显示对象数据成员的内容。
假设类Stoc第1个对象的三个参数分别为:
"600001",3000和5.67,第2个对象的第1个数据成员的值是"600001",第2和3数据成员的值取默认值。
要求编写程序分别显示这两个对象数据成员的值。
【解】实现本题功能的程序如下:
#include
usingnamespacestd。
constintSIZE=80。
classStock{
public:
Stock()
{strcpy(stockcode,"")。
}
Stock(charcode[],intq=1000,doublep=8.98)
{strcpy(stockcode,code)。
quan=q。
price=p。
}
voidprint(void)
{cout<stockcode。
cout<<""<quan<<""<price<}
private:
charstockcode[SIZE]。
intquan。
doubleprice。
}。
intmain()
{Stockst1("600001",3000,5.67)。
st1.print()。
Stockst2("600002")。
st2.print()。
return0。
}
第4章类和对象
(二)
【4.12】以下程序的运行结果是()。
#include
usingnamespacestd。
classB{
public:
B(){}
B(inti,intj)
{x=i。
y=j。
}
voidprintb()
{cout<}
private:
intx,y。
}。
classA{
public:
A()
{}
A(intI,intj)。
voidprinta()。
private:
Bc。
}。
A:
:
A(inti,intj):
c(i,j)
{}
voidA:
:
printa()
{c.printb()。
}
intmain()
{Aa(7,8)。
a.printa()。
return0。
}
A)8,9B)7,8
C)5,6D)9,10
【4.13】以下程序的运行结果是()。
#include
usingnamespacestd。
classA{
public:
voidset(inti,intj)
{x=i。
y=j。
}
intget_y()
{returny。
}
private:
intx,y。
}。
classbox{
public:
voidset(intl,intw,ints,intp)
{length=l。
width=w。
label.set(s,p)。
}
intget_area()
{returnlength*width。
}
private:
intlength,width。
Alabel。
}。
intmain()
{boxb。
b.set(4,6,1,20)。
cout<return0。
}
A)24B)4
C)20D)6
【4.14】以下程序的运行结果是()。
#include
usingnamespacestd。
classSample{
public:
Sample(inti,intj)
{x=i。
y=j。
}
voiddisp()
{cout<<"disp1"<}
voiddisp()const
{cout<<"disp2"<}
private:
intx,y。
}。
intmain()
{constSamplea(1,2)。
a.disp()。
return0。
}
A)disp1B)disp2
C)disp1disp2D)程序编译出错
【4.15】以下程序的运行结果是()。
#include
usingnamespacestd。
classR{
public:
R(intr1,intr2)
{R1=r1。
R2=r2。
}
voidprint()。
voidprint()const。
private:
intR1,R2。
}。
voidR:
:
print()
{cout<}
voidR:
:
print()const
{cout<}
intmain()
{Ra(6,8)。
constRb(56,88)。
b.print()。
return0。
}
A)6,8B)56,88
C)0,0D)8,6
【4.16】指出下面程序中的错误,并说明原因。
#include
usingnamespacestd。
classStudent{
public:
Student()
{++x。
cout<<"\npleaseinputstudentNo."。
cin>>Sno。
}
staticintget_x()
{returnx。
}
intget_Sno()
{returnSno。
}
private:
staticintx。
intSno。
}。
intStudent:
:
x=0。
intmain()
{cout<:
get_x()<<"Studentexist\n"。
Studentstu1。
Student*pstu=newStudent。
cout<:
get_x()<<"Studentexist,y="<cout<:
get_x()<<"Studen