习题8及其解答(第二版)doc

上传人:u**** 文档编号:183615341 上传时间:2021-06-09 格式:DOC 页数:5 大小:35.50KB
返回 下载 相关 举报
习题8及其解答(第二版)doc_第1页
第1页 / 共5页
习题8及其解答(第二版)doc_第2页
第2页 / 共5页
习题8及其解答(第二版)doc_第3页
第3页 / 共5页
习题8及其解答(第二版)doc_第4页
第4页 / 共5页
习题8及其解答(第二版)doc_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《习题8及其解答(第二版)doc》由会员分享,可在线阅读,更多相关《习题8及其解答(第二版)doc(5页珍藏版)》请在金锄头文库上搜索。

1、第8章 虚函数与多态性习题8 8.1 选择题1在C+中,要实现动态联编,必须使用( d )调用虚函数。(a) 类名(b) 派生类指针(c) 对象名(d) 基类指针2以下函数中,不能说明为虚函数的是( d )。(a) 私有成员函数(b) 公有成员函数(c) 构造函数(d) 析构函数3在派生类中,重载一个虚函数时,要求函数名、参数的个数、参数的类型、参数的顺序和函数的返回值( a )。(a) 相同(b)不同(c) 相容(d) 部分相同4下面函数原型中,( b )声明了fun为纯虚函数。(a) void fun()=0; (b)virtual void fun()=0;(c) virtual voi

2、d fun(); (d)virtual void fun() ;5假设一个类中含有纯虚函数,那么该类称为( c )。(a) 基类(b)纯基类(c) 抽象类(d) 派生类 6假设 Aclass为抽象类,以下正确的说明语句是( b )。(a) Aclass fun( int ) ; (b)Aclass * p ;(c) int fun( Aclass ) ; (d)Aclass Obj ;7下面描述中,正确的选项是( d )。(a) 虚函数是没有实现的函数(b) 纯虚函数是返回值等于0的函数(c) 抽象类是只有纯虚函数的类(d) 抽象类指针可以指向不同的派生类8.2 阅读以下程序,写出执行结果1#

3、include class Bclass public:Bclass( int i, int j ) x = i; y = j; virtual int fun() return 0 ; protected: int x, y ;class Iclass:public Bclass public : Iclass(int i, int j, int k):Bclass(i, j) z = k; int fun() return ( x + y + z ) / 3; private : int z ;void main() Iclass obj( 2, 4, 10 ); Bclass p1 =

4、obj; cout p1.fun() endl; Bclass & p2 = obj ; cout p2.fun() endl; cout p2.Bclass : fun() endl; Bclass *p3 = &obj; cout fun() endl;【答案】0 5 0 52#include class Base public: virtual void getxy( int i,int j = 0 ) x = i; y = j; virtual void fun() = 0 ; protected: int x , y; ;class A: public Base public:voi

5、d fun() coutx = xty = x * x = x*xendl; ;class B:public Base public: void fun() cout x = x t y = y endl; cout y = x / y = x / y getxy( 10 ); pb - fun(); pb = &obj2; pb - getxy( 100, 20 );pb - fun();【答案】x = 10 y = x*x = 100 x = 100 y = 20 y = x / y = 58.3思考题1在C+中,使用类体系依靠什么机制实现程序运行时的多态?2如果一个基类的虚函数被声明为私

6、有成员函数,会有语法错误吗?可以在应用类体系时实现动态联编吗?请你验证一下。3虚函数和纯虚函数的区别是什么?4一个非抽象类的派生类是否可以为抽象类?利用例8-11进行验证,从Hex_type类派生一个Hex_format类,其中包含一个纯虚函数Show_format(),然后定义Hex_format的派生类实现Show_format()。8.4编程题1使用虚函数编写程序求球体和圆柱体的体积及表面积。由于球体和圆柱体都可以看作由圆继承而来,所以可以定义圆类Circle作为基类。在Circle类中定义一个数据成员radius和两个虚函数area()和volume()。由Circle类派生Spher

7、e类和Column类。在派生类中对虚函数area()和volume()重新定义,分别求球体和圆柱体的体积及表面积。【解答】#include const double PI=3.14159265;class circle public: circle(double r) radius = r; virtual double area() return 0.0; virtual double volume() return 0.0; protected: double radius;class sphere:public circle public: sphere( double r ):circ

8、le( r ) double area() return 4.0 * PI * radius * radius; double volume() return 4.0 * PI * radius * radius * radius / 3.0; ;class column:public circle public: column( double r,double h ):circle( r ) height = h; double area() return 2.0 * PI * radius * ( height + radius ); double volume() return PI *

9、 radius * radius * height; private: double height;void main() circle *p; sphere sobj(2); p = &sobj; cout 球体: endl; cout 体积 = volume() endl; cout 表面积 = area() endl; column cobj( 3,5 ); p = &cobj; cout 圆柱体: endl; cout 体积 = volume() endl; cout 表面积 = area() endl;2某学校对教师每月工资的计算规定如下:固定工资+课时补贴。教授的固定工资为5000

10、元,每个课时补贴50元。副教授的固定工资为3000元,每个课时补贴30元。讲师的固定工资为2000元,每个课时补贴20元。定义教师抽象类,派生不同职称的教师类,编写程序求假设干个教师的月工资。【解答】#include #include class teacher public: teacher( char tname,int time ) strcpy( name,tname ); coursetime = time; virtual int pay() = 0; virtual void print() = 0; char *getname() return name; int getcou

11、rsetime() return coursetime; protected: char name30; int coursetime;class professor:public teacher public: professor( char pname,int time ):teacher( pname,time ) int pay() return 5000+coursetime*50; void print() cout教授:getname();class associateprofessor:public teacher public: associateprofessor( cha

12、r pname,int time ):teacher( pname,time ) int pay() return 3000 + coursetime * 30; void print() cout 副教授: getname(); ; class lecturer:public teacher public: lecturer( char pname,int time ):teacher( pname,time ) int pay() return 2000 + coursetime * 20; void print() cout 讲师: getname();图8.2 Counter类体系; void main() professor pobj( 李小平,32 ); pobj.print(); cout t 工资: pobj.pay() endl; as

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号