《程序设计c++试题和答案》由会员分享,可在线阅读,更多相关《程序设计c++试题和答案(7页珍藏版)》请在金锄头文库上搜索。
1、1程序设计(C+语言)期中考试试题班级:_ 学号:_ 姓名:_一、简答题(每小题 4 分,共 20 分)1. 在 C+中声明类时,如何做到多个实例共享一个全局变量?2. 引用和指针之间有什么区别?3. 什么是抽象类?析构函数可以声时为虚函数吗?如果可以,在什么情况下使用?4. 什么是多态性?多态性是如何实现的? 5. 构造函数与析构函数的功能是什么? 在继承层次上,构造函数和析构函数的调用顺序如何? 二、程序改错题 (每小题 5 分,共 20 分)1. 下面的程序是否有错误,如果有错,请说明原因并改正。# include int * FuncOne() int * pint = new int
2、(5); count func(); A* aa = bb; aa-func(); 以上程序的输出结果是 。2. 分析下面的程序,并写出运行结果。class Sample public:int v;Sample() ;Sample(int n):v(n) ;Sample( Sample ;Sample PrintAndDouble( Sample o) cout Print(); D d(4); d.Print ();B * p = delete pb;以上程序的输出结果是 。四、程序填空题(每小题 10 分,共 20 分)1. 填空使程序能编译通过,并写出运行的输出结果。class MySt
3、ring 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 template class myc
4、lass T i;public:myclass (T a) i = a; void show( ) cout obj(This); obj.show();该程序输出结果为: 。五、程序设计题(共 20 分)设有如下定义的几个类,其中,Graphic 是个抽象类,它定义了平面封闭图形应该具有的运算求面积 getArea,它可以有任意多子类,如 Circle 和Rectangle 便是它的两个子类。GraphicContainer是一个包含 Graphic 对象的类,该类有两个数据成员,其中:m_buffer 是个数组,用于存放不同的 Graphic 对象;m_sum 用来表示该数组中实际存放元素
5、的个数,即 Graphic 对象的总数。现在要求你完成该类的求所有 Graphic 对象总面积函数 getAllArea 的实现代码。为了完成该函数,允3许你在其它相关类中增加方法及其实现。程序设计(C+语言)期中考试参考答案一、简答题(每小题 5 分,共 20 分)6. 在 C+中声明类时,如何做到多个实例共享一个全局变量?声明一个类静态成员变量。 7. 引用和指针之间有什么区别?引用是一个别名,而指针是一个保存地址的变量。8. 什么是抽象类?析构函数可以声时为虚函数吗?如果可以,在什么情况下使用?如果一个类中包括纯虚函数,则该类为抽象类,抽象类不能实例化,主要是作为接口定义。一般情况下类的
6、析构函数都定义成虚函数,主要是考虑在使用基类指针操作派生类对象时保证类的析构顺序。9. 什么是多态性?多态性是如何实现的? 函数多态性是指用多个含义重载一个函数的能力,即允许创建多个名称相同的函数。可通过改变同名函数变元的类型或个数来实现。10. 构造函数与析构函数的功能是什么? 在继承层次上,构造函数和析构函数的调用顺序如何? 构造函数用来初始化。析构函数用来做清除工作,一般包括内存释放。在 继承层次上,构造函数和析构函数的调用顺序为:构造函数是先基类,后派生类;析构函数是先派生类,后基类。二、程序改错题 (每小题 5 分,共 20 分)5. 下面的程序是否有错误,如果有错,请说明原因并改正
7、。# include int * FuncOne() int * pint = new int(5); count int FuncOne() int * pint = new int(5); cout 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 ;Sample PrintAndDouble( Sample o)
8、cout Print(); D d(4); d.Print ();B * p = 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 ;strc
9、py( 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 template class myclass T i;public:myclass (T a) i = a; void show( ) cout obj(This); obj.show();该程序输出结果为
10、:This 五、程序设计题(共 20 分)设有如下定义的几个类,其中,Graphic 是个抽象类,它定义了平面封闭图形应该具有的运算求面积 getArea,它可以有任意多子类,如 Circle 和 Rectangle 便是它的两个子类。GraphicContainer 是一个包含 Graphic 对象的类,该类有两个数据成员,其中:m_buffer 是个数组,用于存放不同的Graphic 对象; m_sum 用来表示该数组中实际存放元素的个数,即 Graphic 对象的总数。现在要求你完成该类的求所有 Graphic 对象总面积函数 getAllArea 的实现代码。为了完成该函数,允许你在其
11、它相关类中增加方法及其实现。#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:
12、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:GraphicContain
13、er(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: coutheightwidth;m_bufferi=new Triangle(height, width);break;case R:coutheightwidth;m_bufferi=new Rectangle(height, width);break;case C:coutredius;m_bufferi=new Circle(redius);break;default:coutgetArea();return sumArea;void main() GraphicContainer GC(4);cout“All Areas sum = “GC.getAllArea()endl;