资源预览内容
第1页 / 共14页
第2页 / 共14页
第3页 / 共14页
第4页 / 共14页
第5页 / 共14页
第6页 / 共14页
第7页 / 共14页
第8页 / 共14页
第9页 / 共14页
第10页 / 共14页
亲,该文档总共14页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
1 概念填空题 1.1 C+支持两种多态性,分别是 编译时 和 运行时 。 1.2 在编译时就确定的函数调用称为 静态联编 ,它通过使用 函数重载,模板等 实现。 1.3 在运行时才确定的函数调用称为 动态联编 ,它通过 虚函数 来实现。 1.4 虚函数的声明方法是在函数原型前加上关键字 virtual 。在基类中含有虚函数,在派生 类中的函数没有显式写出 virtual 关键字,系统依据以下规则判断派生类的这个函数是否 是虚函数:该函数是否和基类的虚函数 同名 ;是否与基类的虚函数 参数个数相同、类型 ;是否与基类的虚函数 相同返回类型 。如果满足上述 3 个条件,派生类的函数就是 虚函 数。并且该函数 覆盖 基类的虚函数。 1.5 当通过 引用 或 指针 使用虚函数时,C+会在与对象关联的派生类中正确的选择重定 义的函数。实现了 运行时 时多态。而通过 对象 使用虚函数时,不能实现运行时时多态。1.6 纯虚函数是一种特别的虚函数,它没有函数的 函数体 部分,也没有为函数的功能提 供实现的代码,它的实现版本必须由 派生类 给出,因此纯虚函数不能是 友元函数 。拥 有纯虚函数的类就是 抽象类 类,这种类不能 实例化 。如果纯虚函数没有被重载,则派 生类将继承此纯虚函数,即该派生类也是 抽象 。 1.7 类的构造函数 不可以 (可以/不可以)是虚函数,类的析构函数可以 (可以/不可以)是 虚函数。当类中存在动态内存分配时经常将类的 析构函数 函数声明成 虚函数 。2简答题 2.1 在 C+中,能否声明虚构造函数?为什么?能否声明虚析构函数?为什么? 2.2 什么是抽象类?抽象类有何作用?可以声明抽象类的对象吗?为什么? 2.3 多态性和虚函数有何作用? 2.4 是否使用了虚函数就能实现运行时的多态性?怎样才能实现运行时的多态性? 2.5 为什么析构函数总是要求说明为虚函数?3选择题 3.1 在 C+中,要实现动态联编,必须使用(D)调用虚函数。 A.类名 B.派生类指针 C.对象名 D.基类指针 3.2 下列函数 中,不能说明为虚函数的是(C)。 A.私有成员函数 B.公有成员函数 C.构造函数 D.析构函数 3.3 在派生类中,重载一个虚函数时,要求函数名、参数的个数、参数的类型、参数的顺序和 函数的返回值(A)。A.相同 B.不同 C.相容 D.部分相同 3.4 当一个类的某个函数被说明为 virtual 时,该函数在该类的所有派生类中(A) 。A都是虚函数B只有被重新说明时才是虚函数C只有被重新说明为 virtual 时才是虚函数D都不是虚函数 3.5( C)是一个在基类中说明的虚函数,它在该基类中没有定义,但要求任何派生类都必 须定义自己的版本。A虚析构函数B虚构造函数C纯虚函数D静态成员函数3.6 以下基类中的成员函数,哪个表示纯虚函数(C) 。Avirtual void vf(int);Bvoid vf(int)=0;Cvirtual void vf( )=0;Dvirtual void vf(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 类定义如下。 class A public: virtual void func1( ) void fun2( ) ; class B:public A public: void func1( ) cout using namespace std; class A public: virtual void func( ) cout using namespace std; class A public: virtual A( ) cout #include using namespace std; class Vehicle public:virtual void showinfo()=0; protected: char Name20; ;class Car:public Vehicle public: Car(char *name) strcpy(Name,name); void showinfo()coutshowinfo (); vp= vp-showinfo (); vp= vp-showinfo (); return 0; 5.2 定义一个 shape 抽象类,派生出 Rectangle 类和 Circle 类,计算各派生类对象的面积 Area( )。 #include #include using namespace std; class Shape public: virtual void Area()=0; ;class Rectangle:public Shape public: Rectangle(double w,double h) width=w,height=h; void Area()coutArea ();sp= sp-Area (); return 0; 5.3 定义猫科动物 Animal 类,由其派生出猫类(Cat)和豹类(Leopard) ,二者都包含虚函 数 sound( ),要求根据派生类对象的不同调用各自重载后的成员函数。 #include #include using namespace std; class Animal public: virtual void Speak()=0; ; class Cat :public Animal void Speak() coutSpeak(); Leopard leopard; pa= pa-Speak(); return 0; 5.4 矩形法(rectangle)积分近似计算公式为:x(y0+y1+.yn-1)badxxf)(梯形法(1adder)积分近似计算公式为: y0+2(y1+.yn-1)+ynbadxxf)(2x辛普生法(simpson)积分近似计算公式(n 为偶数)为:y0+ yn +4(y1+y3.yn-1)+2(y2+y4+yn-2)badxxf)(3x被积函数用派生类引入,定义为纯虚函数。基类(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+xx),下限为 0.0,上限为 1.0。 解法一 #include #include using namespace std; class Integer protected: double result,a,b,step;/Intevalue 积分值,a 积分下限,b 积分上限 int n; public: virtual double fun(double x)=0;/被积函数声明为纯虚函数 virtual void Integerate() coutIntegerate();/动态,可以访问派生类定义的被积函数 bp-Print(); sinL sl(0.0,3.1415926535/2.0,100); bp= bp-Integerate();/动态,可以访问派生类定义的被积函数 bp-Print(); sinS ss(0.0,3.1415926535/2.0,100); bp= bp-Integerate();/动态,在层次中选 bp-Print();expR er(0.0,1.0,100);bp= bp-Integerate();/动态,可以访问派生类定义的被积函数 bp-Print(); expL el(0.0,1.0,100); bp= bp-Integerate();/动态,可以访问派生类定义的被积函数 bp-Print(); expS es(0.0,1.0,100); bp= bp-Integerate();/动态,在层次中选 bp-Print();otherR or(0.0,1.0,100); bp= bp-Integerate();/动态,可以访问派生类定义的被积函数 bp-Print(); otherL ol(0.0,1.0,100);/增加到 100000 也达不到辛普生法的精度 bp= bp-Integerate();/动态,可以访问派生类定义的被积函数 bp-Print(); otherS os(0.0,1.0,100); bp= bp-Integerate();/动态,在层次中选 bp-Print(); return 0; 解法二 /Integer 为抽象类,fun 为被积函数,其余 3 个虚函数为积分函数 #include #include using namespace std; class Integer protected: double result,a,b,step;/Intevalue 积分值,a 积分下限,b 积分上限 int n; public: virtual double fun(double x)=0;/被积函数声明为纯虚函数 virtual void Rectangle()=0; virtual void Ladder()=0; virtual void Simpson()=0; Integer(double ra=0,double rb=0,int nn=2000) a=ra; b=rb;n=nn; step=(b-a)/n; result=0; void Print() cout.precision(15); coutRectangle(); Inp-Print(); Inp-Ladder(); Inp-Print(); Inp-Simpson(); Inp-Print(); return 0;解法三 /Integer 为抽象类,fun 为被积函数,其余 3 个虚函数为积分函数 #include #include using namespace std; class Fun/被积函数,抽象类 public: virtual double fun(double x)=0;/被积函数声明为纯虚函数 ; class Integer/积分函数,抽象类 protected: double result,a,b,step;/Intevalue 积分值,a 积分下限,b 积分上限 int n; public: virtual void Integrate()=0; Integer(double ra=0,double rb=0,int nn=2000) a=ra; b=rb; n=nn; step=(b-a)/n; result=0; void
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号