第五章
1.下面关于C++中类的继承与派生的说法错误的是()。
A.基类的protected成员在公有派生类的成员函数中可以直接使用
B.基类的protected成员在私有派生类的成员函数中可以直接使用
C.私有派生时,基类的所有成员访问权限在派生类中保持不变
D.继承可以分为单一继承与多重继承
2.派生类继承基类时,如果省略继承方式,则默认为()继承。
A.公有B.保护 C. 私有
3.当一个派生类公有继承一个基类时,基类中的所有公有成员成为派生类的()。
A. public成员
B. private成员
C. protected 成员
D. 友元
4.关于多重继承二义性的描述,错误的是()。
A.派生类的多个基类中存在同名成员时,派生类对这个成员访问可能出现二义性
B.一个派生类是从具有共同的间接基类的两个直接基类派生来的,派生类对该公共基类的访问可能出现二义性
C.解决二义性最常用的方法是作用域运算符对成员进行限定
D.派生类和它的基类中出现同名函数时,将可能出现二义性
5.在如下继承层次下,当实例化有派生类对象时,调用构造函数的顺序为()。
class Base{…};
class Base1: virtual Base{…};
class Base2: virtual Base{…};
class Derived : public Base2, public Base1{…};
A. Base(), Base1(), Base(), Base2(), Drived()
B. Base(), Base2(), Base(), Base1(), Drived()
C. Base(), Base1(), Base2(), Drived()
D. Base(), Base2(), Base1(), Drived()
6.派生类的对象对它的基类成员中()是可以访问的。
A. 公有继承的公有成员
B. 公有继承的私有成员
C. 公有继承的保护成员
D. 私有继承的公有成员
7.派生类采用()方式可以使基类中的保护数据成员成为自己的私有数据成员。
A. 私有继承
B.保护继承
C. 公有继承
D.私有、保护、公有均可
8.在如下继承层次下,有关访问权限描述不正确的为()。
class A{
protected: int x;
public : void set_X( int i){ x = i;} ;
int get_X( ){ return x; } ;
};
class B : public A
{ int y; };
class C: protected B{…};
A. B类中可以直接访问父类A中的数据成员x;
B. B类对象不可以直接访问其数据成员y;
C. C 类对象可以调用get_X()访问父类数据成员x;
9.如有基类Base和派生类Derived,实例化对象Base b; Derived d;叙述正确的是()。A.Derived* pDev = new Base();
B.Derived类中不能定义与Base类同名的成员,以免引用时引起歧义;
C.Base* pb =&d ; Derived* pd = &b; 为正确的赋值操作;
D.Base& rb = d; Base bb = d; 为正确的赋值操作;
10.分析以下程序的执行结果。
#include
using namespace std;
class base{
int n;
public:
base (int a){
cout << "constructing base class" << endl;
n=a;
cout << "n=" << n << endl;
}
~base(){ cout << "destructing base class" << endl; }
};
class subs : public base{
int m;
public:
subs(int a, int b) : base(a){
cout << "constructing sub class" << endl;
m=b;
cout << "m=" << m << endl;
}
~subs(){ cout << "destructing sub class" << endl; }
};
void main (){
subs s(1,2);
}
11.分析以下程序的执行结果。
#include
using namespace std;
class Animal{
protected:
string name ;
public:
Animal(string n):name(n)
{ cout<< "Animal 1"< Animal():name("无名") { cout<< "Animal 0"< void show() { cout< }; class Cat: public Animal { int year; public: Cat( ) { cout<<"Cat 0"< Cat(string n, int y):Animal(n),year(y) { cout<<"Cat 1"< void show() { cout< void main() { Cat cat1, cat2("Garfield",3); cat1.show(); cat2.show(); }