C++面向对象程序设计课后题答案Word文档格式.docx

上传人:b****5 文档编号:20804816 上传时间:2023-01-25 格式:DOCX 页数:57 大小:28.43KB
下载 相关 举报
C++面向对象程序设计课后题答案Word文档格式.docx_第1页
第1页 / 共57页
C++面向对象程序设计课后题答案Word文档格式.docx_第2页
第2页 / 共57页
C++面向对象程序设计课后题答案Word文档格式.docx_第3页
第3页 / 共57页
C++面向对象程序设计课后题答案Word文档格式.docx_第4页
第4页 / 共57页
C++面向对象程序设计课后题答案Word文档格式.docx_第5页
第5页 / 共57页
点击查看更多>>
下载资源
资源描述

C++面向对象程序设计课后题答案Word文档格式.docx

《C++面向对象程序设计课后题答案Word文档格式.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计课后题答案Word文档格式.docx(57页珍藏版)》请在冰豆网上搜索。

C++面向对象程序设计课后题答案Word文档格式.docx

#include<

math.h>

doublesroot(intm)

returnsqrt(m);

doublesroot(longm)

doublesroot(doublem)

cout<

"

sroot(145)="

sroot(145)<

sroot(123456)="

sroot(123456)<

sroot(1.44)="

sroot(1.44)<

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

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

intk=0;

for(inti=0;

=20;

for(intj=0;

j<

=50;

j++)

{

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

=0)

{

k++;

}

}

将一元人民币兑换成1、2、5分的硬币,共有"

k<

种换法"

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

要求使用变量的引用。

voidchange(int&

a,int&

b)

intt=a;

a=b;

b=t;

}

intm,n;

请输入两个整数:

;

cin>

>

m>

n;

if(m>

n)

change(m,n);

这两个数从小到大依次为:

"

m<

n<

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

假设方程为:

2*x*x*x-4*x*x+3*x-6=0

iostream>

cmath>

usingnamespacestd;

doublef(doublex)

return2*x*x*x-4*x*x+3*x-6;

doubleleft,right,middle,ymid,yleft,yright;

请按由小到大的顺序输入两个数:

left>

right;

yleft=f(left);

yright=f(right);

if(yleft*yright>

0)

您输入的两个数,用二分法无法求得方程的根"

exit(0);

