第8章习题答案.docx

上传人:b****6 文档编号:7903738 上传时间:2023-01-27 格式:DOCX 页数:14 大小:19.34KB
下载 相关 举报
第8章习题答案.docx_第1页
第1页 / 共14页
第8章习题答案.docx_第2页
第2页 / 共14页
第8章习题答案.docx_第3页
第3页 / 共14页
第8章习题答案.docx_第4页
第4页 / 共14页
第8章习题答案.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

第8章习题答案.docx

《第8章习题答案.docx》由会员分享,可在线阅读,更多相关《第8章习题答案.docx(14页珍藏版)》请在冰豆网上搜索。

第8章习题答案.docx

第8章习题答案

第八章多态

1.单选题

(1).下列关于运算符重载的描述中,(D)是正确的。

(A)可以改变参与运算的操作数个数(B)可以改变运算符原来的优先级

(C)可以改变运算符原来的结合性(D)不能改变原运算符的语义

(2).下列函数中,不能重载运算符的函数是(b)。

(A)成员函数(B)构造函数(C)普通函数(D)友员函数

(3).要求用成员函数重载的运算符是(A)。

(A)=(B)==(C)<=(D)++

(4).要求用友员函数重载的运算符是(C)。

(A)=(B)[](C)<<(D)()

(5).在C++中,要实现动态联编,必须使用(D)调用虚函数。

(A)类名(B)派生类指针(C)对象名(D)基类指针

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

(A)私有成员函数(B)公有成员函数(C)构造函数(D)析构函数

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

(A)相同(B)不同(C)相容(D)部分相同

(8).C++中,根据(D)识别类层次中不同类定义的虚函数版本。

(A)参数个数(B)参数类型(C)函数名(D)this指针类型

(9).虚析构函数的作用是(C)。

(A)虚基类必须定义虚析构函数(B)类对象作用域结束时释放资源

(C)delete动态对象时释放资源(D)无意义

(10).下面函数原型中,(B)声明了fun为纯虚函数。

(A)voidfun()=0;(B)virtualvoidfun()=0;

(C)virtualvoidfun();(D)virtualvoidfun(){};

(11).若一个类中含有纯虚函数,则该类称为(C)。

(A)基类(B) 纯基类(C)抽象类(D)派生类

(12).假设Aclass为抽象类,下列正确的说明语句是(B)。

(A)Aclassfun(int);(B)Aclass*p;

(C)intfun(Aclass);(D)AclassObj;

2.在C++中,使用类体系依靠什么机制实现程序运行时的多态?

【解答】

在C++中,基类指针可以指向派生类对象,以及基类中拥有虚函数,是支持多态性的前提。

程序通过用同一个基类指针访问不同派生类的虚函数重载版本实现程序运行时的多态。

C++的虚特性负责自动地在程序运行时把基类指针的关联类型转换成当前指向对象的派生类类型。

另外,抽象类机制提供了软件抽象和可扩展性的手段,实现运行时的多态性。

3.如果一个类的虚函数被声明为私有成员函数,会有语法错误吗?

当它作为基类时,可以在应用类体系时实现动态联编吗?

请你验证一下。

【解答】

没有语法错误。

但在应用类体系时无法实现动态编联和多态。

因为私有成员函数只在类内可见,在类外无法调用,无法在类外通过基类指针实现多态。

程序略。

4.虚函数和纯虚函数的区别是什么?

【解答】

虚函数定义时冠以关键字virtual,本身有实现代码,作用是引导基类指针根据指向对象调用类体系中不同重载版本函数。

纯虚函数是指在说明时代码“为0”的虚函数,即纯虚函数本身并没有实现代码,必须通过它的派生类定义实现版本。

5.阅读下列程序,写出运行结果。

#include

usingnamespacestd;

classT

{public:

T(){a=0;b=0;c=0;}

T(inti,intj,intk)

{a=i;b=j;c=k;}

voidget(int&i,int&j,int&k)

{i=a;j=b;k=c;}

Toperator*(Tobj);

private:

inta,b,c;

};

TT:

:

operator*(Tobj)

{Ttempobj;

tempobj.a=a*obj.a;

tempobj.b=b*obj.b;

tempobj.c=c*obj.c;

returntempobj;

}

intmain()

