C++网络作业6答案.doc_第1页
C++网络作业6答案.doc_第2页
C++网络作业6答案.doc_第3页
C++网络作业6答案.doc_第4页
C++网络作业6答案.doc_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

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

文档简介

作业4一、 选择题1 下列关于动态联编的描述中,错误的是_。D A)动态联编是以虚函数为基础的 B)动态联编是在运行时确定所调用的函数代码的 C)动态联编调用函数操作是指向对象的指针或对象引用D)动态联编是在编译时确定操作函数的注:先期联编也称静态联编,迟后联编也称动态联编。注释:动态联编一直要到程序运行时才能确定调用哪个函数。 虚函数是实现动态联编的必要条件之一。没有虚函数一定不能实现动态联编,但 有虚函数存在时,必须同时满足下列条件,才能够实现动态联编: 类之间满足子类型关系; 调用虚函数操作的是指向对象的指针或者对象引用:或者是由成员函数调用 虚函数。2 关于虚函数的描述中,正确的是_。D A)虚函数是一个静态成员函数 B)虚函数是一个非成员函数 C)虚函数既可以在函数说明时定义,也可以在函数实现时定义D)派生类的虚函数与基类中对应的虚函数具有相同的参数个数和类型注释:虚函数是非静态的成员函数。它不能是友元函数,但可以在另一个类中被声明为友元函数。 虚函数声明只能出现在类定义的函数原型声明中,而不能在成员函数的函数体实现的时候。 派生类的虚函数与基类中对应的虚函数必须满足下列条件,否则派生类中的虚函数将丢失其虚特性,在调用时进行静态联编: 派生类中的虚函数与基类中的虚函数具有相同的名称: 派生类中的虚函数与基类中的虚函数具有相同的参数个数和相同的对应参数 类型: 派生类中的虚函数与基类中的虚函数的返回值或者相同,或者都返回指针或 引用,并且派生类虚函数所返回的指针或引用的基类型是基类中的虚函数所 返回的指针或引用的基类型的子类型。3 在下面四个选项中,_是用来声明虚函数的。A A)virtual B)public C)using D)false注释:说明虚函数的一般格式如下: virtua1()4 对虚函数的调用_。D A)一定使用动态联编 B)必须使用动态联编C)一定使用静态联编 D)不一定使用动态联编注释:参见第1题的注释。5 实现运行时的多态性要使用_。D A)重载函数 B)构造函数 C)析构函数 D)虚函数注释:参见第1题的注释。6 要实现动态联编,必须通过_调用虚函数。A A)对象指针 B)成员名限定 C)对象名 D)派生类名注释:参见第1题的注释。7 在派生类中重新定义虚函数时,除了_方面,其他方面都必须与基类中相应的 虚函数保持一致。D A)参数个数 B)参数类型 C)函数名称 D)函数体注释:参见第2题的注释。8 下面关于构造函数和析构函数的描述,错误的是_。D A)析构函数中调用虚函数采用静态联编B)对虚析构函数的调用可以采用动态联编C)当基类的析构函数是虚函数时,其派生类的析构函数也一定是虚函数D)构造函数可以声明为虚函数注释:构造函数不能声明为虚函数,但析构函数可以声明为虚函数。当基类的析构函数声明为虚函数时,无论派生类是否使用virtual关键字说明,派生类的析构函数一定是虚函数,对缺省析构函数亦然。而且,如果满足动态联编的其他条件,对虚析构函数的调用将采用动态联编。 构造函数不能声明为虚函数,但在构造函数中可以调用虚函数。在构造函数或析构函数中调用虚函数,将采用静态联编。9 关于纯虚函数和抽象类的描述中,错误的是_。C A)纯虚函数是一种特殊的虚函数,它没有具体的实现B)抽象类是指具有纯虚函数的类C)一个基类中说明有纯虚函数,该基类的派生类一定不再是抽象类D)抽象类只能作为基类来使用,其纯虚函数的实现由派生类给出注释:带有纯虚函数的类称为抽象类。抽象类中的纯虚函数的实现由派生类给出: 但派生类仍可不给出纯虚函数的定义,继续作为抽象类存在。10 下列描述中,_是抽象类的特性。D A)可以说明虚函数 B)可以进行构造函数重载C)可以定义友元函数 D)不能说明其对象注释:抽象类区别于其他类的最根本的特征是不能定义对象。11 _是一个在基类中说明的虚函数,它在该基类中没有定义,但要求任何派生 类都必须定义自己的版本。 C A)虚析构函数 B)虚构造函数 C)纯虚函数 D)静态成员函数12 如果一个类至少有一个纯虚函数,那么就称该类为_。A A)抽象类 B)虚基类 C)派生类 D)以上都不对13 以下_成员函数表示纯虚函数。C A)virtual int vf(int); B)void vf(int)=0;C)virtual void vf()=0; D)virtual void vf(int)()注释:纯虚函数的声明格式如下: virtual()=0; 注意纯虚函数与虚函数体为空的区别。纯虚函数根本就没有函数体,而空的虚 函数的函数体为空:前者所在的类是抽象类,不能直接进行实例化,而后者所在的 类是可以实例化的:14 下面的描述中,正确的是_。A A)virtual可以用来声明虚函数B)含有纯虚函数的类是不可以用来创建对象的,因为它是虚基类C)即使基类的构造函数没有参数,派生类也必须建立构造函数D)静态数据成员可以通过成员初始化列表来初始化注释:virtual关键字既可以用来声明虚基类,也可以用来声明虚函数。 含有纯虚函数的类是抽象类,它不能用来定义对象。 静态数据成员的初始化必须在类体外进行。 如果所有的基类和子对象构造函数都不需要参数,派生类也不需要参数时,派生类构造函数可以不定义。15 在下面程序中,A、B、C、D四句编译时不会出现错误的是_。A #includeclass Base public: Base() Base(int c): count(c) virtual void print() const=0;private: int count;class Derived: public Basepublic: Derived() :Base(0 ) Derived(int c): Base(c) void print()const coutDerivedendl; ;void main() Derived d(10); Base *pb; pb=&d; /A Base & cb=d; Derived dd=*pb; Derived &cd=cb; /C Base bb=d; /D注释:B和C处不符合赋值兼容规则。D处出错是因为Base是含有纯虚函数的抽象类,不能建立抽象类的对象。 16 在下面程序中,A,B、C、D四句编译时出现错误的是_。C class A /A public: /B A( ) func(); /C virtual void func()=0; /D;注释:构造函数中不能调用纯虚函数,但可以调用虚函数。17 分析下面的程序,正确的输出结果是_B#include #include class Basepublic: virtual char *fun() const=0;char* Base:fun() const return “Base”; class Derivedl1: virtual public Basepublic:char* fun() const return Derived11; ;class Derivedl2: virtual public Basepublic: char* fun() const return Derivedl2; ;class Derived2: public Derivedl1,public Derivedl2public: char* fun() const char *ptr; ptr=new charstrlen(Derivedl1:fun()+strlen(Derivedl2:fun()+l; strcpy(ptr,Derived11:fun(), strcat(ptr,Derived12:fun(); return ptr; ;void main() Base *pb; pb=new Derived11; coutfun()endl; pb=new Derivedl2; coutfun()endl; pb=new Derived2; coutfun()endl;A) B)Base Derivedl1Base Derivedl2Base Derivedl1Derivedl2C) D)Derivedl1 Derivedl2Derivedl1 Derivedl2Derivedl1Derivedl2 Derivedl1Derivedl2注释:参见第1题、第2题的注释。派生类Derivedll、Derivedl2和Derived2中的函数fun与基类Base中的虚函数fun的函数名、参数表和返回类型都完全相同,因此这三个类中的fun函数也是虚函数,对fun函数的调用采用动态联编。二、填空题1 动态联编中直到_程序运行_时才能确定调用哪个函数;而静态联编则是在_程序编译时进行的。 注释:动态联编与静态联编的概念。2 静态联编所支持的多态性称为_编译时的_多态性,动态联编所支持的多态性则称为 _运行时的_多态性,动态多态性由_虚函数_来支持。 注释:在C+中,多态性的实现和联编有关。3 对虚函数使用对象指针或引用调用,系统使用_动态_联编;使用对象调用时系统使用_静态_联编。注释:参见选择填空第1题、第5题的注释。4 动态联编是在_虚函数_的支持下实现的,它通过_指针或引用来调用该函数操作。5 在一个成员函数内调用一个虚函数时,对该虚函数的调用进行_动态_联编。6 在析构函数中调用虚函数时,采用_静态_联编。7 C+中_不支持_虚构造函数,但_支持_虚析构函数。 注释:参见选择填空第8题的注释。 多态是不同的对象对同一消息有不同的行为特征,虚函数作为运行过程中多态的基础,主要是针对对象的,而构造函数的调用意味着要建立一个对象,这时必须确切地知道这个对象的类型,并且,我们也不会为一个已存在的对象调用构造函数。因此,虚构造函数没有意义。 在C+中可以声明虚析构函数。析构函数的功能是在该类对象消亡之前进行一些必要的清理工作,如果一个类的析构函数是虚函数,那么,由它派生而来的所有子类的析构函数也是虚函数。析构函数设置为虚函数后,在使用指针调用时可以进行动态联编,实现运行时的多态,从而保证使用基类的指针就能够调用适当的析构函数针对不同的对象进行清理工作。8 在类定义中,将_=0_置于虚函数的函数原型的末尾可以将该函数声明为纯虚函数9 带有_纯虚函数_的类称为抽象类,它只能作为_基类_来使用。 注释:抽象类的作用主要有两个:直接用作基类或用作指针或引用的基类型。10 抽象类不能_定义对象_,但可以_声明抽象类的指针或引用_作为参数类型,函数返回类型或显式转换类型。 注释:注意抽象类不能定义对象是指不能定义需要分配存储空间的对象。因此可以声明抽象类的指针或引用,它们在程序运行时可以指向并访问派生类对象。11 下列程序的运行结果如下: Derivels Print() called.Derive2s Print() called.根据结果将程序补充完整。#include class Base public: Base(int i) b=i; _virtual void Print()=0;_protected: int b;class Derivel :public Basepublic:_Derive1( int i ):Base( i ) _ void Print() cout”Derive1s Print() called.”endl; ;class Derive2:public Base _public:Derive1(int i ): Base( i ) void Print( ) cout”Derive2s Print( ) called. “Print();void main() _Derive1 *d1=new Derive1(1);_ Derive2 *d2=new Derive2(2); fun(dl); fun(d2); 注释:派生类Derived1和Derived2从基类Base公有继承,它们是Base的子类型。 主程序中两次调用fun函数,该函数通过指针对象obj调用了Print函数,得到 了不同的输出结果。而同样的消息被不同类型的对象接收时导致完全不同的行为, 恰好体现了面向对象的多态特性。根据运行时的多态必须满足的条件,Print函数一 定是一个虚函数,并且在所有类中都必须进行定义。由于Base类中的Print函数除 了提供一个公共的接口外,没有其他的作用,所以最好定义为纯虚函数。 12 将下列程序补充完整。#include class convertpublic: convert(double i) vail=i; _virtual void compute( )=0;_protected: double val1; double val2;/liters to gallonsclass l_to_g:public convertpublic: _l_to_g(double i ) : convert(i) _ void compute() val2=val1/3.7854; coutval1 liters is val2 gallons.endl;/Fahrenheit to Celsiusclass f_to_c:public convert _public: f_to_c(double i ):convert( i ) void compute( ) val2=(val1-32)*5/9;coutval1”Fahrenheit is “val2”Celsius.”endl; ;void fun(_convert& f _) pute();void main() l_to_g lgobj(4); f_to_c fcobj(70); fun(lgobj); fun(fcobj);13 根据不同的输出结果,在函数Tone中填入正确的语句。#include class Instrumentpublic: virtual void Print() const coutInstrument:Printendl; ;class Piano: public Instrumentpublic: void Print()const coutPiano:Printendl; ;class Guitar: public Instrumentpublic: void Print() const coutGuitar:Printendl; ;void Tone(_) _void main()Guitar g; Tone(g); Piano p; Tone(p);(1)输出结果为:Instrument: :PrintInstmment:Print(2)输出结果为:Guitar: :PrintPiano: :Print(1) Instrument obj obj.Print( ) (2) Instrument& obj obj.Print( ) 参考第3题,第一次的输出是由静态联编产生的,第二次的输出是由动态态联编产生的。14 下列程序的运行结果如下:Bases cons.Deriveds cons.Deriveds des.Bases des.根据结果将程序补充完整。#include class Basepublic: Base() coutBases cons.endl; _virtual Base( )_ coutBases des.endl; ;class Derived:public Basepublic: Derived() coutDeriveds cons.endl; Derived() coutDeriveds des.endl; ;void main() 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) : x_(p.x_), y_(p.y_), z_(p.z_) /形参point为常引用,它所引用的对象不能被更新,即传给它的实参不能被更新 。 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_ & 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& 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) return 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() Point 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 Point& 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 -(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类,二者都有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 : 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);/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 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.14*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);private: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 Rectangle 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 D11 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;Derive1( int i ):Base( i ) public:Derive1(int i ): Base( i ) void Print( ) cout”Derive2s Print( ) called. “ endl; Base *objDerive1 *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 ) void compute( ) val2=(val1-32)*5/9;coutval1”Fahrenheit is “val2”Celsius.”endl; convert& f 13 (1) Instrument obj obj.Print( ) (2) Instrument& obj obj.Print( )14virtual Base( )new Derived 三、编程1,#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) : x_(p.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_ & 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& 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) return 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() Point 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,#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 temp(*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.print(); +(+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 : p

温馨提示

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

评论

0/150

提交评论