C++模拟题5精品文档11页.doc
如有侵权,请联系网站删除,仅供学习与交流C+模拟题5【精品文档】第 11 页C+程序设计模拟试卷(五)一、单项选择题(本大题共20小题,每小题1分,共20分)在每小题列出的四个备选项中只有一个是符合题目要求的,请将其代码填写在题后的括号内。错选、多选或未选均无分。1. 静态成员函数没有(B)A. 返回值B. this指针C. 指针参数D. 返回类型2. 在类的定义中,用于为对象分配内存空间,对类的数据成员进行初始化并执行其他内部管理操作的函数是(C)A. 友元函数B. 虚函数C. 构造函数D. 析构函数3. 所有在函数中定义的变量,都是(B)A. 全局变量B. 局部变量C. 静态变量D. 寄存器变量4. 假定AB为一个类,则执行“AB a(2), b3,*p4;”语句时调用该类构造函数的次数为(B)A. 3B. 4C. 5D. 95. 如果表达式+a中的“+”是作为成员函数重载的运算符,若采用运算符函数调用格式,则可表示为(B)A. a.operator+(1)B. operator+(a)C. operator+(a,1)D. a.operator+()6. 已知f1和f2是同一类的两个成员函数,但f1不能直接调用f2,这说明(C)A. f1和f2都是静态函数B. f1不是静态函数,f2是静态函数C. f1是静态函数,f2不是静态函数D. f1和f2都不是静态函数7. 一个函数功能不太复杂,但要求被频繁调用,则应把它定义为 (A)A. 内联函数B. 重载函数C. 递归函数D. 嵌套函数8. 解决定义二义性问题的方法有(B)A. 只能使用作用域分辨运算符B. 使用作用域分辨运算符或成员名限定C. 使用作用域分辨运算符或虚基类D. 使用成员名限定或赋值兼容规则9. 在main函数中可以用p.a的形式访问派生类对象p的基类成员a,其中a是(D)A. 私有继承的公有成员B. 公有继承的私有成员C. 公有继承的保护成员D. 公有继承的公有成员10. 在C+中不返回任何类型的函数应该说明为(C)A. intB. charC. voidD. double11. 若Sample类中的一个成员函数说明如下:void set(Sample& a),则Sample& a的含义是(B)A. 指向类Sample的名为a的指针B. a是类Sample的对象引用,用来作函数Set()的形参C. 将a的地址赋给变量SetD. 变量Sample与a按位与的结果作为函数Set的参数12. 要实现动态联编必须(B)A. 通过成员名限定来调用虚函数B. 通过对象名来调用虚函数C. 通过派生类对象来调用虚函数D. 通过对象指针或引用来调用虚函数13. 在派生类中定义虚函数时,可以与基类中相应的虚函数不同的是(B)A. 参数类型B. 参数个数C. 函数名称D. 函数体14. 实现两个相同类型数加法的函数模板的声明是(D)A. add(T x,T y)B. T add(x,y)C. T add(T x,y)D. T add(T x,T y)15. 下列不是描述类的成员函数的是(C)A. 构造函数B. 析构函数C. 友元函数D. 拷贝构造函数16. 继承机制的作用是(C)A. 信息隐藏B. 数据封装C. 定义新类D. 数据抽象17. 已知:p是一个指向类A数据成员m的指针,A1是类A的一个对象。如果要给m赋值为5,正确的是(C)A. A1.p=5;B. A1->p=5;C. A1.*p=5;D. *A1.p=5;18. 如果采用动态多态性,要调用虚函数的是()A. 基类对象指针B. 对象名C. 基类对象D. 派生类名19. 若有以下定义,则说法错误的是(B)int a=100,*p=&a;A. 声明变量p,其中*表示p是一个指针变量B. 变量p经初始化,获得变量a的地址C. 变量p只可以指向一个整型变量D. 变量p的值为10020. C+语言建立类族是通过(B)A. 类的嵌套B. 类的继承C. 虚函数D. 抽象类二、填空题(本大题共20小题,每小题1分,共20分)请在每小题的空格中填上正确答案。错填、不填均无分。1. 假设int a=1,b=2;则表达式(+a/b)*b-的值为_2_。2. 复制构造函数使用_构造函数_作为形式参数。3. 通过C+语言中的_继承_机制,可以从现存类中构建其子类。4. 静态成员函数、友元函数、构造函数和析构函数中,不属于成员函数的是_友元函数_。5. 在下面的类定义中,私有成员有_X,Y_ length,height;_。class Locationint X,Y;protected:int zeroX,zerxY;int SetZero(intzeroX, intzeroY);private:int length,height;public:void init(int initX,int initY);int GetX();int GetY();6. 在C+程序设计中,建立继承关系倒挂的树应使用_单继承_继承。7. C+支持的两种多态性分别是_静态_多态性和运行多态性。8. C+中语句const char * const p=“hello”;所定义的指针p和它所指的内容都不能被_赋值_。9. 在C+中,定义虚函数的关键字是_virteor_。10. 采用私有派生方式,基类的public成员在私有派生类中是_私有_成员。11. 对赋值运算符进行重载时,应声明为_inline_函数。12. 在C+中有两种参数传递方式即值传递和_引用_传递。13. 预处理命令以_#define_符号开头。14. 在构造函数和析构函数中调用虚函数时采用_构造函数_。15. C+是通过引用运算符_来定义一个引用的。16. 如果要把类B的成员函数void fun()说明为类A的友元函数,则应在类中加入语句_friend void B :fun();_。17. 如果要把PI声明为值为3.14159类型为双精度实数的符号常量,该声明语句是_const double PI 3.14159_。18. 在C四个流对象中,_cout_用于标准屏幕输出。19. 执行下列代码int a=32;double c=32;cout.setf(ios:hex);cout<<"hex:a="<<a<<",c="<<c<<endl;cout.unsetf(ios:hex);程序的输出结果为_ hex:a=20_,c=30_。20. 已知有20个元素int类型向量V1,若用V1初始化为V2向量,语句是vetiror<int>(20)_。三、改错题(本大题共5小题,每小题4分,共20分)1. #include <iostream.h>class A private: publicint x;public:A(int i)x=i;A()x=0;friend int min(A&,A&);int min(A & a,A &b) return (a.x>b.x)?a.x:b.x;void main() A a(3),b(5);cout<<a.min(a,b)<<endl;2. #include <iostream.h>class shapepublic:virtual int area()return 0;class rectangle:public shapepublic:int a, b;void setLength (int x, int y) a=x;b=y;int area() return a*b;void main()rectangle r;r.setLength(3,5);shape s1,*s2=&r;cout <<r.area() <<endl;s2=s1;cout <<s2.area()<<endl;3. 下面的类定义中有一处错误,请用下横线标出错误所在行并给出修改意见。#include <iostream.h>template <class T>class Aprivate:T x,y,s;public:A(T a,T b)x=a,y=b;s=x+y;void show()cout<<"x+y="<<s<<endl;void main() A <int>add(10,100);add.show();4. 生成具有n个元素的动态数组。#include <iostream.h>void main()int n;cin>>n;int an;a0=2;cout<<a0<<endl;5. #include <iostream.h>class Aint i;public:virtual void fun()=0;A(int a)i=a;class B:public Aint j;public:void fun()cout<<"B:fun()n" B(int m,int n=0):A(m),j(n)void main()A *pa;B b(7);pa=&b;四、完成程序题(本大题共5小题,每小题4分,共20分)1. 在下面程序横线处填上适当字句,以使该程序执行结果为:50 4 34 21 100 7.1 8.1 9.1 10.1 11.1#include <iostream.h>template <class T>void f (_Ta,_int_n_)_;for (int i=0;i<n/2;i+)t=ai, ai=an-1-i, an-1-i=t;void main ()int a5=10,21,34,4,50;double d6=11.1,10.1,9.1,8.1,7.1;f(a,5);f(d,6);for (int i=0;i<5;i+)cout <<ai<< ""cout <<endl;for (i=0;i<6;i+)cout << di << ""cout << endl;2. 完成下面类中成员函数的定义。#include <iostream.h>#include <iomanip.h>class Arrprotected:float *p;int n;/数组大小(元素个数)public:Arr(int sz=10) n=sz;p=new floatn;Arr(void)_deletep;_int Getn(void) constreturn n;float & operator(int i)_void Print();void Arr:Print()int i;for(i=0;i< this->Getn();i+)if (i%10=0)cout << endl;cout<<setw(6)<<pi;cout<<endl;void main()Arr a(20);for (int i=0;i<a.Getn();i+)ai=i* 2;a.Print();3. 下面是一个输入半径,输出其面积和周长的C+程序,在下划线处填上正确的语句。#include <iostream>_using namespace std;_;_const double pi 3.14159_;void main()double rad;cout<<"rad="cin>>rad;double l=2.0*pi*rad;double s=pi*rad*rad;cout<<"n The long is:"<<l<<endl;cout<<"The area is:"<<s<<endl;4. 在下划线处填上缺少的部分。#include <iostream.h>class Samppublic:void Setij(int a,int b)i=a,j=b;Samp()cout<<"Destroying."<<i<<endl;int GetMuti()return i*j;protected:int i;int j;int main()Samp *p;p=new Samp5;if(!p)cout<<"Allocation errorn"return 1;for(int j=0;j<5;j+)pj.Setij(j,j);for(int k=0;k<5;k+)cout<<"Muti"<<k<<" is:"<<pk._ GetMuti()_<<endl;_p. GetMuti();_return 0;5. 请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:11,1013,12#include <iostream.h>class Aint a;public:A(int i=0)a=i;int Geta()return a;void show()cout<<a<<endl;class BA a;int b;public:B(int i,int j)_:A_(int i=0)_void show()cout<<a.Geta()<<","<<b<<endl;void main()B b2=B(10,11),B(12,13);for(int i=0;i<2;i+)_b.show();_五、程序分析题(本大题共2小题,每小题5分,共10分)1. 给出下面程序输出结果。#include <iostream.h>class Baseprivate:int Y;public:Base(int y=0) Y=y;cout<<"Base("<<y<<")n"Base() cout<<"Base()n"void print() cout <<Y<< ""class Derived:public Baseprivate:int Z;public:Derived (int y, int z):Base(y)Z=z;cout<<"Derived("<<y<<","<<z<<")n"Derived() cout<<"Derived()n"void print()Base:print();cout<<Z<<endl;void main()Derived d(10,20);d.print();2. 给出下面程序输出结果。#include <iostream.h>class testint x;public:test(int i=0):x(i)virtual void fun1()cout << "test:x"<<x<<endl;class ft:public testint y;public:void fun1()cout <<"ft:y="<<y<<endl;ft(int i=2):test(i),y(i)void main() ft ft1(3);void (test:*p)();p=test:fun1;(ft1.*p)();六、程序设计题(本大题共1小题,共10分)1. 求n(n=3)个学生的最高分和最低分及姓名,已有student类声明和main函数,完成student类的实现部分。#include <iostream.h>#include <string.h>class studentchar name10;int deg;public:student(char na="",int d=0);char * getname();friend int compare(student &s1,student &s2);int getdeg();void main()student st=student("王强",74),student("李刚",68),student("张雪",84);int i=0,min=0,max=0;for(i=1;i<3;i+)if(compare(stmax,sti)=-1)max=i;if(compare(stmin,sti)=1)min=i;cout<<"最高分:"<<stmax.getdeg()<<"姓名:"<<stmax.getname()<<endl;cout<<"最低分:"<<(*(st+min).getdeg()<<"姓名:"<<stmax.getname()<<endl;