第8章习题答案_第1页
第8章习题答案_第2页
第8章习题答案_第3页
第8章习题答案_第4页
第8章习题答案_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第八章 多态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

2、) 类名(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动态对象时

3、释放资源(D) 无意义(10)下面函数原型中,( B )声明了fun为纯虚函数。(A) void fun()=0; (B) virtual void fun()=0;(C) virtual void fun(); (D) virtual void fun() ;(11)若一个类中含有纯虚函数,则该类称为( C )。(A) 基类(B)纯基类(C) 抽象类(D) 派生类 (12)假设 Aclass为抽象类,下列正确的说明语句是( B )。(A) Aclass fun( int ) ; (B) Aclass * p ;(C) int fun( Aclass ) ; (D) Aclass Obj ;2

4、在C+中,使用类体系依靠什么机制实现程序运行时的多态?【解答】在C+中,基类指针可以指向派生类对象,以及基类中拥有虚函数,是支持多态性的前提。程序通过用同一个基类指针访问不同派生类的虚函数重载版本实现程序运行时的多态。C+的虚特性负责自动地在程序运行时把基类指针的关联类型转换成当前指向对象的派生类类型。另外,抽象类机制提供了软件抽象和可扩展性的手段,实现运行时的多态性。3如果一个类的虚函数被声明为私有成员函数,会有语法错误吗?当它作为基类时,可以在应用类体系时实现动态联编吗?请你验证一下。【解答】没有语法错误。但在应用类体系时无法实现动态编联和多态。因为私有成员函数只在类内可见,在类外无法调用

5、,无法在类外通过基类指针实现多态。程序略。4虚函数和纯虚函数的区别是什么?【解答】虚函数定义时冠以关键字virtual,本身有实现代码,作用是引导基类指针根据指向对象调用类体系中不同重载版本函数。纯虚函数是指在说明时代码“为0”的虚函数,即纯虚函数本身并没有实现代码,必须通过它的派生类定义实现版本。5阅读下列程序,写出运行结果。#include <iostream>using namespace std;class T public : T() a = 0; b = 0; c = 0; T( int i, int j, int k ) a = i; b =j; c = k; voi

6、d get( int &i, int &j, int &k ) i = a; j = b; k = c; T operator* ( T obj ); private: int a , b , c;T T:operator* ( T obj ) T tempobj; tempobj.a = a * obj.a; tempobj.b = b * obj.b; tempobj.c = c * obj.c; return tempobj;int main() T obj1( 1,2,3 ), obj2( 5,5,5 ), obj3; int a , b , c; obj3 =

7、 obj1 * obj2; obj3.get( a, b, c ); cout<<"( obj1*obj2 ): " <<"a = "<<a<<'t'<<"b = "<<b<<'t'<<"c = "<<c<<'t'<<endl; (obj2*obj3).get( a, b, c ); cout<<"( obj2*ob

8、j3 ): " <<"a = "<<a<<'t'<<"b = "<<b<<'t'<<"c = "<<c<<'t'<<endl; 【解答】( obj1 * obj2 ): a = 5 b = 10 c = 15 ( obj2 * obj3 ): a = 25 b = 50 c = 756阅读下列程序,写出运行结果。#include <iostream>

9、;using namespace std;class Vector public: Vector() Vector(int i,int j) x = i ; y = j ; friend Vector operator+ ( Vector v1, Vector v2 ) Vector tempVector ; tempVector.x = v1.x + v2.x ; tempVector.y = v1.y + v2.y ; return tempVector ; void display() cout << "( " << x << &q

10、uot;, " << y << ") "<< endl ; private: int x , y ;int main() Vector v1( 1, 2 ), v2( 3, 4 ), v3 ; cout << "v1 = " ; v1.display() ; cout << "v2 = " ; v2.display() ; v3 = v1 + v2 ; cout << "v3 = v1 + v2 = " ; v3.display()

11、;【解答】v1 = ( 1, 2 ) v2 = ( 3, 4 ) v3 = v1 + v2 = ( 4, 6 )7阅读下列程序,写出运行结果。#include <iostream>using namespace std;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)

12、 z = k; int fun() return ( x + y + z ) / 3; private : int z ;int main() Iclass obj( 2, 4, 10 ); Bclass p1 = obj; cout << p1.fun() << endl; Bclass &p2 = obj ; cout << p2.fun() << endl; cout << p2.Bclass : fun() << endl; Bclass *p3 = &obj; cout << p3 -