{Tobj1(1,2,3),obj2(5,5,5),obj3;

inta,b,c;

obj3=obj1*obj2;

obj3.get(a,b,c);

cout<<"(obj1*obj2):

"

<<"a="<

(obj2*obj3).get(a,b,c);

cout<<"(obj2*obj3):

"

<<"a="<

}

【解答】

(obj1*obj2):

a=5b=10c=15

(obj2*obj3):

a=25b=50c=75

6.阅读下列程序,写出运行结果。

#include

usingnamespacestd;

classVector

{public:

Vector(){}

Vector(inti,intj)

{x=i;y=j;

}

friendVectoroperator+(Vectorv1,Vectorv2)

{VectortempVector;

tempVector.x=v1.x+v2.x;

tempVector.y=v1.y+v2.y;

returntempVector;

}

voiddisplay()

{cout<<"("<

private:

intx,y;

};

intmain()

{Vectorv1(1,2),v2(3,4),v3;

cout<<"v1=";

v1.display();

cout<<"v2=";

v2.display();

v3=v1+v2;

cout<<"v3=v1+v2=";

v3.display();

}

【解答】

v1=(1,2)

v2=(3,4)

v3=v1+v2=(4,6)

7.阅读下列程序,写出运行结果。

#include

usingnamespacestd;

classBclass

{public:

Bclass(inti,intj){x=i;y=j;}

virtualintfun(){return0;}

protected:

intx,y;

};

classIclass:

publicBclass

{public:

Iclass(inti,intj,intk):

Bclass(i,j){z=k;}

intfun(){return(x+y+z)/3;}

private:

intz;

};

intmain()

{Iclassobj(2,4,10);

Bclassp1=obj;

cout<

Bclass&p2=obj;

cout<

cout<

:

fun()<

Bclass*p3=&obj;

cout<fun()<

}

【解答】

0

5

0

5

8.阅读下列程序,写出运行结果。

#include

usingnamespacestd;

classBase

{public:

virtualvoidgetxy(inti,intj=0){x=i;y=j;}

virtualvoidfun()=0;

protected:

intx,y;

};

classA:

publicBase

{public:

voidfun()

{cout<<"x="<

};

classB:

publicBase

{public:

voidfun()

{cout<<"x="<

cout<<"y=x/y="<

}

};

intmain()

{Base*pb;

Aobj1;

Bobj2;

pb=&obj1;

pb->getxy(10);

pb->fun();

pb=&obj2;

pb->getxy(100,20);

pb->fun();

}

【解答】

x=10y=x*x=100

x=100y=20

y=x/y=5

9.分别使用成员函数和友员函数编写程序重载运算符“+”,使该运算符能实现两个字符串的连接。

【解答】

(1)使用成员函数

#include

#include

usingnamespacestd;

classs

{public:

s(){*str='\0';}

s(char*pstr)

{strcpy(str,pstr);

}

char*gets()

{returnstr;

}

soperator+(sobj);

private:

charstr[10];

};

ss:

:

operator+(sobj)

{strcat(str,obj.str);

returnstr;//或return*this

}

intmain()

{sobj1("Visual"),obj2("C++"),obj3;

obj3=obj1+obj2;

cout<

}

(2)使用友员函数

#include

#include

usingnamespacestd;

classs

{public:

s(){*str='\0';}

s(char*pstr)

{strcpy(str,pstr);

}

char*gets()

{returnstr;

}

friendsoperator+(sobj1,sobj2);

private:

charstr[100];

};

soperator+(sobj1,sobj2)

{stempobj;

strcat(tempobj.str,obj1.str);

strcat(tempobj.str,obj2.str);

returntempobj;

}

intmain()

{sobj1("Visual"),obj2("C++"),obj3;

obj3=obj1+obj2;

cout<

}

10.定义一个整数计算类Integer,实现短整数+,-,*,/基本算术运算。

要求可以进行数据范围检查(-32768~32767,或自行设定),数据溢出时显示错误信息并中断程序运行。

【解答】

#include

usingnamespacestd;

classInteger

{private:

shorta;

public:

Integer(shortn=0){a=n;}

Integeroperator+(Integer);

Integeroperator-(Integer);

Integeroperator*(Integer);

Integeroperator/(Integer);

Integeroperator=(Integer);

voiddisplay()

{cout<

};

IntegerInteger:

:

operator+(Integerx)

{Integertemp;

if(a+x.a<-32768||a+x.a>32767)

{cout<<"Dataoverflow!

"<

temp.a=a+x.a;

returntemp;

}

IntegerInteger:

:

operator-(Integerx)

{Integertemp;

if(a-x.a<-32768||a-x.a>32767)

{cout<<"Dataoverflow!

"<

temp.a=a-x.a;

returntemp;

}

IntegerInteger:

:

operator*(Integerx)

{Integertemp;

if(a*x.a<-32768||a*x.a>32767){cout<<"Dataoverflow!

"<

temp.a=a*x.a;

returntemp;

}

IntegerInteger:

:

operator/(Integerx)

{Integertemp;

if(a/x.a<-32768||a/x.a>32767)

{cout<<"Dataoverflow!

"<

temp.a=a/x.a;

returntemp;

}

IntegerInteger:

:

operator=(Integerx)

{a=x.a;

return*this;

}

intmain()

{IntegerA(90),B(30),C;

cout<<"A=";A.display();

cout<<"B=";B.display();

C=A+B;

cout<<"C=A+B=";C.display();

C=A-B;

cout<<"C=A-B=";C.display();

C=A*B;

cout<<"C=A*B=";C.display();

C=A/B;

cout<<"C=A/B=";C.display();

}

11.使用虚函数编写程序求球体和圆柱体的体积及表面积。

由于球体和圆柱体都可以看做由圆继承而来,所以可以把圆类Circle作为基类。

在Circle类中定义一个数据成员radius和两个虚函数area和volume。

由Circle类派生Sphere类和Column类。

在派生类中对虚函数area和volume重新定义,分别求球体和圆柱体的体积及表面积。

【解答】

#include

usingnamespacestd;

constdoublePI=3.14159265;

classcircle

{public:

circle(doubler){radius=r;}

virtualdoublearea(){return0.0;}

virtualdoublevolume(){return0.0;}

protected:

doubleradius;

};

classsphere:

publiccircle

{public:

sphere(doubler):

circle(r){}

doublearea()

{return4.0*PI*radius*radius;}

doublevolume()

{return4.0*PI*radius*radius*radius/3.0;}

};

classcolumn:

publiccircle

{public:

column(doubler,doubleh):

circle(r){height=h;}

doublearea()

{return2.0*PI*radius*(height+radius);}

doublevolume()

{returnPI*radius*radius*height;}

private:

doubleheight;

};

intmain()

{circle*p;

spheresobj

(2);

p=&sobj;

cout<<"球体:

"<

cout<<"体积="<volume()<

cout<<"表面积="<area()<

columncobj(3,5);

p=&cobj;

cout<<"圆柱体:

"<

cout<<"体积="<volume()<

cout<<"表面积="<area()<

}

12.某学校对教师每月工资的计算规定如下:

固定工资+课时补贴。

教授的固定工资为5000元,每个课时补贴50元。

副教授的固定工资为3000元,每个课时补贴30元。

讲师的固定工资为2000元,每个课时补贴20元。

定义教师抽象类,派生不同职称的教师类,编写程序求若干个教师的月工资。

【解答】

#include

usingnamespacestd;

classteacher

{public:

teacher(chartname[],inttime)

{strcpy(name,tname);

coursetime=time;

}

virtualintpay()=0;

virtualvoidprint()=0;

char*getname()

{returnname;}

intgetcoursetime()

{returncoursetime;}

protected:

charname[30];

intcoursetime;

};

classprofessor:

publicteacher

{public:

professor(charpname[],inttime):

teacher(pname,time){}

intpay()

{return5000+coursetime*50;}

voidprint()

{cout<<"教授:

"<

};

classassociateprofessor:

publicteacher

{public:

associateprofessor(charpname[],inttime):

teacher(pname,time){}

intpay()

{return3000+coursetime*30;}

voidprint()

{cout<<"副教授:

"<

};

classlecturer:

publicteacher

{public:

lecturer(charpname[],inttime):

teacher(pname,time){}

intpay()

{return2000+coursetime*20;}

voidprint()

{cout<<"讲师:

"<

};

intmain()

{professorpobj("李小平",32);

pobj.print();

cout<<'\t'<<"工资:

"<

associateprofessorapobj("王芳芳",56);

apobj.print();

cout<<'\t'<<"工资:

"<

lecturerlobj("何大建",72);

lobj.print();

cout<<'\t'<<"工资:

"<

}

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

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

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

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