22、【
单选题
】 有如下程序:
#include<iostream>
using namespace std;
class A{
public
static int a;
void init(){a=1;}
A(int a=2) {init();a++;}
};
int A:;a=0
A obj;
Int main()
{
cout<<obj.a;
retum0;
}
运行时输出的结果是
[2分]
27、【
单选题
】 有如下程序:
#include<iostream>
using namespace std;
class test {
private:
int a;
public:
test(){cout<<”constructor”<<endl;}
test(int a){cout<<a<<endl;}
test(const test&_test)
{
a=_testa;
cout<<”copy constructor”<<endl;
}
test(){cout<<”destructor”<<endl;}
};
int main()
}
test A(3)
return0;
运行时输出的结果是
[2分]
、
3
、
constructor
destruclor
、
copy constructor
、
3
dstructor destruclor
答案:
28、【
单选题
】 若有如下类声明
class My Class {
public:
MyClass() {cout<<1;}
};
执行下列语句
MyClass a,b[2],*P[2];
以后,程序的输出结果是
[2分]
、
11
、
111
、
1111
、
11111
答案:
29、【
单选题
】 有如下程序:
#include<iostream>
using namespace std;
class point {
public:
static int number;
public:
point(){number++;
~point(){number-;}
};
imt point::number=0;
void main(){
point*ptr;
printA,B;
{
point*ptr_point=new point{3};
ptr=ptr_point;
}
pointC;
cout<<point::number<<endl;
delete[]ptr;
}
运行时输出的结果是
[2分]
、
3
、
4
、
6
、
7
答案:
30、【
单选题
】 如果不使用多态机制,那么通过基类的指针虽然可以指向派生类对象,但是只能访问从基类继承的成员,有如下程序,没有使用多态机制。
#include<iostream>
using namespace std;
class Base{
int a,b;
public:
Bace(int x ,int y) { a=x; b=y;}
Void show(){ cout<<a<<’,’<<b<<endl;}
}
clsaa Derived:public Base {
int c ,d;
public:
Derived(int x,int y,int z,int m):Base(x,y){c=z; d=m;}
Void show(){ cout<<c<<’,’d<<endl;}
};
int main()
{
Base B1(50,50),*pb;
Derived DI(10,20,30,40);
Pb=&D1;
Pb->show();
Return 0;
}
运行时输出的结果是
[2分]
、
10,20
、
30,40
、
20,30
、
50,50
答案:
31、【
单选题
】 有如下程序:
#include<iostream>
using namespace std;
class A
{
public;
A(int i ){x= i ;}
Void sispa(){cout<<x<<’,’;}
Private:
int x ;
};
class B;publicA
{
public;
B(int i):A(i +10) {x =i ;}
voud dispb() {dispa();cout<<x<,endl;}
Private:
Int x;
};
int main()
{
B b(2);
b.dispb()
retum 0:
}
运行时输出的结果是
[2分]
、
10,2
、
12,10
、
12,2
、
2,2
答案:
32、【
单选题
】 虚函数支持多态调用,一个基类的指针可以指向派生类的对象,而且通过这样的指针调用虚函数时,,被调用的是指针所指的实际对象的虚函数,而非虚函数不支持多态调用。有如下程序:
#include<iostream>
using namespace std;
class Base
{
public:
virtual void f(){cout<<”f0+”;}
void g() {cout<<”g0+”;}
};
class Derived:public Base
{
public:
void f() {cout<<”f+”;}
void g() {cout<<”g+”;}
};
int main()
{
Derived d;
Base*p=&d;
p->f(); p->g();
return 0:
}
运行时输出的结果是
[2分]
、
f+g+
、
f0+g+
、
f+g0+
、
f0+g0+
答案:
33、【
单选题
】 下面程序中对一维坐标点类Point进行运算符重载
#include<iostream>
using namespace std;
class point {
public:
point (int vaI) {x=val;}
point& operator++(){x++;retum*this;}
print operator++(int) {point old=*this,++(*this);retum old;}
int GetX() const {retum x;}
private:
int x;
};
int main()
{
point a(10);
cout<<(++a).GetX();
cout<<a++.GetX();
retum();
}
编译和运行情况是
[2分]
、
运行时输出1011
、
运行时输出1111
、
运行时输出1112
、
编译有错
答案:
34、【
单选题
】 有如下程序:
#include<iostream>
using namespace std;
int main()
{
char str[100],*p;
cout<<”please input a string:”;
cin>>str;
p=str;
for(int i =0;*p!=”\0”;p++, i ++);
cout<< i <<endl;
retum 0;
}
运行这个程序时,若输入字符串为
abcdefg abcd
则输出结果是
[2分]
、
7
、
12
、
13
、
100
答案:
35、【
单选题
】 有如下程序::
#include<iostream>
using namespace std:
class Sample
{
friend long fun (Sample s);
public:
Sample (long a ){x=a;}
Private:
long x;
};
long fun (Sample s)
{
if(s.x<2)retum 1;
retum s.x*fun(Sample(s.x-a));
}
int main()
{
int sum=0
for(int i =0; i <6; i ++) { sum+=fun(Sample( i );) }
cout<<sum;
retum 0;
}
运行时输出的结果是
[2分]