程序设计C试题和答案.doc

上传人:wux****ua 文档编号:9131695 上传时间:2020-04-03 格式:DOC 页数:7 大小:93.50KB
返回 下载 相关 举报
程序设计C试题和答案.doc_第1页
第1页 / 共7页
程序设计C试题和答案.doc_第2页
第2页 / 共7页
程序设计C试题和答案.doc_第3页
第3页 / 共7页
点击查看更多>>
资源描述
程序设计(C+语言)期中考试试题班级:_ 学号:_ 姓名:_7一、简答题(每小题4分,共20分)1. 在C+中声明类时,如何做到多个实例共享一个全局变量?2. 引用和指针之间有什么区别?3. 什么是抽象类?析构函数可以声时为虚函数吗?如果可以,在什么情况下使用?4. 什么是多态性?多态性是如何实现的? 5. 构造函数与析构函数的功能是什么? 在继承层次上,构造函数和析构函数的调用顺序如何? 二、程序改错题 (每小题5分,共20分)1. 下面的程序是否有错误,如果有错,请说明原因并改正。# 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; 2. 下面的程序是否有错误,如果有错,请说明原因并改正。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&); 三、程序阅读题(每小题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 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.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 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=nDValPrint(); 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 & 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. 填空使程序能编译通过,并写出运行的输出结果。#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和Rectangle便是它的两个子类。GraphicContainer是一个包含Graphic对象的类,该类有两个数据成员,其中:m_buffer是个数组,用于存放不同的Graphic对象;m_sum用来表示该数组中实际存放元素的个数,即Graphic对象的总数。现在要求你完成该类的求所有Graphic对象总面积函数getAllArea的实现代码。为了完成该函数,允许你在其它相关类中增加方法及其实现。程序设计(C+语言)期中考试参考答案一、简答题(每小题5分,共20分)6. 在C+中声明类时,如何做到多个实例共享一个全局变量?声明一个类静态成员变量。 7. 引用和指针之间有什么区别?引用是一个别名,而指针是一个保存地址的变量。8. 什么是抽象类?析构函数可以声时为虚函数吗?如果可以,在什么情况下使用?如果一个类中包括纯虚函数,则该类为抽象类,抽象类不能实例化,主要是作为接口定义。一般情况下类的析构函数都定义成虚函数,主要是考虑在使用基类指针操作派生类对象时保证类的析构顺序。9. 什么是多态性?多态性是如何实现的? 函数多态性是指用多个含义重载一个函数的能力,即允许创建多个名称相同的函数。可通过改变同名函数变元的类型或个数来实现。10. 构造函数与析构函数的功能是什么? 在继承层次上,构造函数和析构函数的调用顺序如何? 构造函数用来初始化。析构函数用来做清除工作,一般包括内存释放。在继承层次上,构造函数和析构函数的调用顺序为:构造函数是先基类,后派生类;析构函数是先派生类,后基类。二、程序改错题 (每小题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; 有错误,内存泄漏。# 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; ; 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是一字符串指针,该指针指向的内容是常量,指针指向的内容不能被修改。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分)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) ;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 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 ( 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;以上程序的输出结果是: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 ;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. 填空使程序能编译通过,并写出运行的输出结果。#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对象的类,该类有两个数据成员,其中: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, 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; 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(int sum)m_sum=sum;m_buffer=new Graphic *m_sum;char select;double height, width, redius;for(int i=0; iselect;swith(select) case T: cout”Please input the height and width of the triangle:”heightwidth;m_bufferi=new Triangle(height, width);break;case R:cout”Please input the height and width of the rectangle:”heightwidth;m_bufferi=new Rectangle(height, width);break;case C:cout”Please input the redius of the circle:”redius;m_bufferi=new Circle(redius);break;default: cout”Please input the correct graphic!”endl; i-; GraphicContainer() for(int i=0; im_sum; i+) delete m_bufferi; delete m_buffer; double getAllArea() double sumArea=0; for(int i=0; igetArea(); return sumArea;void main() GraphicContainer GC(4);cout“All Areas sum = “GC.getAllArea()endl;
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 图纸专区 > 考试试卷


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!