13、> fun() << endl;【解答】0 5 0 58阅读下列程序,写出运行结果。#include <iostream>using namespace std;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: void fun() cout<<"x = "<<x<<

14、;'t'<<"y = x * x = "<<x*x<<endl; ;class B : public Base public: void fun() cout << "x = " << x << 't' << "y = " << y << endl; cout << "y = x / y = " << x / y << endl; ;int

15、main() Base * pb; A obj1; B obj2; pb = &obj1; pb -> 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 = 59分别使用成员函数和友员函数编写程序重载运算符“+”,使该运算符能实现两个字符串的连接。【解答】(1) 使用成员函数#include <iostream>#include<cstrin

16、g>using namespace std;class s public: s() *str = '0' s( char *pstr ) strcpy( str,pstr ); char *gets() return str; s operator+( s obj ); private: char str10; ;s s:operator+( s obj ) strcat( str,obj.str ); return str; /或return *this int main() s obj1( "Visual" ),obj2( " C+&qu

17、ot; ),obj3; obj3 = obj1 + obj2; cout << obj3.gets() << endl;(2)使用友员函数#include <iostream>#include<cstring>using namespace std;class s public: s() *str= '0' s( char *pstr ) strcpy( str,pstr ); char *gets() return str; friend s operator+( s obj1,s obj2 ); private: char s

18、tr100; ; s operator+( s obj1,s obj2 ) s tempobj; strcat( tempobj.str,obj1.str ); strcat( tempobj.str,obj2.str ); return tempobj; int main() s obj1( "Visual" ),obj2( " C+" ),obj3; obj3 = obj1 + obj2; cout << obj3.gets() << endl;10定义一个整数计算类Integer,实现短整数 +,-,*,/ 基本算术运算。要

19、求可以进行数据范围检查(-3276832767,或自行设定),数据溢出时显示错误信息并中断程序运行。【解答】#include <iostream>using namespace std;class Integer private: short a; public:Integer (short n=0) a=n; Integer operator +(Integer); Integer operator -(Integer); Integer operator *(Integer); Integer operator /(Integer); Integer operator =(In

20、teger);void display() cout<<a<<endl; ;Integer Integer:operator+(Integer x) Integer temp; if(a+x.a<-32768|a+x.a>32767) cout<<"Data overflow!"<<endl; abort(); temp.a=a+x.a; return temp; Integer Integer:operator-(Integer x) Integer temp;if(a-x.a<-32768|a-x.a&g

21、t;32767) cout<<"Data overflow!"<<endl; abort(); temp.a=a-x.a;return temp; Integer Integer:operator*(Integer x) Integer temp;if(a*x.a<-32768|a*x.a>32767) cout<<"Data overflow!"<<endl; abort();temp.a=a*x.a;return temp; Integer Integer:operator/(Integer

22、 x) Integer temp; if(a/x.a<-32768|a/x.a>32767) cout<<"Data overflow!"<<endl; abort(); temp.a=a/x.a; return temp; Integer Integer:operator=(Integer x) a=x.a; return *this; int main() Integer A(90),B(30),C;cout<<"A="A.display();cout<<"B="B.di

23、splay();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和两个虚函数are

24、a和volume。由Circle类派生Sphere类和Column类。在派生类中对虚函数area和volume重新定义,分别求球体和圆柱体的体积及表面积。【解答】#include <iostream>using namespace std;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;cl

25、ass sphere:public circle public: sphere( double r ):circle( 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 * r

26、adius * ( height + radius ); double volume() return PI * radius * radius * height;private: double height;int main() circle *p; sphere sobj(2); p = &sobj; cout << "球体:" << endl; cout << "体积 = " << p->volume() << endl; cout << "表面积 =

27、" << p->area() << endl; column cobj( 3,5 ); p = &cobj; cout << "圆柱体:" << endl; cout << "体积 = " << p->volume() << endl; cout << "表面积 = " << p->area() << endl;12某学校对教师每月工资的计算规定如下:固定工资+课时补贴。教授的固定

28、工资为5000元,每个课时补贴50元。副教授的固定工资为3000元,每个课时补贴30元。讲师的固定工资为2000元,每个课时补贴20元。定义教师抽象类,派生不同职称的教师类,编写程序求若干个教师的月工资。【解答】#include <iostream>using namespace std;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 getcoursetime() return coursetime; protected: char name30;int coursetime; ;class professor:public teacher public: professor( char pname,int time ):teacher( pnam

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论