

下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1.下列关于动态联编的描述中,错误的就是_ 。DA动态联编就是以虚函数为基础的B) 动态联编就是在运行时确定所调用的函数代码的C) 动态联编调用函数操作就是指向对象的指针或对象引用D)动态联编就是在编译时确定操作函数的注:先期联编也称静态联编,迟后联编也称动态联编。 注释:动态联编一直要到程序运行时才能确定调用哪个函数。A虚函数就是一个静态成员函数B) 虚函数就是一个非成员函数C) 虚函数既可以在函数说明时定义,也可以在函数实现时定义D)派生类的虚函数与基类中对应的虚函数具有相同的参数个数与类型注释:虚函数就是非静态的成员函数。它不能就是友元函数,但可以在另一个类中被声明为友元函数。虚函数声明
2、只能出现在类定义的函数原型声明中,而不能在成员函数的函数体实现的时候。派生类的虚函数与基类中对应的虚函数必须满足下列条件,否则派生类中的虚函数将丢失其虚特性,在调用时进行静态联编:派生类中的虚函数与基类中的虚函数具有相同的名称:派生类中的虚函数与基类中的虚函数具有相同的参数个数与相同的对应参数 类型:派生类中的虚函数与基类中的虚函数的返回值或者相同,或者都返回指针或引用,并且派生类虚函数所返回的指针或引用的基类型就是基类中的虚函数所返回的指针或引用的基类型的子类型。3 在下面四个选项中,_ 就是用来声明虚函数的。AA)virtual B)public C)usi ngD)false注释:说明虚
3、函数的一般格式如下:virtua1函数返回类型函数名 ( 参数表)对虚函数的调用。 DA) 一定使用动态联编B)必须使用动态联编C) 一定使用静态联编D)不一定使用动态联编注释:参见第 1 题的注释。5 实现运行时的多态性要使用 _。DA)重载函数 B)构造函数 C)析构函数D)虚函数作业 4选择题虚函数就是实现动态联编的必要条件之一。没有虚函数一定不能实现动态联编,但2 关于虚函数的描述中,正确的就是_ 。D注释:参见第 1 题的注释。6 要实现动态联编,必须通过_调用虚函数。AA)对象指针 B)成员名限定C)对象名 D)派生类名注释:参见第 1 题的注释。7 在派生类中重新定义虚函数时,除
4、了_ 方面,其她方面都必须与基类中相应的虚函数保持一致。DA)参数个数 B)参数类型C)函数名称D)函数体注释:参见第 2 题的注释。8 下面关于构造函数与析构函数的描述,错误的就是_。DA)析构函数中调用虚函数采用静态联编B)对虚析构函数的调用可以采用动态联编C)当基类的析构函数就是虚函数时,其派生类的析构函数也一定就是虚函数D)构造函数可以声明为虚函数注释:构造函数不能声明为虚函数,但析构函数可以声明为虚函数。当基类的析构函数声明为虚函数时,无论派生类就是否使用virtual关键字说明,派生类的析构函数一疋就是虚函数,对缺省析构函数亦然。而且,如果满足动态联编的其她条件,对虚析构函数的调用
5、将采用动态联编。构造函数不能声明为虚函数,但在构造函数中可以调用虚函数。在构造函数或析 构函数中调用虚函数,将采用静态联编。9 关于纯虚函数与抽象类的描述中,错误的就是_。CA)纯虚函数就是一种特殊的虚函数,它没有具体的实现B)抽象类就是指具有纯虚函数的类C)一个基类中说明有纯虚函数,该基类的派生类一定不再就是抽象类D)抽象类只能作为基类来使用,其纯虚函数的实现由派生类给出注释:带有纯虚函数的类称为抽象类。抽象类中的纯虚函数的实现由派生类给出:但派生类仍可不给出纯虚函数的定义,继续作为抽象类存在。10 下列描述中,_就是抽象类的特性。DA)可以说明虚函数 B)可以进行构造函数重载C)可以定义友
6、元函数 D)不能说明其对象注释:抽象类区别于其她类的最根本的特征就是不能定义对象。11 _ 就是一个在基类中说明的虚函数,它在该基类中没有定义,但要求任何派生类都必须定义自己的版本。CA)虚析构函数 B)虚构造函数C)纯虚函数D)静态成员函数12 如果一个类至少有一个纯虚函数,那么就称该类为_。AA)抽象类 B)虚基类 C)派生类 D)以上都不对13 以下成员函数表示纯虚函数。CA)virtual int vf(i nt); B)void vf(in t)=0;C)virtual void vf()=0; D)virtual void vf(in t)()注释:纯虚函数的声明格式如下:virt
7、ual 函数返回类型 函数名 (参数表)=0;注意纯虚函数与虚函数体为空的区别。纯虚函数根本就没有函数体,而空的虚函数的函数体为空:前者所在的类就是抽象类,不能直接进行实例化,而后者所在的类就是可以实例化的:14 下面的描述中,正确的就是_。AA) virtual可以用来声明虚函数B) 含有纯虚函数的类就是不可以用来创建对象的,因为它就是虚基类C) 即使基类的构造函数没有参数,派生类也必须建立构造函数D) 静态数据成员可以通过成员初始化列表来初始化注释:virtual关键字既可以用来声明虚基类,也可以用来声明虚函数。含有纯虚函数的类就是抽象类,它不能用来定义对象。静态数据成员的初始化必须在类体
8、外进行。如果所有的基类与子对象构造函数都不需要参数,派生类也不需要参数时,派生类构造函数可以不定义。15 在下面程序中,A、B C D 四句编译时不会出现错误的就是_。A#includeclass Basepublic:Base() Base(i nt c): coun t(c) virtual void prin t() con st=0;private:int count;class Derived: public Basepublic:Derived() :Base(0 ) Derived(i nt c): Base(c) void prin t()c onst coutDerived#
9、inelude class Basepublic:virtual char *fun() con st=0;char* Base:fun() const returnBase”; class Derivedll: virtual public Basepublic:char* fun() const return Derivedll; ;class Derivedl2: virtual public Basepublic:char* fun() const return Derivedl2; ;class Derived2: public Derivedl1,public Derivedl2p
10、ublic:char* fun() constchar *ptr;ptr=new charstrle n( Derivedl1:fu n( )+strle n( Derivedl2:fu n( )+1;strcpy(ptr,Derived11:fu n(),strcat(ptr,Derived12:fu n();return ptr;void mai n()Base *pb;pb=new Derivedll;coutfu n() e ndl;pb=new Derivedl2;coutfu n() e ndl;pb=new Derived2;coutfu n() class Basepublic
11、:Base( int i) b=i;_virtual void Prin t()=0;_protected:int b;class Derivel :public Basepublic:_Derive1( int i ):Base( i ) _void Print()cout”Derive1s Print() Galledendl;class Derive2:public Base一 public:Derive1(int i ): Base( i ) void Print( ) cout” Derive2 s Print() c“lPri nt();void mai n() Derivel *
12、d 仁 new Derive1(1);_Derive2 *d2=new Derive2 (2); |fun (dl);fun (d2);注释:派生类 Derivedl 与 Derived2 从基类 Base 公有继承,它们就是 Base 的子类型。主程序中两次调用 fun 函数,该函数通过指针对象obj 调用了 Print 函数,得到了不同的输出结果。而同样的消息被不同类型的对象接收时导致完全不同的行为,恰好体现了面向对象的多态特性。根据运行时的多态必须满足的条件,Print 函数一定就是一个虚函数,并且在所有类中都必须进行定义。由于Base 类中的 Print 函数了提供一个公共的接口外,没
13、有其她的作用,所以最好定义为纯虚函数。将下列程序补充完整。#inelude viostream、 hclass convertpublic:con vert(double i) vail=i;_ virtual void compute( )=0;protected:double val1;double val2;/liters to gall onsclass l_to_g:public convertpublic:_l_to_g(double i ) : con vert(i) void compute()val2=val1/3 、7854;coutval1 liters is val2
14、gall ons ;/Fahre nheit to Celsiusclass f_to_c:public convert一 public:f_to_c(double i ):c on vert( i ) void compute()val2=(val1-32)*5/9;coutval1 ” Fahrenheit is“ val2、Cefeiesdl;void fun_ convert& f _)f、compute();void mai n()除12、class In strume ntpublic:virtual void Prin t() const coutI nstrume nt
15、:Pri nte ndl; ;class Piano: public In strume ntpublic:void Prin t()co nst coutPia no:Pri nte ndl; ;class Guitar: public In strume ntpublic:void Prin t() const coutGuitar:Pri ntclass Basepublic:Base() coutBases cons 、endl; _virtual Base( )_ coutBases des 、endl; ;class Derived:public Basepublic:Derive
16、d() coutDeriveds cons、 endl; Derived() coutDeriveds des、endl; ;void mai n()Base *ptr=_ new Derived _delete ptr;三、编程1.在作业 1 编程 1 的 Point 类中完成赋值运算符=、插入运算符 、比较运算符=、! 加法运算符+、-的重载。#include #include class Point public:Point(float x=0, float y=0, float z=0): x_(x), y_(y), z_(z) Point(const Point& p) :
17、x_(p、x_), y_(p、y_), z_(p、z_) 形参 point 为常引用,它所引用的对象不能被更新,即传给它的实参不能被更新void n egate() x_ *= -1; y_ *= -1; z_ *= -1; double norm() return sqrt(x_*x_ + y_*y_ + z_*z_); void prin t() cout ( x_ , y_ , z_ );Point& operator=(const Point& point);bool Point:operator=(const Point& point) const/ 常成员函
18、数 ,只有它才有资格操作常量与常对象 return x_ = point 、 x_ & y_ = point 、 y_ & z_ = point 、 z_;bool Point:operator!=(const Point& point) const return x_ != point 、 x_ | y_ != point 、 y_ | z_ != point 、 z_;friend Point operator+(const Point& p1, const Point& p2);friend Point operator-(const Point&
19、amp; p1, const Point& p2);friend ostream& operator(ostream& ostr, const Point& point);private:float x_, y_, z_;Point operator+(const Point& p1, const Point& p2) return Point(p1 、 x_+p2 、x_,p1、y_+p2、y_,p1、z_+p2 、z_);Point operator-(const Point& p1, const Point& p2) ret
20、urn Point(p1 、 x_-p2 、 x_,p1 、 y_-p2 、 y_,p1 、 z_-p2 、 z_);ostream& operator(ostream& ostr, const Point& point) return ostr ( point 、x_ , point 、 y_ , point 、z_ );Point& Point:operator=(const Point& point) x_ = point 、 x_;y_ = point 、 y_;z_ = point 、 z_;return *this;void main() P
21、oint p(12,-3,4),q(14,5,12),r1,r2;r1=p+q; coutr1r1 、 norm()endl;r2=p-q+r1;cout r2r2 、 norm()endl; if(r1=r2)cout r1=r2endl;else cout r1!=r2endl;2 假设 Point 类的坐标为整型 ,对它重载 +(自增 )、-(自减)运算符 (包括前后缀 )。 #include #include class Point public:Point(int x=0, int y=0, int z=0): x_(x), y_(y), z_(z) Point(const Poin
22、t& p) : x_(p 、x_), y_(p 、 y_), z_(p 、z_) Point& operator +() x_+;y_+;z_+;return *this;/为什么要返回引用 ,下列代码行不?/Point operator +()/ x_+;y_+;z_+;/ return *this;/Point operator +(int)/为什么不必返回引用 Point temp(*this); x_+;y_+;z_+;return temp;Point& operator -() x_-;y_-;z_-;return *this;Point operator
23、-(int) Point temp(*this);x_-;y_-;z_-;return temp;void print() cout ( x_ , y_ , z_ );private:int x_, y_, z_;void main() Point p(12,-3,4);cout p = ;p、print();Point q(p+);cout q = ;q、print();cout p = ;p、print();+(+p);cout p = ;p、print();p-;cout p = ;p、print();3.定义一个 Shape 基类,由它派生出 Rectangle 与 Circle 类,
24、二者都有 GetArea()函数计算对 象的面积。使用 Rectangle 类创建一个派生类 Square。#include class Shape public:/Shape() /Shape() virtual float GetArea()return -1;class Circle:public Shapepublic:Circle(float radius):itsRadius(radius)/Circle()float GetArea()return 3 、 14*itsRadius*itsRadius;private:float itsRadius;class Rectangle
25、 : public Shapepublic:Rectangle(float len,float width): itsLength(len),itsWidth(width);/Rectangle();float GetArea()return itsLength*itsWidth;float GetLength()return itsLength;float GetWidth()return itsWidth;private:float itsWidth;float itsLength;class Square:public Rectanglepublic:Square(float len);
26、/Square();Square:Square(float len): Rectangle(len,len) void main()Shape *sp;sp=new Circle(5);cout The area of the Circle is GetArea()endl; delete sp;sp=new Rectangle(4,6);cout The area of the Rectangle is GetArea()endl; delete sp;sp=new Square(5);cout The area of the Square is GetArea()endl; delete
27、sp;4.定义一个 Shape 抽象类,由它派生出 Rectanglr 与 Circle 类,二者都有 GetArea()函数计算对 象的面积 ,GetPerim( ) 函数计算对象的周长。#include class Shape public:/Shape() /Shape() virtual float GetArea()=0;virtual float GetPerim()=0;class Circle:public Shapepublic:Circle(float radius):itsRadius(radius)/Circle()float GetArea()return 3 、 1
28、4*itsRadius*itsRadius;float GetPerim()return 6 、28*itsRadius;private:float itsRadius;class Rectangle : public Shapepublic:Rectangle(float len,float width): itsLength(len),itsWidth(width);/Rectangle();virtual float GetArea()return itsLength*itsWidth;float GetPerim()return 2*(itsLength+itsWidth);priva
29、te:float GetLength()return itsLength;float GetWidth()return itsWidth;private:float itsWidth;float itsLength;void main()Shape *sp;sp=new Circle(5);cout The area of the Circle is GetArea()endl;cout The Perimeter of the Circle is GetPerim()endl; delete sp;sp=new Rectangle(4,6);cout The area of the Rect
30、angle is GetArea()endl;cout The Perimeter of the Rectangle is GetPerim()endl; delete sp;作业 6 答案:一,选择题 :1 D 2 D 3 A 4 D 5 D 6 A 7 D 8 D 9 C 10 D 11 C 12 A 13 C 14 A 15 A 16 C 17 B 二,填空题 :2程序运行 程序编译2 编译时的 运行时的 虚函数3动态 静态4虚函数 指针或引用5动态6静态7不支持 支持8 =09纯虚函数 基类10 定义对象 声明抽象类的指针或引用11 virtual void Print()=0; De
31、rive1( int i ):Base( i ) public:Derive1(int i ): Base( i ) void Print( ) cout”Derive2 s Print( ) called Base *obj Derive1 *d1=new Derive1(1);_Derive2 *d2=new Derive2(2);12 virtual void compute( )=0; l_to_g(double i ) : convert(i) public:f_to_c(double i ):convert( i ) voidcompute( )1,#include #includ
32、e class Point public:val2=(val1-32)*5/9;coutval1 ” Fahrenheit is convert& f13(1) Instrument obj(2) Instrument& obj14 virtual Base( ) new obj 、 Print( )obj、Print()Derived三、编程val2、 ”Ceelsniudsl; endl; Point(float x=0, float y=0, float z=0): x_(x), y_(y), z_(z) Point(constPoint& p) : x_(p 、
33、x_), y_(p 、 y_), z_(p 、 z_) void negate() x_ *=-1; y_ *= -1; z_ *= -1; double norm() return sqrt(x_*x_ + y_*y_ +z_*z_); void print() cout ( x_ , y_ , z_ );Point& operator=(const Point& point);bool Point:operator=(const Point& point) const return x_ = point 、 x_ & y_ = point 、 y_ &
34、; z_ = point 、bool Point:operator!=(const Point& point) const return x_ != point 、 x_ | y_ != point 、 y_ | z_ != point 、 z_;friend Point operator+(const Point& p1, const Point& p2);friend Point operator-(const Point& p1, const Point& p2);friend ostream& operator(ostream&
35、ostr, const Point& point); private:float x_, y_, z_;Point operator+(const Point& p1, const Point& p2) return Point(p1 、x_+p2、x_,p1、y_+p2、y_,p1 、z_+p2 、 z_);Point operator-(const Point& p1, const Point& p2) return Point(p1 、 x_-p2 、 x_,p1 、 y_-p2、 y_,p1、z_-p2、z_);ostream& oper
36、ator(ostream& ostr, const Point& point) return ostr ( point 、 x_ , point 、 y_ , point 、Point& Point:operator=(const Point& point) x_ = point 、 x_;y_ = point 、 y_;z_ = point 、 z_;return *this;void main() Point p(12,-3,4),q(14,5,12),r1,r2;r1=p+q;coutr1r1 、 norm()endl;r2=p-q+r1;cout r2r
37、2 、 norm()endl;if(r1=r2)cout r1=r2endl;else cout r1!=r2endl;z_;z_ );2, #include #include class Point public:Point(int x=0, int y=0, int z=0): x_(x), y_(y), z_(z) Point(const Point& p) : x_(p 、x_), y_(p 、 y_), z_(p 、z_) Point&operator +() x_+;y_+;z_+;return *this;Point operator +(int) Point t
38、emp(*this); x_+;y_+;z_+;return temp;Point& operator -() x_-;y_-;z_-;return *this;Point operator -(int) Point temp(*this);x_-;y_-;z_-;return temp;void print() cout ( x_ , y_ , z_ );private:int x_, y_, z_;void main() Point p(12,-3,4);cout p = ;p、print();Point q(p+); cout q = q、print();cout p = p、p
39、rint();+(+p); cout p = p、print();p-;cout p = ;p、print();3,#include class Shape public:/Shape() /Shape() virtual float GetArea()return -1; ;class Circle:public Shapepublic:Circle(float radius):itsRadius(radius)/Circle()float GetArea()return 3 、 14*itsRadius*itsRadius; private:float itsRadius;class Rectangle : public Shapepublic:Rectangle(float len,float width): itsLength(len),itsWidth(width);/Rectangle();float GetArea()return itsLength*itsWidth;float GetLength()return itsLength;float
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 护理安全管理
- 大班家长会活动情况总结模版
- 山东省济宁市2025年高考模拟考试英语试题及答案(济宁三模)
- 浅析药品GMP修订草案邓海根老师-09-08-04
- 小学开展语言文字工作总结模版
- 心房内传导阻滞的临床护理
- 统编人教版三年级语文下册《口语交际:春游去哪儿玩》公开课教学课件
- 学前儿童发展 课件 第10章 学前儿童语言的发展
- 内蒙古根河市阿龙山中学2025届七年级数学第二学期期末达标检测模拟试题含解析
- 湖南省凤凰县联考2025年七下数学期末质量检测模拟试题含解析
- 2025+CSCO非小细胞肺癌诊疗指南解读课件
- 中学生学宪法班会课件
- 医院后勤考试试题及答案
- 县人民医院老住院楼装修改造项目可行性研究报告申请报告编写
- 肾内科健康科普护理
- 第1课 中华文明的起源与早期国家 课件 人教版必修上册中外历史纲要
- 互联网运营思维
- T∕CACM 1085-2018 中医治未病技术操作规范 调神益智针法预防血管性认知障碍
- 装修销售培训课件
- 暗挖开挖技术交底
- 数据治理架构试题及答案
评论
0/150
提交评论