do{

middle=(left+right)/2;

ymid=f(middle);

if(yleft*ymid<

right=middle;

yright=ymid;

if(yright*ymid<

left=middle;

yleft=ymid;

}while(fabs(ymid)>

=1e-6);

方程的根为:

middle<

第三章类和对象

【3.6】假设在程序中已经声明了类point,并建立了其对象p1和p4。

请回答以下几个语句有什么区别?

(1)pointp2,p3;

(2)pointp2=p1;

(3)pointp2(p1);

(4)p4=p1;

【解答】语句

(1)使用带默认参数的构造函数,或不带参数的构造函数,定义了point类的两个对象p2,p3;

语句

(2)在建立新对象p2时,用已经存在的对象p1去初始化新对象p2,在这个过程中用“赋值法”调用了拷贝构造函数;

语句(3)在建立新对象p2时,用已经存在的对象p1去初始化新对象p2,在这个过程中用“带入法”调用了拷贝构造函数;

【3.7】B

【3.8】C

【3.9】C

【3.10】B

【3.11】B

【3.12】A

【3.13】A

【3.14】B

【3.15】A

【3.16】B

【3.17】B

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

10,20

30,48

50,68

70,80

90,16

11,120

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

Constructing

Destructing

100

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

3objectsinexistence

4objectsinexistenceafterallocation

3objectsinexistenceafterdelection

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

Countingat0

Countingat1

Countingat2

Countingat3

Countingat4

Countingat5

Countingat6

Countingat7

Countingat8

Countingat9

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

Defaultconstructorcalled.

Constructor:

a=1,b=2

a=3,b=4

a=5,b=6

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

Con.

Copycon.

default.

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

A=5

B=14

A=9

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

5,7

22,25

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

B=15

A=10

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

classStudent

charname[10];

intage;

floataver;

voidprintStu();

};

voidmain()

Studentp1,p2,p3;

p1.age=30;

//Error:

age为私有成员,不能在类外直接访问

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

intsno;

public:

/*在成员函数定义前加public,不然默认为private,则在主函数中不能调用私有的成员函数*/

voidsetSno(intd);

voidsetAge(inta);

//添加成员函数的声明

voidprintStu()

//类外定义成员函数需加类名:

:

,改为:

voidStudent:

printStu()

\nStudentNo.is"

sno<

"

ageis"

age<

."

voidsetSno(ints)

setSno(ints)

sno=s;

voidsetAge(inta)

setAge(inta)

age=a;

Studentlin;

lin.setSno(20021);

lin.setAge(20);

lin.printStu();

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

classPoint

public:

intx,y;

private:

Point()//构造函数不能说明为private的

x=1;

y=2;

Pointcpoint;

cpoint.x=2;

构造函数Point是私有的,语句“Pointcopint;

”执行时出现错误。

错误的原因是:

类Point的构造函数是私有函数,创建对象cpoint时不能调用它。

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

classCounter

Counter(intnumber);

voidincrement();

//给原值加1

voiddecrement();

//给原值减1

intgetValue();

//取得计数器值

intprint();

//显示计数

intvalue;

counter:

counter(intnumber)

value=number;

voidcounter:

increment()

value++;

decrement()

value--;

intcounter:

getvalue()

returnvalue;

print()

\nvalueis:

value<

counterop(10);

op.print();

op.decrement();

op.increment();

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

classDate

voidprintDate();

//显示日期

voidsetDay(intd);

//设置日的值

voidsetMonth(intm);

//设置月的值

voidsetYear(inty);

//设置年的值

intday,month,year;

voidDate:

printDate()

{

year<

/"

month<

day<

setDay(intd)

day=d;

setMonth(intm)

month=m;

}

setYear(inty)

year=y;

DatetestDay;

testDay.setDay(5);

testDay.setMonth(10);

testDay.setYear(2003);

testDay.printDate();

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

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

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

constdoublePI=3.1415926;

classCylinder

doubleradius,height,volume;

Cylinder(doubler,doubleh)

radius=r;

height=h;

volume=PI*radius*radius*height;

voidvol()

thevolumeis"

volume<

Cylinderc(2,5);

c.vol();

【3.33】构建一个类book,其中含有两个私有数据成员qu和price,建立一个有5个元素的数组对象,将qu初始化为1~5,将price初始化为qu的10倍。

显示每个对象的qu*price值。

【3.34】修改上题,通过对象指针访问对象数组,使程序以相反的顺序显示对象数组的qu*price值。

classBook

intqu,price;

Book(inti);

voidprint();

voidprintqandp();

Book:

Book(inti)

qu=i;

price=qu*10;

voidBook:

theBook'

squis:

qu<

spriceis:

price<

printqandp()

squ*priceis:

qu*price<

Booka[5]={1,2,3,4,5};

inti;

for(i=0;

i<

5;

i++)

a[i].print();

Book*b=&

a[4];

//逆序显示数组元素

b->

printqandp();

b--;

Book*c=a;

//顺序显示数组元素

c->

c++;

//或者

Book(intq,intp);

voidprintqandp();

Book(intq,intp)

qu=q;

price=p;

Booka[5]={Book(1,10),Book(2,20),Book(3,30),Book(4,40),Book(5,50)};

【3.35】构建一个类Stock,含字符数组stockcode[]及整型数据成员quan、双精度型数据成员price。

构造函数含3个参数:

字符数组na[]及q、p。

当定义Stock的类对象时,将对象的第一个字符串参数赋给数据成员stockcode,第2个和第3个参数分别赋给quan和price。

未设置第2个和第3个参数时,quan的值为1000,price的值为8.98。

成员函数print()没有形参,需使用this指针,显示对象数据成员的内容。

假设类Stock第1个对象的三个参数为:

“600001”、3000和5.67,第2个对象的第1个数据成员的值是“600001”,第2和第3个数据成员的值取默认值。

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

string>

classStock

Stock(charna[],intq=1000,doublep=8.98);

print();

charstockcode[16];

intquan;

floatprice;

Stock:

Stock(charna[],intq,doublep)

strcpy(stockcode,na);

quan=q;

stocknode:

this->

stockcode<

quan:

quan;

price:

Stocka("

600001"

3000,5.67);

Stockb("

);

a.print();

b.print();

【3.36】编写一个程序,已有若干学生的数据,包括学号、姓名、成绩,要求输出这些学生的数据并计算出学生人数和平均成绩(要求将学生人数和总成绩用静态数据成员表示)。

stringname;

stringsno;

doublescore;

staticintnum;

staticdoublesum;

Student(stringn,stringno,doubles);

staticdoublegetAverage();

staticdoublegettotal();

staticintgetnum();

intStudent:

num=0;

doubleStudent:

sum=0;

Student:

Student(stringn,stringno,doubles)

name=n;

sno=no;

score=s;

num++;

sum+=s;

学生的学号为:

,姓名为:

name<

,成绩为:

score<

getnum()

returnnum;

getAverage()

if(num==0)

return0;

else

returnsum/num;

gettotal()

returnsum;

当前学生人数为:

getnum()<

学生的总成绩为:

gettotal()<

学生的平均成绩为:

getAverage()<

Students1("

12012"

张明"

80);

s1.print();

s1.getnum()<

s1.gettotal()<

s1.getAverage()<

Students2("

09615"

李涛"

50);

s2.print();

s2.getnum()<

s2.gettotal()<

s2.getAverage()<

Students3("

12016"

王晓"

90);

s3.print();

s3.getnum()<

s3.gettotal()<

s3.getAverage()<

第四章派生类与继承

【4.8】A

【4.9】C

【4.10】C

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

当前位置:首页 > 成人教育 > 专升本

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1