C网络作业答案Word格式.docx

上传人:b****6 文档编号:21489480 上传时间:2023-01-30 格式:DOCX 页数:16 大小:19.35KB
下载 相关 举报
C网络作业答案Word格式.docx_第1页
第1页 / 共16页
C网络作业答案Word格式.docx_第2页
第2页 / 共16页
C网络作业答案Word格式.docx_第3页
第3页 / 共16页
C网络作业答案Word格式.docx_第4页
第4页 / 共16页
C网络作业答案Word格式.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

C网络作业答案Word格式.docx

《C网络作业答案Word格式.docx》由会员分享,可在线阅读,更多相关《C网络作业答案Word格式.docx(16页珍藏版)》请在冰豆网上搜索。

C网络作业答案Word格式.docx

virtua1<

函数返回类型>

<

函数名>

(<

参数表>

4对虚函数的调用________。

A)一定使用动态联编B)必须使用动态联编

C)一定使用静态联编D)不一定使用动态联编

参见第1题的注释。

5实现运行时的多态性要使用___________。

A)重载函数B)构造函数C)析构函数D)虚函数

6要实现动态联编,必须通过____调用虚函数。

A)对象指针B)成员名限定C)对象名D)派生类名

7在派生类中重新定义虚函数时,除了_____方面,其他方面都必须与基类中相应的

虚函数保持一致。

A)参数个数B)参数类型C)函数名称D)函数体

参见第2题的注释。

8下面关于构造函数和析构函数的描述,错误的是__。

A)析构函数中调用虚函数采用静态联编

B)对虚析构函数的调用可以采用动态联编

C)当基类的析构函数是虚函数时,其派生类的析构函数也一定是虚函数

D)构造函数可以声明为虚函数

构造函数不能声明为虚函数,但析构函数可以声明为虚函数。

当基类的析构函

数声明为虚函数时,无论派生类是否使用virtual关键字说明,派生类的析构函数一定

是虚函数,对缺省析构函数亦然。

而且,如果满足动态联编的其他条件,对虚析构函

数的调用将采用动态联编。

构造函数不能声明为虚函数,但在构造函数中可以调用虚函数。

在构造函数或析

构函数中调用虚函数,将采用静态联编。

9关于纯虚函数和抽象类的描述中,错误的是__。

C

A)纯虚函数是一种特殊的虚函数,它没有具体的实现

B)抽象类是指具有纯虚函数的类

C)一个基类中说明有纯虚函数,该基类的派生类一定不再是抽象类

D)抽象类只能作为基类来使用,其纯虚函数的实现由派生类给出

带有纯虚函数的类称为抽象类。

抽象类中的纯虚函数的实现由派生类给出:

但派生类仍可不给出纯虚函数的定义,继续作为抽象类存在。

10下列描述中,____是抽象类的特性。

A)可以说明虚函数B)可以进行构造函数重载

C)可以定义友元函数D)不能说明其对象

抽象类区别于其他类的最根本的特征是不能定义对象。

11_______是一个在基类中说明的虚函数,它在该基类中没有定义,但要求任何派生

类都必须定义自己的版本。

C

A)虚析构函数B)虚构造函数C)纯虚函数D)静态成员函数

12如果一个类至少有一个纯虚函数,那么就称该类为__。

A)抽象类B)虚基类C)派生类D)以上都不对

13以下___成员函数表示纯虚函数。

A)virtualintvf(int);

B)voidvf(int)=0;

C)virtualvoidvf()=0;

D)virtualvoidvf(int)()

纯虚函数的声明格式如下:

virtual<

)=0;

注意纯虚函数与虚函数体为空的区别。

纯虚函数根本就没有函数体,而空的虚

函数的函数体为空:

前者所在的类是抽象类,不能直接进行实例化,而后者所在的

类是可以实例化的:

14下面的描述中,正确的是_____。

A)virtual可以用来声明虚函数

B)含有纯虚函数的类是不可以用来创建对象的,因为它是虚基类

C)即使基类的构造函数没有参数,派生类也必须建立构造函数

D)静态数据成员可以通过成员初始化列表来初始化

virtual关键字既可以用来声明虚基类,也可以用来声明虚函数。

含有纯虚函数的类是抽象类,它不能用来定义对象。

静态数据成员的初始化必须在类体外进行。

如果所有的基类和子对象构造函数都不需要参数,派生类也不需要参数时,派

生类构造函数可以不定义。

15在下面程序中,A、B、C、D四句编译时不会出现错误的是__。

#include<

>

classBase

{

public:

Base(){}

Base(intc):

count(c){}

virtualvoidprint()const=0;

private:

intcount;

};

classDerived:

publicBase

public:

Derived():

Base(0){}

Derived(intc):

Base(c){}

voidprint()const{cout<

"

Derived"

endl;

}

voidmain()

Derivedd(10);

Base*pb;

pb=&

d;

Derive2'

sPrint()called.

根据结果将程序补充完整。

#include<

classBase

Base(inti){b=i;

}

___①virtualvoidPrint()=0;

___

protected:

intb;

classDerivel:

publicBase

