程序设计C试题和答案_第1页
程序设计C试题和答案_第2页
程序设计C试题和答案_第3页
程序设计C试题和答案_第4页
程序设计C试题和答案_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

1、文档供参考,可复制、编制,期待您的好评与关注! 程序设计(C+语言)期中考试试题班级:_ 学号:_ 姓名:_7 / 7一、简答题(每小题4分,共20分)1. 在C+中声明类时,如何做到多个实例共享一个全局变量?2. 引用和指针之间有什么区别?3. 什么是抽象类?析构函数可以声时为虚函数吗?如果可以,在什么情况下使用?4. 什么是多态性?多态性是如何实现的? 5. 构造函数与析构函数的功能是什么? 在继承层次上,构造函数和析构函数的调用顺序如何? 二、程序改错题 (每小题5分,共20分)1. 下面的程序是否有错误,如果有错,请说明原因并改正。# include <iostream.h>

2、; int * FuncOne() int * pint = new int(5); count <<"the value of pInt in FuncOne is:"<<* pint<<endl; return pint; int main() int * pint = FuncOne(); cout <<"the value of pInt back in main is:"<< * pint << endl return 0; 2. 下面的程序是否有错误,如果有错,请说明原因

3、并改正。struct A1 int i; ; class A2 int i; ; int main() A1 a1; a1.i = 0; A2 a2; a2.i = 0; 3. 下面的程序是否有错误,如果有错,请说明原因并改正。int main() char szTest = "hello" const char* psz = szTest; psz0 = b; 4. 下面的程序是否有错误,如果有错,请说明原因并改正。class Shape() public: Shape(); virtual Shape(); virtual Shape(const Shape&)

4、; 三、程序阅读题(每小题5分,共20分)1. 分析下面的程序,并写出运行结果。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" << endl; ;void main() B* bb = new B; bb->func(); A* aa = bb; aa->fun

5、c(); 以上程序的输出结果是 。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.v << endl;Sample c = P

6、rintAndDouble( 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;以

7、上程序的输出结果是 。4. 分析下面的程序,并写出运行结果。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;void Print() B:Print(); cout << "nDVal="<&

8、lt;nDVal<<endl; ;main() B * pb = new B(2); pb->Print(); D d(4); d.Print (); B * p = &d; p->Print(); delete pb;以上程序的输出结果是 。四、程序填空题(每小题10分,共20分)1. 填空使程序能编译通过,并写出运行的输出结果。class MyString private:char * p;public:MyString( char * s ) p = new charstrlen(s)+1;strcpy(p,s);MyString( MyString &a

9、mp; 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.c_str () << endl << s2.c_str () ;该程序输出结果为: 。2. 填空使程序能编译通过,并写出运行的输出结果。#inc

10、lude <iostream.h>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和Rectang

11、le便是它的两个子类。GraphicContainer是一个包含Graphic对象的类,该类有两个数据成员,其中:m_buffer是个数组,用于存放不同的Graphic对象;m_sum用来表示该数组中实际存放元素的个数,即Graphic对象的总数。现在要求你完成该类的求所有Graphic对象总面积函数getAllArea的实现代码。为了完成该函数,允许你在其它相关类中增加方法及其实现。程序设计(C+语言)期中考试参考答案一、简答题(每小题5分,共20分)6. 在C+中声明类时,如何做到多个实例共享一个全局变量?声明一个类静态成员变量。 7. 引用和指针之间有什么区别?引用是一个别名,而指针是一

12、个保存地址的变量。8. 什么是抽象类?析构函数可以声时为虚函数吗?如果可以,在什么情况下使用?如果一个类中包括纯虚函数,则该类为抽象类,抽象类不能实例化,主要是作为接口定义。一般情况下类的析构函数都定义成虚函数,主要是考虑在使用基类指针操作派生类对象时保证类的析构顺序。9. 什么是多态性?多态性是如何实现的? 函数多态性是指用多个含义重载一个函数的能力,即允许创建多个名称相同的函数。可通过改变同名函数变元的类型或个数来实现。10. 构造函数与析构函数的功能是什么? 在继承层次上,构造函数和析构函数的调用顺序如何? 构造函数用来初始化。析构函数用来做清除工作,一般包括内存释放。在继承层次上,构造

13、函数和析构函数的调用顺序为:构造函数是先基类,后派生类;析构函数是先派生类,后基类。二、程序改错题 (每小题5分,共20分)5. 下面的程序是否有错误,如果有错,请说明原因并改正。# include <iostream.h> int * FuncOne() int * pint = new int(5); count <<"the value of pInt in FuncOne is:"<<* pint<<endl; return pint; int main() int * pint = FuncOne(); cout &

14、lt;<"the value of pInt back in main is:"<< * pint << endl return 0; 有错误,内存泄漏。# include <iostream.h> int FuncOne() int * pint = new int(5); cout <<"the value of pInt in FuncOne is: "<<* pint<<endl; int temp = *pint; delete pint; return temp;

15、int main() int theint = FuncOne(); cout <<”the value of pInt back in main is:”<<theint << endl; return 0; 6. 下面的程序是否有错误,如果有错,请说明原因并改正。struct A1 int i; ; 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

16、 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是一字符串指针,该指针指向的内容是常量,指针指向的内容不能被修改。int main() char szTest = "hello" char* const psz = szTest; psz0 = b; 8. 下面的程序是否有错误,如果有错,请说明原

17、因并改正。class Shape() public: Shape(); virtual Shape(); virtual Shape(const Shape&); 有错误,不能声明一个拷贝构造函数为虚拟函数。 class Shape() public: Shape(); virtual Shape(); Shape(const Shape&); 三、程序阅读题(每小题5分,共25分)5. 分析下面的程序,并写出运行结果。class A public: virtual void func() cout << "I am in base" <&

18、lt; endl; ;class B : public A public: virtual void func() cout << "I am in derived" << endl; ;void main() B* bb = new B; bb->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

19、) ;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.v << endl;Sample c = PrintAndDouble( b );cout << c.v << endl;Sample d;d = a;cout << d.v <&

20、lt; endl;以上程序的输出结果是:79205  7. 分析下面的程序,并写出运行结果。class A public:int val;A(int n = 0) 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 (

21、 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="<<nDVal<<endl; ;main() B * pb = new B(2); pb->Print(); D d

22、(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( MyString & o ) p = new charstrlen(o.p ) + 1

23、;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 () ;该程序输出结果为:ThisHell

24、o 4. 填空使程序能编译通过,并写出运行的输出结果。#include <iostream.h>template < class T >class myclass T i;public: myclass (T a) i = a; void show( ) cout << i << endl; ;void main() myclass< char * > obj("This"); obj.show();该程序输出结果为:This 五、程序设计题(共20分)设有如下定义的几个类,其中,Graphic是个抽象类,它定义了

25、平面封闭图形应该具有的运算求面积getArea,它可以有任意多子类,如Circle和Rectangle便是它的两个子类。GraphicContainer是一个包含Graphic对象的类,该类有两个数据成员,其中:m_buffer是个数组,用于存放不同的Graphic对象;m_sum用来表示该数组中实际存放元素的个数,即Graphic对象的总数。现在要求你完成该类的求所有Graphic对象总面积函数getAllArea的实现代码。为了完成该函数,允许你在其它相关类中增加方法及其实现。#include<iostream.h>class Graphic public: virtual d

26、ouble getArea()=0;class Triangle: public Graphic protected:double height, width;public:Triangle(double h, double 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; d

27、ouble 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(int sum)m_sum=sum;m_buffer=new Graphic *m_sum;char select;double he

温馨提示

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

评论

0/150

提交评论