版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、程序设计(C+语言)期中考试试题班级:_ 学号:_ 姓名:_一、简答题(每小题4分,共20分)1. 在C+中声明类时,如何做到多个实例共享一个全局变量?2. 引用和指针之间有什么区别?3. 什么是抽象类?析构函数可以声时为虚函数吗?如果可以,在什么情况下使用?4. 什么是多态性?多态性是如何实现的? 5. 构造函数与析构函数的功能是什么? 在继承层次上,构造函数和析构函数的调用顺序如何? 二、程序改错题 (每小题5分,共20分)1. 下面的程序是否有错误,如果有错,请说明原因并改正。# include int * FuncOne() int * pint = new int(5); count
2、 the value of pInt in FuncOne is:* pintendl; return pint; int main() int * pint = FuncOne(); cout the value of pInt back in main is: * pint endl return 0; 2. 下面的程序是否有错误,如果有错,请说明原因并改正。struct A1 int i; ; class A2 int i; ; int main() A1 a1; a1.i = 0; A2 a2; a2.i = 0; 3. 下面的程序是否有错误,如果有错,请说明原因并改正。int mai
3、n() char szTest = hello; const char* psz = szTest; psz0 = b; 4. 下面的程序是否有错误,如果有错,请说明原因并改正。class Shape() public: Shape(); virtual Shape(); virtual Shape(const Shape&); 三、程序阅读题(每小题5分,共20分)1. 分析下面的程序,并写出运行结果。class A public: virtual void func() cout I am in base endl; ;class B : public A public: virtual
4、void func() cout I am in derived func(); A* aa = bb; aa-func(); 以上程序的输出结果是 。2. 分析下面的程序,并写出运行结果。class Sample public:int v;Sample() ;Sample(int n):v(n) ;Sample( Sample & x) v = 2 + x.v ; ;Sample PrintAndDouble( Sample o) cout o.v endl; o.v = 2 * o.v; return o;int main() Sample a(5);Sample b = a;cout b
5、.v endl;Sample c = PrintAndDouble( b );cout c.v endl;Sample d;d = a;cout d.v endl;以上程序的输出结果是 。3. 分析下面的程序,并写出运行结果。class A public:int val;A(int = 0) val = n; ; A& GetObj() return *this; ;main() A a; cout a.val endl;a.GetObj() = 5;cout a.val endl;以上程序的输出结果是 。4. 分析下面的程序,并写出运行结果。class B private: int nBVa
6、l;public: B ( int n ) nBVal = n; void Print() cout nBVal= nBVal endl; ;class D: public B private : int nDVal;public: D( int n) : B(3*n) nDVal = n;void Print() B:Print(); cout nDVal=nDValPrint(); D d(4); d.Print (); B * p = &d; p-Print(); delete pb;以上程序的输出结果是 。四、程序填空题(每小题10分,共20分)1. 填空使程序能编译通过,并写出运行的
7、输出结果。class MyString private:char * p;public:MyString( char * s ) p = new charstrlen(s)+1;strcpy(p,s);MyString( MyString & o ) strcpy( p, o.p);MyString() delete p; void Copy( char * s) p = new charstrlen(s)+1;strcpy(p,s);const char * c_str() ;main() MyString s1(This), s2 =s1;s2.Copy ( Hello);cout s1.
8、c_str () endl s2.c_str () ;该程序输出结果为: 。2. 填空使程序能编译通过,并写出运行的输出结果。#include template class myclass T i;public: myclass (T a) i = a; void show( ) cout i endl; ;void main() myclass obj(This); obj.show();该程序输出结果为: 。五、程序设计题(共20分)设有如下定义的几个类,其中,Graphic是个抽象类,它定义了平面封闭图形应该具有的运算求面积getArea,它可以有任意多子类,如Circle和Rectan
9、gle便是它的两个子类。GraphicContainer是一个包含Graphic对象的类,该类有两个数据成员,其中:m_buffer是个数组,用于存放不同的Graphic对象;m_sum用来表示该数组中实际存放元素的个数,即Graphic对象的总数。现在要求你完成该类的求所有Graphic对象总面积函数getAllArea的实现代码。为了完成该函数,允许你在其它相关类中增加方法及其实现。程序设计(C+语言)期中考试参考答案一、简答题(每小题5分,共20分)6. 在C+中声明类时,如何做到多个实例共享一个全局变量?声明一个类静态成员变量。 7. 引用和指针之间有什么区别?引用是一个别名,而指针是
10、一个保存地址的变量。8. 什么是抽象类?析构函数可以声时为虚函数吗?如果可以,在什么情况下使用?如果一个类中包括纯虚函数,则该类为抽象类,抽象类不能实例化,主要是作为接口定义。一般情况下类的析构函数都定义成虚函数,主要是考虑在使用基类指针操作派生类对象时保证类的析构顺序。9. 什么是多态性?多态性是如何实现的? 函数多态性是指用多个含义重载一个函数的能力,即允许创建多个名称相同的函数。可通过改变同名函数变元的类型或个数来实现。10. 构造函数与析构函数的功能是什么? 在继承层次上,构造函数和析构函数的调用顺序如何? 构造函数用来初始化。析构函数用来做清除工作,一般包括内存释放。在继承层次上,构
11、造函数和析构函数的调用顺序为:构造函数是先基类,后派生类;析构函数是先派生类,后基类。二、程序改错题 (每小题5分,共20分)5. 下面的程序是否有错误,如果有错,请说明原因并改正。# include int * FuncOne() int * pint = new int(5); count the value of pInt in FuncOne is:* pintendl; return pint; int main() int * pint = FuncOne(); cout the value of pInt back in main is: * pint endl return 0
12、; 有错误,内存泄漏。# include int FuncOne() int * pint = new int(5); cout the value of pInt in FuncOne is: * pintendl; int temp = *pint; delete pint; return temp; int main() int theint = FuncOne(); cout ”the value of pInt back in main is:”theint endl; return 0; 6. 下面的程序是否有错误,如果有错,请说明原因并改正。struct A1 int i; ;
13、class A2 int i; ; int main() A1 a1; a1.i = 0; A2 a2; a2.i = 0; 有错误,类定义中未显示权限定义符缺省为private。 struct A1 int i; ; class A2 public:int i; ; int main() A1 a1; a1.i = 0; A2 a2; a2.i = 0; 7. 下面的程序是否有错误,如果有错,请说明原因并改正。int main() char szTest = hello; const char* psz = szTest; psz0 = b; 有错误,psz是一字符串指针,该指针指向的内容是
14、常量,指针指向的内容不能被修改。int main() char szTest = hello; char* const psz = szTest; psz0 = b; 8. 下面的程序是否有错误,如果有错,请说明原因并改正。class Shape() public: Shape(); virtual Shape(); virtual Shape(const Shape&); 有错误,不能声明一个拷贝构造函数为虚拟函数。 class Shape() public: Shape(); virtual Shape(); Shape(const Shape&); 三、程序阅读题(每小题5分,共25分)
15、5. 分析下面的程序,并写出运行结果。class A public: virtual void func() cout I am in base endl; ;class B : public A public: virtual void func() cout I am in derived func(); A* aa = bb; aa-func(); 以上程序的输出结果是:I am in derived I am in derived 6. 分析下面的程序,并写出运行结果。class Sample public:int v;Sample() ;Sample(int n):v(n) ;Sam
16、ple( Sample & x) v = 2 + x.v ; ;Sample PrintAndDouble( Sample o) cout o.v endl; o.v = 2 * o.v; return o;int main() Sample a(5);Sample b = a;cout b.v endl;Sample c = PrintAndDouble( b );cout c.v endl;Sample d;d = a;cout d.v endl;以上程序的输出结果是:79205 7. 分析下面的程序,并写出运行结果。class A public:int val;A(int n = 0)
17、val = n; ; A& GetObj() return *this; ;main() A a; cout a.val endl;a.GetObj() = 5;cout a.val endl;以上程序的输出结果是:05 8. 分析下面的程序,并写出运行结果。class B private: int nBVal;public: B ( int n ) nBVal = n; void Print() cout nBVal= nBVal endl; ;class D: public B private : int nDVal;public: D( int n) : B(3*n) nDVal = n
18、;void Print() B:Print(); cout nDVal=nDValPrint(); D d(4); d.Print (); B * p = &d; p-Print(); delete pb;以上程序的输出结果是:nBVal=2nBVal=12nDVal=4nBVal=12 四、程序填空题(每1小题10分,共20分)3. 填空使程序能编译通过,并写出运行的输出结果。class MyString private:char * p;public:MyString( char * s ) p = new charstrlen(s)+1;strcpy(p,s);MyString( MyS
19、tring & o ) p = new charstrlen(o.p ) + 1 ;strcpy( p, o.p);MyString() delete p; void Copy( char * s) if (p!=NULL) delete p;p = new charstrlen(s)+1;strcpy(p,s);const char * c_str() return p;main() MyString s1(This), s2 =s1;s2.Copy ( Hello);cout s1.c_str () endl s2.c_str () ;该程序输出结果为:ThisHello 4. 填空使程序
20、能编译通过,并写出运行的输出结果。#include template class myclass T i;public: myclass (T a) i = a; void show( ) cout i endl; ;void main() myclass obj(This); obj.show();该程序输出结果为:This 五、程序设计题(共20分)设有如下定义的几个类,其中,Graphic是个抽象类,它定义了平面封闭图形应该具有的运算求面积getArea,它可以有任意多子类,如Circle和Rectangle便是它的两个子类。GraphicContainer是一个包含Graphic对象的
21、类,该类有两个数据成员,其中:m_buffer是个数组,用于存放不同的Graphic对象;m_sum用来表示该数组中实际存放元素的个数,即Graphic对象的总数。现在要求你完成该类的求所有Graphic对象总面积函数getAllArea的实现代码。为了完成该函数,允许你在其它相关类中增加方法及其实现。#includeclass Graphic public: virtual double getArea()=0;class Triangle: public Graphic protected:double height, width;public:Triangle(double h, dou
22、ble w) height=h; width=w; double getArea() return height*width*0.5; ;class Rectangle: public Graphic protected:double height, width; public: Rectangle(double h, double w) height=h; width=w; double getArea() return height*width; ;class Circle: public Graphic protected: double redius; public: Circle(double r) redius=r; double getArea() return redius*redius*3.14; ;class GraphicContainer private:Graphic *m_buffer; int m_sum; public: GraphicContainer(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 建筑防腐雨季施工应对方案
- 建筑保温与结构一体化系统施工工艺详解
- 家用储能设备销售培训课件
- 2026三下数学复式统计表核心素养课件
- 废弃资源综合利用项目选址论证报告
- 工业噪声治理改造项目环境影响报告
- 电梯配件生产质量质保手册
- 辐射实验室专项安全课件
- 电梯安装验收标准
- 承压类特种设备安全附件标准规范应用技术手册
- 2026年有限空间试题和答案
- 2026年党员党史知识竞赛试题(附答案)
- 2026年安徽省中考英语试题(含答案)
- 2026河北省新高一入学摸底测试全科高频考点与模拟训练
- 2026河北石家庄行唐县住房和城乡建设局公开招聘协管员95名考试参考题库及答案详解
- 公考必考成语1000个
- 苏科版(2024)八年级下册物理期末复习重要知识点考点提纲
- 监所艾滋病防治管理办法
- 方剂学选择模考试题(附参考答案)
- HW系列变速箱拆装培训
- 激光切割机日常保养表
评论
0/150
提交评论