计算机学院2004年03级C++面向对象程序设计试题(A卷).pdf

上传人:灯火****19 文档编号:137988093 上传时间:2020-07-13 格式:PDF 页数:8 大小:206.95KB
返回 下载 相关 举报
计算机学院2004年03级C++面向对象程序设计试题(A卷).pdf_第1页
第1页 / 共8页
计算机学院2004年03级C++面向对象程序设计试题(A卷).pdf_第2页
第2页 / 共8页
计算机学院2004年03级C++面向对象程序设计试题(A卷).pdf_第3页
第3页 / 共8页
计算机学院2004年03级C++面向对象程序设计试题(A卷).pdf_第4页
第4页 / 共8页
计算机学院2004年03级C++面向对象程序设计试题(A卷).pdf_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《计算机学院2004年03级C++面向对象程序设计试题(A卷).pdf》由会员分享,可在线阅读,更多相关《计算机学院2004年03级C++面向对象程序设计试题(A卷).pdf(8页珍藏版)》请在金锄头文库上搜索。

1、C+面向对象程序设计试题 (A卷) (满分:100分 时间:150分钟 日期:2005.1.10) 1、 (共20分,每题2分)单项选择 1. 已定义了一个类A并有语句 A* pa=new A(5); 那么以下说法 正确的是: A. 该语句会创建A类的一个对象,并将此对象的地址赋给指针 pa; B. 该语句会创建A类的5个对象,且它们的地址是连续的; C. 当指针变量pa超出其作用域时,pa和为对象分配的空间都 将被自动释放; D. 类A中一定有一个显式定义的构造函数和一个显式定义的 虚析构函数; 2. 定义类A的非静态成员函数A则exp不能是: A.类A中类型为A的静态数据成员 B. f中用

2、语句 A a = one; 定义的量a C. one D. *this 3. 在一个类中,下列哪些成员可以是多个? A. 无参构造函数 B. 析构函数 C. 原型相同的非静态成员函数 D. 静态成员函数 4. 对类T中的下列成员,若不考虑代码优化,一般来说它们中 的哪个执行起来最慢? A. 缺省的无参构造函数 B. 缺省的拷贝构造函数 C. T f( ) T t; return t; D. 不能判定A、B、C哪个 最慢 5. 对函数原型为int的全局函数,下列语句 块哪个是错误的: A. int x=10,y=5; min(x,y)=100; B. int x=10,y=5, z; z=min

3、(x,y+=x); C. int x=10,y=5; min(x,10)=y; D. int x=10,y=5; x=min(x,y); 6. 关于拷贝构造函数错误的说法有: A. 拷贝构造函数是一种特殊的构造函数,且不能在类中进 行函数重载。 B. 若自定义派生类的拷贝构造函数,那么也必须自定义基类 的拷贝构造函数。 C. 拷贝构造函数只能有一个参数,且为常量引用或非常量引 用。 D. 拷贝构造函数不允许在函数体中使用return语句。 7. 下列对虚基类声明正确的是: A. class virtual B: public A B. class B: virtual public A C.

4、class B: public A virtual D. virtual class B: public A 8. 若有语句A*const p=new B;,其中类B是从类A直接派生得到 的,那么下列说法错误的是: A. 若要执行语句 p-f( );那么类A中一定有一个与f( )函数匹 配的函数声明。 B. 类B一定是通过public继承方式从类A派生的。 C. 类B的构造函数中,至少有一个无参的或提供全部缺省参 数的构造函数。 D. 通过指针p,只能访问类中的常量成员函数或静态成员函 数。 9. 下面说明的哪个数据不能作为类T的数据成员定义。 A. T* pT B. class Q q1,q

5、2; C. static T t; D. const T t; 10. 下列哪种运算符可以被重载: A. sizeof B. : C. ? : D. 2、 (共15分,每题3分)回答下列各题,可以举例说明。 1. 说明基类中的public、 protected和 private成员分别在public、 protected和private继承方式下在派生类中的可访问性。 2. 若类A有私有数据成员int n,哪些情况下,在一个函数的函 数体中能够直接访问A类的数据成员n。(至少三种) 3. 哪些情况下,定义构造函数时必须使用初始化列表。(至少 三种) 4. 将一个基类的析构函数定义为虚函数的作用

