void fun1(B*ptr){ptr->show();} void fun2(B &ref){ref.show();} void fun3(B b){b.show();} void main(){
B b,*p=new D; D d; fun1(p); fun2(b); fun3(d); }
程序的输出结果是 D
A DBD B BBD C DBB D Dbb 二、填空题(每空2分,共三十分)
(6).在有定义语句:int a=3,b=2,c=1;则表达式a
For (int j=I;j<=I;j++) For (int j=I;j<=I;j++) Sum++;
(8).已知有函数f的定义如下: Int f(){
Static int -6; s-2; return s }
则在某程序中第二次执行函数用语句f();时,函数f的返回值是_____. (9)已知如下程序得输出结果时23,请将划线处缺失得部分补充完整。 #include
Void print() cout{cout<<23;} }
Int main(){
Myclass*p=new myclass(); ______print(); Return(); Class sample{ Public:
Sample(){} _________ }
6
(14)请将如下程序补充完整,使得输出结果为bbaa #include
_____{cout<<”aa”;} };
Class B :public A{ Public:
~B(){cout<<”bb”;} };
Int main (){
B*p=new B; Delete p; Return(); }
(15)请将下列栈类stack补充完整 Class stack{ Private;
Int plist[100]; //int 数组,用于存放栈的元素 Int top; //栈顶元素(数组下标) Public:
Stack():top(0){}
Void push(sons int &item); //新元素item压入栈 Int pop(void); //将栈顶元素弹出栈 };
Void stack::pbsh(const int &item){
If (top ==99) //如果栈满,程序结束 Exit(1);
Top++; //栈顶指针增1 ___【15】____; }
7
Int stack::pop(){
If (top<0) //如果栈空,程序结束 Exit(1);
Return plish[top--]; } 参考答案
6.2 7.6 8.4 9.(*p) 10.ABBA 11.void Base::fun() 12.virtual display()==0; 14.-A() 15.pList[top]=item
2008年9月计算机等级考试
一、选择题(每小题2分,共70分)
(11)在下列原型所示的 C++函数中,按“传值”方式传递参数的是(A)。 A)void f1(int x); B)void f2(int*x); C)void f3(const int*x); D)void f4(int&x);
(12)在 C++中,编译系统自动为一个类生成缺省构造函数的条件是(A)。 A)该类没有定义任何有参构造函数 B)该类没有定义任何无参构造函数 C)该类没有定义任何构造函数 D)该类没有定义任何成员函数
(13)在一个派生类的成员函数中,试图调用其基类的成员函数“void f();”,但无法通过编译。这说明(C)。
A)f()是基类的私有成员 C)派生类的继承方式为私有 B)f()是基类的保护成员 D)派生类的继承方式为保护 (14)下列关于运算符重载的叙述中,错误的是(B)。 A)有的运算符可以作为非成员函数重载
B)所有的运算符都可以通过重载而被赋予新的含义 C)不得为重载的运算符函数的参数设置默认值 D)有的运算符只能作为成员函数重载
(15)下列关于模板的叙述中,错误的是(D)。
A)调用模版函数时,在一定条件下可以省略模板实参 B)可以用 int、double 这样的类型修饰符来声明模版参数
C)模板声明中的关键字 class 都可以用关键字 typename 替代 D)模板的形参表中可以有多个参数
(16)要利用 C++流进行文件操作,必须在程序中包含的头文件是(B)。 A)iostream B)fstream C)strstream D)iomanip (17)下列字符串中不能作为 C++标识符使用的是(B)。 A)WHILE B)user C)_lvar D)9stars (18)下列语句中错误的是(D)。 A)const int a; B)const int a=10; C)const int*point=0; D)const int*point=new int(10);
(19)有如下程序: #include
8
for(int i=0; i<6; i+=3){ sum=i;
for(int j = i; j<6; j++)sum+=j; }
cout< 运行时的输出结果是(A)。 A)3 B)10 C)12 D)15 (20)下列语句中,正确的是(D)。 A)char*myString=\ B)char myString=\ C)char myString[11]=\ D)char myString[12]=\ (21)若已经声明了函数原型“void fun(int a, double b=0.0);”,则下列重载函数声明中正确的是(C)。 A)void fun(int a=90, double b=0.0); B)int fun(int a, double B); C)void fun(double a, int B); D)bool fun(int a, double b = 0.0); (22)有如下程序: #include Sample(){} ~Sample(){cout<<'*';} }; int main(){ Sample temp[2], *pTemp[2]; return 0; } 执行这个程序输出星号(*)的个数为(B)。 A)1 B)2 C)3 D)4 (23)下列选项中,与实现运行时多态性无关的是(A)。 A)重载函数 B)虚函数 C)指针 D)引用 (24)下列运算符函数中,肯定不属于类 Value 的成员函数的是(B)。 A)Value operator+(Value); B)Value operator-(Value, Value); C)Value operator*(int); D)Value operator/(Value); (25)下列模板声明中,有语法错误的是(D)。 A)template B)template (26)在语句“cout<<'A';”中,cout 是(B)。 A)类名 B)对象名 C)函数名 D)C++的关键字 (27)有如下程序: #include 9 using namespace std; class MyClass{ public: MyClass(int i=0){cout<<1;} MyClass(const MyClass&x){cout<<2;} MyClass& operator=(const MyClass&x) {cout<<3; return*this;} ~MyClass(){cout<<4;} }; int main(){ MyClass obj1(1),obj2(2),obj3(obj1); return 0: } 运行时的输出结果是(A)。 A)112444 B)11114444 C)121444 D)11314444 (28)有如下程序: #include MyClass(int x):val(x){} void Set(int x){val=x;} void Print()const{cout<<\private: int val; }; int main(){ const MyClass obj1(10); MyClass obj2(20); obj1.Print(); //语句 1 obj2.Print(); //语句 2 obj1.Set(20); //语句 3 obj2.Set(30); //语句 4 return 0; } 其主函数中错误的语句是(C)。 A)语句 1 B)语句 2 C)语句 3 D)语句 4 (29)在类声明中,紧跟在“public:”后声明的成员的访问权限是(A)。 A)私有 B)公有 C)保护 D)默认 (30)对于通过公有继承定义的派生类,若其成员函数可以直接访问基类的某个成员,说明该基类成员的访问权限是(D)。 A)公有或私有 B)私有 C)保护或私有 D)公有或保护 (31)定义派生类时,若不使用关键字显式地规定采用何种继承方式,则默认方式为(A)。 A)私有继承 B)非私有继承 C)保护继承 D)公有继承 10