{

___②Derive1(inti):

Base(i){}___

voidPrint()

{

cout<

”Derive1’sPrint()called.”<

}

classDerive2:

_③public:

Derive1(inti):

Base(i){}

voidPrint(){cout<

”Derive2’sPrint()called.“<

endl;

}_____

voidfun(_④Base*obj__)

obj->

Print();

_⑤Derive1*d1=newDerive1

(1);

_

Derive2*d2=newDerive2

(2);

fun(dl);

fun(d2);

注释:

派生类Derived1和Derived2从基类Base公有继承,它们是Base的子类型。

主程序中两次调用fun函数,该函数通过指针对象obj调用了Print函数,得到

了不同的输出结果。

而同样的消息被不同类型的对象接收时导致完全不同的行为,

恰好体现了面向对象的多态特性。

根据运行时的多态必须满足的条件,Print函数一

定是一个虚函数,并且在所有类中都必须进行定义。

由于Base类中的Print函数除

了提供一个公共的接口外,没有其他的作用,所以最好定义为纯虚函数。

12将下列程序补充完整。

classconvert

convert(doublei){vail=i;

_①virtualvoidcompute()=0;

doubleval1;

doubleval2;

};

<

voidfun(__④convert&

f_)

();

l_to_glgobj(4);

f_to_cfcobj(70);

fun(lgobj);

fun(fcobj);

13根据不同的输出结果,在函数Tone中填入正确的语句。

classInstrument

virtualvoidPrint()const{cout<

Instrument:

:

Print"

classPiano:

publicInstrument

voidPrint()const{cout<

Piano:

classGuitar:

voidPrint()const{cout<

Guitar:

voidTone(_____①_____)

___②_____

Guitarg;

Tone(g);

Pianop;

Tone(p);

(1)输出结果为:

:

Print

Instmment:

(2)输出结果为:

(1)①Instrumentobj②()

(2)①Instrument&

obj②()

参考第3题,第一次的输出是由静态联编产生的,第二次的输出是由动态态联编产生的。

 

14下列程序的运行结果如下:

Base'

scons.

Derived'

sdes.

Base(){cout<

scons."

_①virtual~Base()_{cout<

sdes."

Derived(){cout<

~Derived(){cout<

Base*ptr=_____②newDerived______

deleteptr;

三、编程

1.在作业1编程1的Point类中完成赋值运算符=、插入运算符<

、比较运算符==、!

=和加法运算符+、-的重载。

classPoint

{public:

Point(floatx=0,floaty=0,floatz=0):

x_(x),y_(y),z_(z){}

Point(constPoint&

p):

x_,y_,z_{}

3A6A9C11C12A13C14A15A16C2’“<

④Base*obj ⑤Derive1*d1=newDerive1

(1);

   Derive2*d2=newDerive2

(2);

11①virtualvoidcompute()=0;

 ②l_to_g(doublei):

convert(i){}

③public:

f_to_c(doublei):

convert(i){}

voidcompute()

{

val2=(val1-32)*5/9;

cout<

val1<

”Fahrenheitis“<

val2<

”Celsius.”<

④convert&

f

13

14 ①virtual~Base() ②newDerived 

1,

voidnegate(){x_*=-1;

y_*=-1;

z_*=-1;

doublenorm(){returnsqrt(x_*x_+y_*y_+z_*z_);

voidprint()

{cout<

'

('

x_<

"

"

y_<

z_<

)"

;

Point&

operator=(constPoint&

point);

boolPoint:

operator==(constPoint&

point)const

{returnx_==&

&

y_==&

z_==;

operator!

=(constPoint&

point)const

{returnx_!

=||y_!

=||z_!

=;

friendPointoperator+(constPoint&

p1,constPoint&

p2);

friendPointoperator-(constPoint&

friendostream&

operator<

(ostream&

ostr,constPoint&

private:

floatx_,y_,z_;

Pointoperator+(constPoint&

p2)

{returnPoint+,+,+;

Pointoperator-(constPoint&

{returnPoint&

point)

{returnostr<

("

Point&

Point:

operator=(constPoint&

{x_=;

y_=;

z_=;

return*this;

{Pointp(12,-3,4),q(14,5,12),r1,r2;

r1=p+q;

cout<

r1<

()<

r2=p-q+r1;

cout<

r2<

if(r1==r2)cout<

r1==r2"

elsecout<

r1!

=r2"

2,

Point(intx=0,inty=0,intz=0):

Point(constPoint&

operator++()

{x_++;

y_++;

z_++;

Pointoperator++(int)

{Pointtemp(*this);

x_++;

returntemp;

operator--()

{x_--;

y_--;

z_--;

Pointoperator--(int)

x_--;

intx_,y_,z_;

{Pointp(12,-3,4);

p="

();

Pointq(p++);

q="

++(++p);

p--;

3,

classShape

//Shape(){}

//~Shape(){}

virtualfloatGetArea(){return-1;

classCircle:

publicShape

Circle(floatradius):

itsRadius(radius){}

//~Circle(){}

floatGetArea(){return*itsRadius*itsRadius;

floatitsRadius;

classRectangle:

publicShape

Rectangle(floatlen,floatwidth):

itsLength(len),itsWidth(width){};

//~Rectangle(){};

floatGetArea(){returnitsLength*itsWidth;

floatGetLength(){returnitsLength;

floatGetWidth(){returnitsWidth;

floatitsWidth;

floatitsLength;

classSquare:

publicRectangle

Square(floatlen);

//~Square(){}

Square:

Square(floatlen):

Rectangle(len,len)

{}

Shape*sp;

sp=newCircle(5);

TheareaoftheCircleis"

sp->

GetArea()<

deletesp;

sp=newRectangle(4,6);

TheareaoftheRectangleis"

sp=newSquare(5);

TheareaoftheSquareis"

4,

virtualfloatGetArea()=0;

virtualfloatGetPerim()=0;

floatGetPerim(){return*itsRadius;

virtualfloatGetArea(){returnitsLength*itsWidth;

floatGetPerim(){return2*(itsLength+itsWidth);

ThePerimeteroftheCircleis"

GetPerim()<

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

当前位置:首页 > 职业教育 > 中职中专

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

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