6、和目的是什么? 5. 为什么在类中不能用成员函数void f(int. 8. 类的多个实例对象可共享本类的虚拟表。 9. 用const修饰的成员函数只能通过常量对象调用。 10. 类B是类A的派生类,类C是类A的友员类,那么类C也是类B 的友员类。 4、 (25分,每小题5分) 指明下列各程序中存在的不符合C+语 言规范之处(每题一处),说明你的理由或依据。解答方式 可参考下面例子。 例子程序: #include class A protected: void f( ) cout A:f( ) endl; ; void main( ) A a; a.f( ); 对左边例子程序可以解答如 下:

7、成员函数f被说明成 protected,这与main函数中对它 的使用:a.f( )语句不符。理由 是:一个类中protected访问权限 下的成员除了可用于实现该 类、实现该类的派生类或后裔 类、实现友员外,不能被使 用。 (1) #include class Complex public: Complex(float r=0.0,float i=0.0) : rPart(r), iPart(i) private: float rPart; float iPart; ; void main( ) Complex c1; Complex c2(3.14); Complex c3(c2); c1

8、 = c2; cout c1 c2 c3 endl; (2)(3) #include class B; class A public: A(int anInt=0):i(anInt) A(const B int getI( ) const return i; private: int i; ; class B public: B(int int1=0, int int2=0): i(int1),j(int2) int getI( ) const return i; int getJ( ) const return j; operator A( ) int k = (i aB.getJ( ) i

9、 = aB.getI( ); else i = aB.getJ( ); void main( ) B b(10,20); A a(b); cout a.getI( ) endl; class A public: A* f( ) delete pA; pA = new A; return pA; A( ) delete pA; static A* g( ) return this; private: int i; static A* pA; ; A* A:pA = 0; void main( ) A a; A* p = a.f( ); A* q = a.g( ); (4) #include cl

10、ass B public: B(int int1,int int2) : j(int2), i(int1) virtual void f( ) const cout B:f( ) endl; private: int i; int j; ; class D: public B public: D(int anInt) : i(anInt) void f( ) const cout D:f( ) endl; private: int i; ; void main( ) D d; d.f( ); (5) #include class A public: A(int anInt=0): a(anIn

11、t) void f( ) cout A:f( ) 0) a.f( ); else cout A:g( ) endl; private: int i; A a; ; void main( ) A a1(100); a1.g( ); A a2(-10); a2.g( ); 5、 (5分)写出下面程序的运行结果 #include class A public: A( ) cout A:A( ) endl; virtual A( ) cout A:A( ) endl; virtual void g( ) cout A:g( ) void main( ) B b; b.k( ); endl; void

12、h( ) cout A:h( ) endl; virtual void f( ) g( ); h( ); ; class B:public A public: B( ) cout B:B( ) endl; virtual B( ) cout B:B( ) endl; virtual void g( ) cout B:g( ) endl; void h( ) cout B:h( ) endl; virtual void k( ) f( ); g( ); h( ); ; 6、 (5分)写出下面程序的运行结果 #include class A public: A( ) cout1endl; virt

13、ual A( ) cout2endl; virtual int Add(int n)return 0; ; class C:public A public: C(A virtual C( ) cout4endl; virtual int Add(int n) return a.Add(n+num); private: A int num; ; class B:public A public: B(int n):num(n) virtual B( ) virtual int Add(int n) num+=n;return num; private: int num; ; void main (

14、 ) B b(100); C c1(b,1), c2(c1,2); coutc2.Add(50)endl; 7、 (共10分)某程序中关于类A和类B的部分定义如下: #include clas class A public: A(int num):n(num) void Show( ) const coutnShow( ); array1-Show( ); private: A* array2; ; 1) (5分)定义并实现类B的赋值函数,使得类B对象间能够进行深 赋值。 2) (5分)定义并实现类B的完成深拷贝的拷贝构造函数。 8、 (共10分)小王编写一个程序时,定义了类B和全局函数f, 部分代码如下: class B public: void f( B B(int n):data(n) int Data( ) const return data; void g1( ); void g2( ); void g3( ); private: const int data; ; if(condition =1) b.g1( ); else if(condition =5) b.g2( ); else if(condition = 9) b.g3( ); 当把此程序交给用户试用时,针对函数f,用户提出了一项 新的要求:当condition为100时,依次执行b的成员函数g1(

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 学术论文 > 管理论文

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