考试题,哪位大侠帮忙搂搂??谢谢!!
二)、分析下面的代码,回答问题(每题 10 分,共 40 分)
1.下面程序的执行结果是什么?为什么?
class B{
protected:
virtual void f( ){cout<<"B"<<endl;}
public:
void g( ){f( );}
};
class C:public B{
protected:
void f( ){cout<<"C"<<endl;}
};
void main( ) {
C d;
d.g( );
}
2.下面程序的执行结果是什么?为什么?
class BC {
public:
BC( ) { cout << "BC' constructor\n"; }
~BC( ) { cout << "BC' destructor\n"; }
};
class DC : public BC {
public:
DC( ) { cout << "DC' constructor\n"; }
~DC( ) { cout << "DC' destructor\n"; }
};
int main( ) {
DC d;
return 0;
}
3. 下面程序的执行结果是什么?为什么?
void main()
{
char* pn[]={"Fred","Barney","Wilma","Betty",NULL};
print(pn);
}
void print(char* arr[])
{
while(*arr!=NULL){
cout << *arr <<endl;
arr++;
}
}
4.下面程序的执行结果是什么?为什么?
class B
{
int b;
public:
B(){};
B(int i){b=i;};
virtual void func()
{
cout<<"B::func() called"<<endl;
}
};
class D:public B
{
public:
D(){};
D(int i, int j):B(j){d=j;}
private:
int d;
void func()
{
cout<<"D::func() called"<<endl;
};
};
void gfunc(B *obj)
{
obj->func();
}
void main()
{
D *pd=new D;
gfunc(pd);
}