c++面向对象课后答案第9章Word文档格式.docx

上传人:b****3 文档编号:7134774 上传时间:2023-05-08 格式:DOCX 页数:24 大小:22KB
下载 相关 举报
c++面向对象课后答案第9章Word文档格式.docx_第1页
第1页 / 共24页
c++面向对象课后答案第9章Word文档格式.docx_第2页
第2页 / 共24页
c++面向对象课后答案第9章Word文档格式.docx_第3页
第3页 / 共24页
c++面向对象课后答案第9章Word文档格式.docx_第4页
第4页 / 共24页
c++面向对象课后答案第9章Word文档格式.docx_第5页
第5页 / 共24页
c++面向对象课后答案第9章Word文档格式.docx_第6页
第6页 / 共24页
c++面向对象课后答案第9章Word文档格式.docx_第7页
第7页 / 共24页
c++面向对象课后答案第9章Word文档格式.docx_第8页
第8页 / 共24页
c++面向对象课后答案第9章Word文档格式.docx_第9页
第9页 / 共24页
c++面向对象课后答案第9章Word文档格式.docx_第10页
第10页 / 共24页
c++面向对象课后答案第9章Word文档格式.docx_第11页
第11页 / 共24页
c++面向对象课后答案第9章Word文档格式.docx_第12页
第12页 / 共24页
c++面向对象课后答案第9章Word文档格式.docx_第13页
第13页 / 共24页
c++面向对象课后答案第9章Word文档格式.docx_第14页
第14页 / 共24页
c++面向对象课后答案第9章Word文档格式.docx_第15页
第15页 / 共24页
c++面向对象课后答案第9章Word文档格式.docx_第16页
第16页 / 共24页
c++面向对象课后答案第9章Word文档格式.docx_第17页
第17页 / 共24页
c++面向对象课后答案第9章Word文档格式.docx_第18页
第18页 / 共24页
c++面向对象课后答案第9章Word文档格式.docx_第19页
第19页 / 共24页
c++面向对象课后答案第9章Word文档格式.docx_第20页
第20页 / 共24页
亲,该文档总共24页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

c++面向对象课后答案第9章Word文档格式.docx

《c++面向对象课后答案第9章Word文档格式.docx》由会员分享,可在线阅读,更多相关《c++面向对象课后答案第9章Word文档格式.docx(24页珍藏版)》请在冰点文库上搜索。

c++面向对象课后答案第9章Word文档格式.docx

B。

派生类指针 

C。

对象名 

D.基类指针

3.2下列函数中,不能说明为虚函数的是(C)。

A.私有成员函数 

B.公有成员函数 

C.构造函数 

D.析构函数

3.3在派生类中,重载一个虚函数时,要求函数名、参数的个数、参数的类型、参数的顺序和函数的返回值(A)。

A.相同 

B.不同 

相容 

D。

部分相同

3.4当一个类的某个函数被说明为virtual时,该函数在该类的所有派生类中(A).

A.都是虚函数

B.只有被重新说明时才是虚函数

C.只有被重新说明为virtual时才是虚函数

D.都不是虚函数

3.5(C)是一个在基类中说明的虚函数,它在该基类中没有定义,但要求任何派生类都必须定义自己的版本。

A.虚析构函数 

B.虚构造函数

C.纯虚函数 

D.静态成员函数

3.6以下基类中的成员函数,哪个表示纯虚函数(C)。

A.virtualvoidvf(int);

B.voidvf(int)=0;

C.virtualvoidvf()=0;

D.virtualvoidvf(int){}

3.7下列描述中,(D)是抽象类的特性。

A.可以说明虚函数

B.可以进行构造函数重载

C.可以定义友元函数

D.不能定义其对象

3.8类B是类A的公有派生类,类A和类B中都定义了虚函数func(),p是一个指向类A对象的指针,则p—〉A:

:

func()将(A)。

A.调用类A中的函数func()

B.调用类B中的函数func()

C.根据p所指的对象类型而确定调用类A中或类B中的函数func()

D.既调用类A中函数,也调用类B中的函数

3.9类定义如下。

classA{

public:

virtualvoidfunc1(){}

voidfun2(){}

};

classB:

