C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx

上传人:b****2 文档编号:4586110 上传时间:2023-05-03 格式:DOCX 页数:124 大小:43.53KB
下载 相关 举报
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第1页
第1页 / 共124页
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第2页
第2页 / 共124页
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第3页
第3页 / 共124页
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第4页
第4页 / 共124页
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第5页
第5页 / 共124页
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第6页
第6页 / 共124页
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第7页
第7页 / 共124页
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第8页
第8页 / 共124页
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第9页
第9页 / 共124页
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第10页
第10页 / 共124页
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第11页
第11页 / 共124页
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第12页
第12页 / 共124页
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第13页
第13页 / 共124页
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第14页
第14页 / 共124页
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第15页
第15页 / 共124页
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第16页
第16页 / 共124页
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第17页
第17页 / 共124页
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第18页
第18页 / 共124页
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第19页
第19页 / 共124页
C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx_第20页
第20页 / 共124页
亲,该文档总共124页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx

《C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx(124页珍藏版)》请在冰点文库上搜索。

C面向对象程序设计习题解答与上机指导第二版源程序Word格式文档下载.docx

cout<

<

cin>

>

b。

{cout<

}

Thelowestcommondenominatoris"

endl<

d。

【2.24】写出下列程序的运行结果。

inti=15。

{inti。

i=100。

:

:

i=i+1。

i<

endl。

运行结果:

101

Pleaseanykeytocontinue。

【2.25】写出下列程序的运行结果。

voidf(int&

m,intn)

{inttemp。

temp=m。

m=n。

n=temp。

{inta=5,b=10。

f(a,b)。

a<

"

b<

结果:

1010

【2.26】分析下面程序的输出结果。

int&

f(int&

i)

{i+=10。

returni。

{intk=0。

m=f(k)。

cout<

k<

m=20。

10

20

【2.27】编写一个C++风格的程序,用动态分配空间的方法计算Fibonacci数列的前20项并存储到动态分配的空间中。

【解】实现本题功能的程序如下:

{int*p=newint[20]。

//动态分配20个整型内存空间

*p=1。

*(p+1)=1。

//对前面2个内存空间赋值1

*p<

\t"

*(p+1)<

p=p+2。

//p指向第3个内存空间

for(inti=3。

=20。

i++)

{*p=*(p-1)+*(p-2)。

if(i%5==0)cout<

p++。

//p指向下一个内存空间。

return0。

【2.28】编写一个C++风格的程序,建立一个被称为sroot的函数,返回其参数的二次方根。

重载函数sroot三次,让它返回整数、长整数与双精度数的二次方根(计算二次方根时,可以使用标准库函数sqrt)。

cmath>

doublesroot(inti)

{returnsqrt(i)。

doublesroot(longl)

{returnsqrt(l)。

doublesroot(doubled)

{returnsqrt(d)。

{inti=12。

longl=1234。

doubled=12.34。

i的二次方根是:

sroot(i)<

l的二次方根是:

sroot(l)<

d的二次方根是:

sroot(d)<

【2.29】编写一个C++风格的程序,解决百钱问题:

将一元人民币兑换成1、2、5分的硬币,有多少种换法?

{inti,j,sum=0。

for(i=0。

for(j=0。

j<

=50。

j++)

if(100-5*i-2*j>

=0)

{sum++。

100-5*i-2*j<

sumis"

sum<

【2.30】编写一个C++风格的程序,输入两个整数,将它们按由小到大的顺序输出。

要求使用变量的引用。

{voidchange(int&

int&

inta,b。

a>

if(a>

b)change(a,b)。

voidchange(int&

a1,int&

b1)

{inttemp。

temp=a1。

a1=b1。

b1=temp。

【2.31】编写C++风格的程序,用二分法求解f(x)=0的根。

#include<

inlinefloatf(floatx)

{return2*x*x*x-4*x*x+3*x-6。

{floatleft,right,middle,ym,yl,yr。

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)。

\nRootis:

middle。

第3章类和对象

(一)

【3.18】写出下面程序的运行结果。

classtest

{public:

test()。

~test(){}。

private:

inti。

}。

test:

test()

{i=25。

for(intctr=0。

ctr<

10。

ctr++)

Countingat"

ctr<

\n"

testanObject。

{return0。

【3.19】写出下面程序的运行结果。

classTest{

intval。

public:

Test()

{cout<

default."

Test(intn)

{val=n。

Con."

Test(constTest&

t)

{val=t.val。

Copycon."

{Testt1(6)。

Testt2=t1。

Testt3。

t3=t1。

【3.20】指出下列程序中的错误,并说明为什么。

classStudent{

public:

voidprintStu()。

private:

charname[10]。

intage。

floataver。

{Studentp1,p2,p3。

p1.age=30。

【3.21】指出下列程序中的错误,并说明为什么。

intsno。

voidsetSno(intd)。

voidprintStu()

{cout<

\nSnois"

sno<

"

ageis"

age<

."

voidsetSno(ints)

{sno=s。

voidsetAge(inta)

{age=a。

{Studentlin。

lin.setSno(20021)。

lin.setAge(20)。

lin.printStu()。

【3.22】指出下列程序中的错误,并说明为什么。

classPoint{

intx,y。

Point()

{x=1。

y=2。

{Pointcpoint。

cpoint.x=2。

【3.23】下面是一个计算器类的定义,请完成该类成员函数的实现。

classcounter{

counter(intnumber)。

voidincrement()。

//给原值加1

voiddecrement()。

//给原值减1

intgetvalue()。

//取得计数器值

intprint()。

//显示计数

intvalue。

classcounter{

counter(intnumber)。

voidincrement()。

voiddecrement()。

intgetvalue()。

//显示计数

counter:

counter(intnumber)

{value=number。

voidcounter:

increment()

{value++。

decrement()

{value--。

intcounter:

getvalue()

{returnvalue。

print()

valueis"

value<

【3.24】根据注释语句的提示,实现类Date的成员函数。

classDate{

voidprintDate()。

//显示日期

voidsetDay(intd)。

//设置日的值

voidsetMonth(intm)。

//设置月的值

voidsetYear(inty)。

//设置年的值

intday,month,year。

{DatetestDay。

testDay.setDay(5)。

testDay.setMonth(10)。

testDay.setYear(2003)。

testDay.printDate()。

voidDate:

printDate()

\nDateis"

day<

month<

year<

setDay(intd)

{day=d。

setMonth(intm)

{month=m。

setYear(inty)

{year=y。

【3.25】建立类cylinder,cylinder的构造函数被传递了两个double值,分别表示圆柱体的半径和高度。

用类cylinder计算圆柱体的体积,并存储在一个double变量中。

在类cylinder中包含一个成员函数vol,用来显示每个cylinder对象的体积。

classcylinder{

cylinder(doublea,doubleb)。

voidvol()。

doubler,h。

doublevolume。

cylinder:

cylinder(doublea,doubleb)

{r=a。

h=b。

volume=3.141592*r*r*h。

voidcylinder:

vol()

volumeis:

volume<

{cylinderx(2.2,8.09)。

x.vol()。

【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个数据成员的值是"

第2和3数据成员的值取默认值。

要求编写程序分别显示这两个对象数据成员的值。

【解】实现本题功能的程序如下:

constintSIZE=80。

classStock{

Stock()

{strcpy(stockcode,"

Stock(charcode[],intq=1000,doublep=8.98)

{strcpy(stockcode,code)。

quan=q。

price=p。

voidprint(void)

this->

stockcode。

quan<

price<

charstockcode[SIZE]。

intquan。

doubleprice。

{Stockst1("

3000,5.67)。

st1.print()。

Stockst2("

600002"

st2.print()。

第4章类和对象

(二)

【4.12】以下程序的运行结果是()。

classB{

B(){}

B(inti,intj)

{x=i。

y=j。

voidprintb()

x<

"

y<

classA{

A()

{}

A(intI,intj)。

voidprinta()。

Bc。

A:

A(inti,intj):

c(i,j)

{}

voidA:

printa()

{c.printb()。

{Aa(7,8)。

a.printa()。

A)8,9B)7,8

C)5,6D)9,10

【4.13】以下程序的运行结果是()。

voidset(inti,intj)

intget_y()

{returny。

classbox{

voidset(intl,intw,ints,intp)

{length=l。

width=w。

label.set(s,p)。

intget_area()

{returnlength*width。

intlength,width。

Alabel。

{boxb。

b.set(4,6,1,20)。

b.get_area()<

A)24B)4

C)20D)6

【4.14】以下程序的运行结果是()。

classSample{

Sample(inti,intj)

{x=i。

voiddisp()

disp1"

voiddisp()const

disp2"

{constSamplea(1,2)。

a.disp()。

A)disp1B)disp2

C)disp1disp2D)程序编译出错

【4.15】以下程序的运行结果是()。

classR{

R(intr1,intr2)

{R1=r1。

R2=r2。

voidprint()。

voidprint()const。

intR1,R2。

voidR:

R1<

R2<

print()const

{Ra(6,8)。

constRb(56,88)。

b.print()。

A)6,8B)56,88

C)0,0D)8,6

【4.16】指出下面程序中的错误,并说明原因。

Student()

{++x。

\npleaseinputstudentNo."

Sno。

staticintget_x()

{returnx。

intget_Sno()

{returnSno。

staticintx。

intSno。

intStudent:

x=0。

{cout<

Student:

get_x()<

Studentexist\n"

Studentstu1。

Student*pstu=newStudent。

cout<

Studentexist,y="

get_Sno()<

Studen

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2