publicA{

voidfunc1(){cout〈<

”classBfunc1”〈〈endl;

}

virtualvoidfunc2(){cout〈<

"

classBfunc2”〈〈endl;

};

则下面正确的叙述是(A)

A.A:

func2()和B:

func1()都是虚函数

B.A:

func2()和B:

func1()都不是虚函数

C.B:

func1()是虚函数,而A:

func2()不是虚函数

D.B:

func1()不是虚函数,而A:

func2()是虚函数

10下列关于虚函数的说明中,正确的是(B)。

A.从虚基类继承的函数都是虚函数

B.虚函数不得是静态成员函数

C.只能通过指针或引用调用虚函数

D.抽象类中的成员函数都是虚函数

4.写出下列程序运行结果

4。

1#include<

iostream〉

usingnamespacestd;

classA{

public:

virtualvoidfunc(){cout〈〈”funcinclassA”〈〈endl;

};

classB{

virtualvoidfunc(){cout〈〈”funcinclassB”〈〈endl;

classC:

publicA,publicB{

voidfunc(){cout〈<

funcinclassC”<

<

endl:

intmain(){

Cc;

A&

pa=c;

B&pb=c;

C&

pc=c;

pa.func();

pb.func();

pc。

func();

funcinclassC

funcinclassC

2#include〈iostream〉

classA{

virtual~A(){

cout<

〈”A:

~A()called“〈〈endl;

}

classB:

publicA{

char*buf;

B(inti){buf=newchar[i];

virtual~B(){

delete[]buf;

B:

~B()called”〈〈endl;

};

voidfun(A*a){

deletea;

intmain()

A*a=newB(10);

fun(a);

~B()called

A:

~A()called

5.程序设计题

5。

1有一个交通工具类vehicle,将它作为基类派生小车类car、卡车类truck和轮船类boat,定义这些类并定义一个虚函数用来显示各类信息。

#include〈iostream>

#include〈cstring〉

classVehicle{

virtualvoidshowinfo()=0;

protected:

charName[20];

classCar:

publicVehicle{

Car(char*name){

strcpy(Name,name);

voidshowinfo(){cout<

〈Name〈〈endl;

intRadius;

classTruck:

Truck(char*name){

strcpy(Name,name);

Name<

endl;

classBoat:

Boat(char*name){

strcpy(Name,name);

Name〈〈endl;

intmain(){

Vehicle*vp;

Carcar("

奔驰”);

Trucktruck("

运输卡车"

);

Boatboat(”游艇"

vp=&car;

vp->

showinfo();

vp=&

truck;

boat;

vp—〉showinfo();

return0;

5.2定义一个shape抽象类,派生出Rectangle类和Circle类,计算各派生类对象的面积Area()。

#include〈iostream〉

#include<

cstring〉

classShape{

virtualvoidArea()=0;

classRectangle:

publicShape{

Rectangle(doublew,doubleh){

width=w,height=h;

voidArea(){cout<

〈width*height〈〈endl;

doublewidth,height;

classCircle:

Circle(doubler){

radius=r;

〈3。

1415*radius*radius〈<

protected:

doubleradius;

Shape*sp;

Rectanglere1(10,6);

Circlecir1(4.0);

sp=&

re1;

sp—〉Area();

sp=&cir1;

sp-〉Area();

5.3定义猫科动物Animal类,由其派生出猫类(Cat)和豹类(Leopard),二者都包含虚函数sound(),要求根据派生类对象的不同调用各自重载后的成员函数.

#include<

fstream>

classAnimal{

virtualvoidSpeak()=0;

classCat:

publicAnimal{

voidSpeak(){

〈”MynameisCat”<

〈endl;

classLeopard:

publicAnimal{

voidSpeak(){

cout〈〈"

MynameisLeopard"

〈〈endl;

intmain(){

Animal*pa;

Catcat;

pa=&cat;

pa—>

Speak();

Leopardleopard;

pa=&

leopard;

pa-〉Speak();

return0;

5.4矩形法(rectangle)积分近似计算公式为:

Δx(y0+y1+…。

yn-1)

梯形法(1adder)积分近似计算公式为:

 

[y0+2(y1+…。

yn-1)+yn]

辛普生法(simpson)积分近似计算公式(n为偶数)为:

[y0+yn+4(y1+y3….yn—1)+2(y2+y4+…yn—2)]

被积函数用派生类引入,定义为纯虚函数。

基类(integer)成员数据包括积分上下限b和a,分区数n,步长step=(b—a)/n,积分值result.定义积分函数integerate()为虚函数,它只显示提示信息。

派生的矩形法类(rectangle)重定义integerate(),采用矩形法做积分运算。

派生的梯形法类(1adder)和辛普生法(simpson)类似.试编程,用3种方法对下列被积函数进行定积分计算,并比较积分精度。

(1)sin(x),下限为0.0,上限为pir/2。

(2)exp(x),下限为0。

0,上限为1.0。

(3)4。

0/(1+x×

x),下限为0。

0,上限为1.0.

解法一

#include〈iostream>

cmath〉

usingnamespacestd;

classInteger{

doubleresult,a,b,step;

//Intevalue积分值,a积分下限,b积分上限

intn;

virtualdoublefun(doublex)=0;

//被积函数声明为纯虚函数

virtualvoidIntegerate(){

这里是积分函数”<

Integer(doublera=0,doublerb=0,intnn=2000){

a=ra;

b=rb;

n=nn;

result=0;

voidPrint(){

cout.precision(15);

cout〈<

”积分值=”〈〈result<

endl;

classRectangle:

publicInteger{

voidIntegerate(){

inti;

step=(b-a)/n;

for(i=0;

i<

=n;

i++)result+=fun(a+step*i);

result*=step;

Rectangle(doublera,doublerb,intnn):

Integer(ra,rb,nn){}

classLadder:

publicInteger{

inti;

step=(b—a)/n;

result=fun(a)+fun(b);

for(i=1;

n;

i++)result+=2*fun(a+step*i);

result*=step/2;

Ladder(doublera,doublerb,intnn):

Integer(ra,rb,nn){}

classSimpson:

step=(b—a)/n;

i+=2)result+=4*fun(a+step*i);

for(i=2;

i+=2)result+=2*fun(a+step*i);

result*=step/3;

Simpson(doublera,doublerb,intnn):

Integer(ra,rb,nn){}

classsinR:

publicRectangle{//矩形法和梯形法采用并列结构

sinR(doublera,doublerb,intnn):

Rectangle(ra,rb,nn){}

doublefun(doublex){returnsin(x);

classsinL:

publicLadder{

sinL(doublera,doublerb,intnn):

Ladder(ra,rb,nn){}

classexpR:

publicRectangle{

expR(doublera,doublerb,intnn):

doublefun(doublex){returnexp(x);

classexpL:

expL(doublera,doublerb,intnn):

classotherR:

publicRectangle{

otherR(doublera,doublerb,intnn):

Rectangle(ra,rb,nn){}

doublefun(doublex){return(4.0/(1+x*x));

classotherL:

publicLadder{

otherL(doublera,doublerb,intnn):

Ladder(ra,rb,nn){}

doublefun(doublex){return(4.0/(1+x*x));

classsinS:

publicSimpson{//辛普生法采用层次结构

sinS(doublera,doublerb,intnn):

Simpson(ra,rb,nn){}

classexpS:

publicsinS{

expS(doublera,doublerb,intnn):

sinS(ra,rb,nn){}

doublefun(doublex){returnexp(x);

classotherS:

publicexpS{

otherS(doublera,doublerb,intnn):

expS(ra,rb,nn){}

doublefun(doublex){return(4。

0/(1+x*x));

Integer*bp;

sinRsr(0.0,3.1415926535/2.0,100);

bp=&sr;

bp-〉Integerate();

//动态,可以访问派生类定义的被积函数

bp-〉Print();

sinLsl(0.0,3。

1415926535/2.0,100);

bp=&

sl;

bp—>

Integerate();

bp—〉Print();

sinSss(0.0,3.1415926535/2。

0,100);

ss;

bp->

//动态,在层次中选

Print();

expRer(0。

0,1.0,100);

er;

//动态,可以访问派生类定义的被积函数

expLel(0.0,1。

0,100);

el;

bp-〉Print();

expSes(0.0,1.0,100);

bp=&es;

bp—〉Integerate();

Print();

otherRor(0.0,1.0,100);

or;

otherLol(0。

0,1.0,100);

//增加到100000也达不到辛普生法的精度

ol;

Integerate();

otherSos(0。

0,1.0,100);

bp=&os;

bp—〉Integerate();

//动态,在层次中选

解法二

//Integer为抽象类,fun为被积函数,其余3个虚函数为积分函数

classInteger{

doubleresult,a,b,step;

//Intevalue积分值,a积分下限,b积分上限

virtualvoidRectangle()=0;

virtualvoidLadder()=0;

virtualvoidSimpson()=0;

Integer(doublera=0,doublerb=0,intnn=2000){

cout〈〈”积分值=”<

〈result<

classSIN:

SIN(doublera,doublerb,intnn):

Integer(ra,rb,nn){}

doublefun(doublex){returnsin(x);

voidRectangle(){

i++)result+=fun(a+step*i);

voidLadder(){

result=fun(a)+fun(b);

for(i=1;

n;

i++)result+=2*fun(a+step*i);

voidSimpson(){

i〈n;

i+=2)result+=2*fun(a+step*i);

Integer*Inp;

SINs(0,3。

1415926,1000);

Inp=&s;

Inp->

Rectangle();

Inp—〉Ladd

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

当前位置:首页 > 农林牧渔 > 林学

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

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